Skip to content

Commit f7d7714

Browse files
committed
Expose draft flag in github_pr_destination
BUG=259290334 PiperOrigin-RevId: 488957715 Change-Id: I6679d5cd3e0f562821f94853e179d4e3a6c451fe
1 parent 8adb97a commit f7d7714

File tree

9 files changed

+50
-17
lines changed

9 files changed

+50
-17
lines changed

docs/reference.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2506,7 +2506,7 @@ primary_branch_migration | `bool`<br><p>When enabled, copybara will ignore the '
25062506

25072507
Creates changes in a new pull request in the destination.
25082508

2509-
`destination` `git.github_pr_destination(url, destination_ref='master', pr_branch=None, partial_fetch=False, allow_empty_diff=True, title=None, body=None, integrates=None, api_checker=None, update_description=False, primary_branch_migration=False, checker=None)`
2509+
`destination` `git.github_pr_destination(url, destination_ref='master', pr_branch=None, partial_fetch=False, allow_empty_diff=True, title=None, body=None, integrates=None, api_checker=None, update_description=False, primary_branch_migration=False, checker=None, draft=False)`
25102510

25112511

25122512
#### Parameters:
@@ -2525,6 +2525,7 @@ api_checker | `checker` or `NoneType`<br><p>A checker for the GitHub API endpoin
25252525
update_description | `bool`<br><p>By default, Copybara only set the title and body of the PR when creating the PR. If this field is set to true, it will update those fields for every update.</p>
25262526
primary_branch_migration | `bool`<br><p>When enabled, copybara will ignore the 'desination_ref' param if it is 'master' or 'main' and instead try to establish the default git branch. If this fails, it will fall back to the param's declared value.<br>This is intended to help migrating to the new standard of using 'main' without breaking users relying on the legacy default.</p>
25272527
checker | `checker` or `NoneType`<br><p>A checker that validates the commit files & message. If `api_checker` is not set, it will also be used for checking API calls. If only `api_checker`is used, that checker will only apply to API calls.</p>
2528+
draft | `bool`<br><p>Flag create pull request as draft or not.</p>
25282529

25292530

25302531
#### Examples:

java/com/google/copybara/git/GitHubPrDestination.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class GitHubPrDestination implements Destination<GitRevision> {
6767
private final String destinationRef;
6868
private final String prBranch;
6969
private final boolean partialFetch;
70+
private final boolean draft;
7071
private final boolean primaryBranchMigrationMode;
7172

7273
private final GeneralOptions generalOptions;
@@ -92,6 +93,7 @@ public class GitHubPrDestination implements Destination<GitRevision> {
9293
String destinationRef,
9394
@Nullable String prBranch,
9495
boolean partialFetch,
96+
boolean draft,
9597
GeneralOptions generalOptions,
9698
GitHubOptions gitHubOptions,
9799
GitDestinationOptions destinationOptions,
@@ -111,6 +113,7 @@ public class GitHubPrDestination implements Destination<GitRevision> {
111113
this.destinationRef = Preconditions.checkNotNull(destinationRef);
112114
this.prBranch = prBranch;
113115
this.partialFetch = partialFetch;
116+
this.draft = draft;
114117
this.generalOptions = Preconditions.checkNotNull(generalOptions);
115118
this.gitHubOptions = Preconditions.checkNotNull(gitHubOptions);
116119
this.destinationOptions = Preconditions.checkNotNull(destinationOptions);
@@ -181,7 +184,7 @@ public Writer<GitRevision> newWriter(WriterContext writerContext) throws Validat
181184
generalOptions,
182185
gitHubPrWriteHook,
183186
state,
184-
/*nonFastForwardPush=*/ true,
187+
/* nonFastForwardPush= */ true,
185188
integrates,
186189
destinationOptions.lastRevFirstParent,
187190
destinationOptions.ignoreIntegrationErrors,
@@ -262,7 +265,7 @@ public ImmutableList<DestinationEffect> write(
262265
api.updatePullRequest(
263266
getProjectName(),
264267
pr.getNumber(),
265-
new UpdatePullRequest(title, prBody, /*state=*/ null));
268+
new UpdatePullRequest(title, prBody, /* state= */ null));
266269
}
267270
result.add(
268271
new DestinationEffect(
@@ -283,7 +286,7 @@ public ImmutableList<DestinationEffect> write(
283286
PullRequest pr =
284287
api.createPullRequest(
285288
getProjectName(),
286-
new CreatePullRequest(title, prBody, prBranch, getDestinationRef()));
289+
new CreatePullRequest(title, prBody, prBranch, getDestinationRef(), draft));
287290
console.infoFmt(
288291
"Pull Request %s/pull/%s created using branch '%s'.",
289292
asHttpsUrl(), pr.getNumber(), prBranch);

java/com/google/copybara/git/GitModule.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,12 @@ public GitDestination gitHubDestination(
17711771
+ "is used, that checker will only apply to API calls.",
17721772
named = true,
17731773
positional = false),
1774+
@Param(
1775+
name = "draft",
1776+
defaultValue = "False",
1777+
named = true,
1778+
positional = false,
1779+
doc = "Flag create pull request as draft or not."),
17741780
},
17751781
useStarlarkThread = true)
17761782
@UsesFlags({GitDestinationOptions.class, GitHubDestinationOptions.class})
@@ -1813,6 +1819,7 @@ public GitHubPrDestination githubPrDestination(
18131819
Boolean updateDescription,
18141820
Boolean primaryBranchMigrationMode,
18151821
Object checker,
1822+
boolean isDraft,
18161823
StarlarkThread thread)
18171824
throws EvalException {
18181825
GeneralOptions generalOptions = options.get(GeneralOptions.class);
@@ -1831,6 +1838,7 @@ public GitHubPrDestination githubPrDestination(
18311838
destinationRef,
18321839
convertFromNoneable(prBranch, null),
18331840
partialFetch,
1841+
isDraft,
18341842
generalOptions,
18351843
options.get(GitHubOptions.class),
18361844
destinationOptions,

java/com/google/copybara/git/github/api/CreatePullRequest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class CreatePullRequest extends GenericJson {
4242
@Key
4343
private String base;
4444

45+
@Key
46+
private final boolean draft;
47+
4548
public String getTitle() {
4649
return title;
4750
}
@@ -62,10 +65,15 @@ public void setTitle(String title) {
6265
this.title = title;
6366
}
6467

65-
public CreatePullRequest(String title, String body, String head, String base) {
68+
public boolean getDraft() {
69+
return draft;
70+
}
71+
72+
public CreatePullRequest(String title, String body, String head, String base, boolean draft) {
6673
this.title = Preconditions.checkNotNull(title);
6774
this.body = Preconditions.checkNotNull(body);
6875
this.head = Preconditions.checkNotNull(head);
6976
this.base = Preconditions.checkNotNull(base);
77+
this.draft = draft;
7078
}
7179
}

java/com/google/copybara/git/github/api/GitHubApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private <T> ImmutableList<T> paginatedGet(String path, String profilerName, Type
221221
}
222222
return builder.build();
223223
}
224-
224+
225225
/**
226226
* Create a pull request
227227
*/

java/com/google/copybara/git/github/api/PullRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class PullRequest extends PullRequestOrIssue implements StarlarkValue {
3838
@Key private Revision base;
3939
@Key("requested_reviewers") private List<User> requestedReviewers;
4040
@Key private Boolean mergeable;
41+
@Key private boolean draft;
4142

4243
@StarlarkMethod(name = "head", doc = "Information about head", structField = true)
4344
public Revision getHead() {
@@ -49,6 +50,11 @@ public Revision getBase() {
4950
return base;
5051
}
5152

53+
@StarlarkMethod(name = "draft", doc = "Whether pull request is a draft", structField = true)
54+
public boolean getDraft() {
55+
return draft;
56+
}
57+
5258
@Nullable
5359
public Boolean isMergeable() {
5460
// Explicit null values in JSON data are not automatically converted to Java null; see

java/com/google/copybara/git/github/api/testing/AbstractGitHubApiTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,15 @@ public void testCreatePullRequest() throws Exception {
340340
&& cpr.getHead().equals("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")),
341341
getResource("pulls_12345_testdata.json"));
342342
// The test does not actually use the data in the CreatePullRequest
343-
PullRequest pullRequest = api.createPullRequest("example/project",
344-
new CreatePullRequest("title",
345-
"[TEST] example pull request one",
346-
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
347-
"aabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
343+
PullRequest pullRequest =
344+
api.createPullRequest(
345+
"example/project",
346+
new CreatePullRequest(
347+
"title",
348+
"[TEST] example pull request one",
349+
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
350+
"aabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
351+
false));
348352

349353
assertThat(pullRequest.getNumber()).isEqualTo(12345);
350354
assertThat(pullRequest.getState()).isEqualTo("open");
@@ -699,7 +703,7 @@ private static <T> JsonValidator<T> createValidator(Class<T> clazz, Predicate<T>
699703
**/
700704
public static class TestCreatePullRequest extends CreatePullRequest {
701705
public TestCreatePullRequest() {
702-
super("invalid", "invalid", "invalid", "invalid");
706+
super("invalid", "invalid", "invalid", "invalid", false);
703707
}
704708
}
705709

java/com/google/copybara/git/github/api/testing/pulls_12345_testdata.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"state": "open",
1010
"locked": false,
1111
"title": "[TEST] example pull request one",
12+
"draft": false,
1213
"user": {
1314
"login": "googletestuser",
1415
"id": 123456,

javatests/com/google/copybara/git/GitHubPrDestinationTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public void testCustomTitleAndBody()
149149
+ "}",
150150
MockRequestAssertion.equals("{\"base\":\"main\","
151151
+ "\"body\":\"custom body\","
152+
+ "\"draft\":false,"
152153
+ "\"head\":\"feature\","
153154
+ "\"title\":\"custom title\"}")));
154155

@@ -199,6 +200,7 @@ public void testCustomTitleAndBody_withUpdate()
199200
+ "}",
200201
MockRequestAssertion.equals("{\"base\":\"main\","
201202
+ "\"body\":\"Body first a\","
203+
+ "\"draft\":false,"
202204
+ "\"head\":\"feature\","
203205
+ "\"title\":\"Title first a\"}")));
204206

@@ -312,8 +314,8 @@ public void testTrimMessageForPrTitle()
312314
+ " \"body\": \"test summary\""
313315
+ "}",
314316
MockRequestAssertion.equals(
315-
"{\"base\":\"main\",\"body\":\"Internal change.\\n\",\"head\":\"feature\","
316-
+ "\"title\":\"Internal change.\"}")));
317+
"{\"base\":\"main\",\"body\":\"Internal change.\\n"
318+
+ "\",\"draft\":false,\"head\":\"feature\",\"title\":\"Internal change.\"}")));
317319

318320
GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination("
319321
+ " url = 'https://github.com/foo',"
@@ -444,7 +446,7 @@ private void checkWrite(Revision revision)
444446
+ " \"body\": \"test summary\"\n"
445447
+ "}",
446448
MockRequestAssertion.equals(
447-
"{\"base\":\"main\",\"body\":\"test summary\\n\",\"head\":\""
449+
"{\"base\":\"main\",\"body\":\"test summary\\n\",\"draft\":false,\"head\":\""
448450
+ "feature"
449451
+ "\",\"title\":\"test summary\"}")));
450452

@@ -601,7 +603,7 @@ public void testWriteNomain() throws ValidationException, IOException, RepoExcep
601603
+ " \"body\": \"test summary\""
602604
+ "}",
603605
MockRequestAssertion.equals(
604-
"{\"base\":\"other\",\"body\":\"test summary\\n\",\"head\":\""
606+
"{\"base\":\"other\",\"body\":\"test summary\\n\",\"draft\":false,\"head\":\""
605607
+ branchName
606608
+ "\",\"title\":\"test summary\"}")));
607609

@@ -788,7 +790,7 @@ private void testBranchNameFromUser(String branchNameFromUser, String expectedBr
788790
+ " \"body\": \"test summary\""
789791
+ "}",
790792
MockRequestAssertion.equals(
791-
"{\"base\":\"other\",\"body\":\"test summary\\n\",\"head\":\""
793+
"{\"base\":\"other\",\"body\":\"test summary\\n\",\"draft\":false,\"head\":\""
792794
+ expectedBranchName
793795
+ "\",\"title\":\"test summary\"}")));
794796

0 commit comments

Comments
 (0)