Skip to content

Commit

Permalink
Expose draft flag in github_pr_destination
Browse files Browse the repository at this point in the history
BUG=259290334
PiperOrigin-RevId: 488957715
Change-Id: I6679d5cd3e0f562821f94853e179d4e3a6c451fe
  • Loading branch information
jordanlin00 committed Nov 17, 2022
1 parent 8adb97a commit f7d7714
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2506,7 +2506,7 @@ primary_branch_migration | `bool`<br><p>When enabled, copybara will ignore the '

Creates changes in a new pull request in the destination.

`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)`
`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)`


#### Parameters:
Expand All @@ -2525,6 +2525,7 @@ api_checker | `checker` or `NoneType`<br><p>A checker for the GitHub API endpoin
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>
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>
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>
draft | `bool`<br><p>Flag create pull request as draft or not.</p>


#### Examples:
Expand Down
9 changes: 6 additions & 3 deletions java/com/google/copybara/git/GitHubPrDestination.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class GitHubPrDestination implements Destination<GitRevision> {
private final String destinationRef;
private final String prBranch;
private final boolean partialFetch;
private final boolean draft;
private final boolean primaryBranchMigrationMode;

private final GeneralOptions generalOptions;
Expand All @@ -92,6 +93,7 @@ public class GitHubPrDestination implements Destination<GitRevision> {
String destinationRef,
@Nullable String prBranch,
boolean partialFetch,
boolean draft,
GeneralOptions generalOptions,
GitHubOptions gitHubOptions,
GitDestinationOptions destinationOptions,
Expand All @@ -111,6 +113,7 @@ public class GitHubPrDestination implements Destination<GitRevision> {
this.destinationRef = Preconditions.checkNotNull(destinationRef);
this.prBranch = prBranch;
this.partialFetch = partialFetch;
this.draft = draft;
this.generalOptions = Preconditions.checkNotNull(generalOptions);
this.gitHubOptions = Preconditions.checkNotNull(gitHubOptions);
this.destinationOptions = Preconditions.checkNotNull(destinationOptions);
Expand Down Expand Up @@ -181,7 +184,7 @@ public Writer<GitRevision> newWriter(WriterContext writerContext) throws Validat
generalOptions,
gitHubPrWriteHook,
state,
/*nonFastForwardPush=*/ true,
/* nonFastForwardPush= */ true,
integrates,
destinationOptions.lastRevFirstParent,
destinationOptions.ignoreIntegrationErrors,
Expand Down Expand Up @@ -262,7 +265,7 @@ public ImmutableList<DestinationEffect> write(
api.updatePullRequest(
getProjectName(),
pr.getNumber(),
new UpdatePullRequest(title, prBody, /*state=*/ null));
new UpdatePullRequest(title, prBody, /* state= */ null));
}
result.add(
new DestinationEffect(
Expand All @@ -283,7 +286,7 @@ public ImmutableList<DestinationEffect> write(
PullRequest pr =
api.createPullRequest(
getProjectName(),
new CreatePullRequest(title, prBody, prBranch, getDestinationRef()));
new CreatePullRequest(title, prBody, prBranch, getDestinationRef(), draft));
console.infoFmt(
"Pull Request %s/pull/%s created using branch '%s'.",
asHttpsUrl(), pr.getNumber(), prBranch);
Expand Down
8 changes: 8 additions & 0 deletions java/com/google/copybara/git/GitModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -1771,6 +1771,12 @@ public GitDestination gitHubDestination(
+ "is used, that checker will only apply to API calls.",
named = true,
positional = false),
@Param(
name = "draft",
defaultValue = "False",
named = true,
positional = false,
doc = "Flag create pull request as draft or not."),
},
useStarlarkThread = true)
@UsesFlags({GitDestinationOptions.class, GitHubDestinationOptions.class})
Expand Down Expand Up @@ -1813,6 +1819,7 @@ public GitHubPrDestination githubPrDestination(
Boolean updateDescription,
Boolean primaryBranchMigrationMode,
Object checker,
boolean isDraft,
StarlarkThread thread)
throws EvalException {
GeneralOptions generalOptions = options.get(GeneralOptions.class);
Expand All @@ -1831,6 +1838,7 @@ public GitHubPrDestination githubPrDestination(
destinationRef,
convertFromNoneable(prBranch, null),
partialFetch,
isDraft,
generalOptions,
options.get(GitHubOptions.class),
destinationOptions,
Expand Down
10 changes: 9 additions & 1 deletion java/com/google/copybara/git/github/api/CreatePullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class CreatePullRequest extends GenericJson {
@Key
private String base;

@Key
private final boolean draft;

public String getTitle() {
return title;
}
Expand All @@ -62,10 +65,15 @@ public void setTitle(String title) {
this.title = title;
}

public CreatePullRequest(String title, String body, String head, String base) {
public boolean getDraft() {
return draft;
}

public CreatePullRequest(String title, String body, String head, String base, boolean draft) {
this.title = Preconditions.checkNotNull(title);
this.body = Preconditions.checkNotNull(body);
this.head = Preconditions.checkNotNull(head);
this.base = Preconditions.checkNotNull(base);
this.draft = draft;
}
}
2 changes: 1 addition & 1 deletion java/com/google/copybara/git/github/api/GitHubApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private <T> ImmutableList<T> paginatedGet(String path, String profilerName, Type
}
return builder.build();
}

/**
* Create a pull request
*/
Expand Down
6 changes: 6 additions & 0 deletions java/com/google/copybara/git/github/api/PullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class PullRequest extends PullRequestOrIssue implements StarlarkValue {
@Key private Revision base;
@Key("requested_reviewers") private List<User> requestedReviewers;
@Key private Boolean mergeable;
@Key private boolean draft;

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

@StarlarkMethod(name = "draft", doc = "Whether pull request is a draft", structField = true)
public boolean getDraft() {
return draft;
}

@Nullable
public Boolean isMergeable() {
// Explicit null values in JSON data are not automatically converted to Java null; see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,15 @@ public void testCreatePullRequest() throws Exception {
&& cpr.getHead().equals("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")),
getResource("pulls_12345_testdata.json"));
// The test does not actually use the data in the CreatePullRequest
PullRequest pullRequest = api.createPullRequest("example/project",
new CreatePullRequest("title",
"[TEST] example pull request one",
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"aabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
PullRequest pullRequest =
api.createPullRequest(
"example/project",
new CreatePullRequest(
"title",
"[TEST] example pull request one",
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
"aabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
false));

assertThat(pullRequest.getNumber()).isEqualTo(12345);
assertThat(pullRequest.getState()).isEqualTo("open");
Expand Down Expand Up @@ -699,7 +703,7 @@ private static <T> JsonValidator<T> createValidator(Class<T> clazz, Predicate<T>
**/
public static class TestCreatePullRequest extends CreatePullRequest {
public TestCreatePullRequest() {
super("invalid", "invalid", "invalid", "invalid");
super("invalid", "invalid", "invalid", "invalid", false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"state": "open",
"locked": false,
"title": "[TEST] example pull request one",
"draft": false,
"user": {
"login": "googletestuser",
"id": 123456,
Expand Down
12 changes: 7 additions & 5 deletions javatests/com/google/copybara/git/GitHubPrDestinationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public void testCustomTitleAndBody()
+ "}",
MockRequestAssertion.equals("{\"base\":\"main\","
+ "\"body\":\"custom body\","
+ "\"draft\":false,"
+ "\"head\":\"feature\","
+ "\"title\":\"custom title\"}")));

Expand Down Expand Up @@ -199,6 +200,7 @@ public void testCustomTitleAndBody_withUpdate()
+ "}",
MockRequestAssertion.equals("{\"base\":\"main\","
+ "\"body\":\"Body first a\","
+ "\"draft\":false,"
+ "\"head\":\"feature\","
+ "\"title\":\"Title first a\"}")));

Expand Down Expand Up @@ -312,8 +314,8 @@ public void testTrimMessageForPrTitle()
+ " \"body\": \"test summary\""
+ "}",
MockRequestAssertion.equals(
"{\"base\":\"main\",\"body\":\"Internal change.\\n\",\"head\":\"feature\","
+ "\"title\":\"Internal change.\"}")));
"{\"base\":\"main\",\"body\":\"Internal change.\\n"
+ "\",\"draft\":false,\"head\":\"feature\",\"title\":\"Internal change.\"}")));

GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination("
+ " url = 'https://github.com/foo',"
Expand Down Expand Up @@ -444,7 +446,7 @@ private void checkWrite(Revision revision)
+ " \"body\": \"test summary\"\n"
+ "}",
MockRequestAssertion.equals(
"{\"base\":\"main\",\"body\":\"test summary\\n\",\"head\":\""
"{\"base\":\"main\",\"body\":\"test summary\\n\",\"draft\":false,\"head\":\""
+ "feature"
+ "\",\"title\":\"test summary\"}")));

Expand Down Expand Up @@ -601,7 +603,7 @@ public void testWriteNomain() throws ValidationException, IOException, RepoExcep
+ " \"body\": \"test summary\""
+ "}",
MockRequestAssertion.equals(
"{\"base\":\"other\",\"body\":\"test summary\\n\",\"head\":\""
"{\"base\":\"other\",\"body\":\"test summary\\n\",\"draft\":false,\"head\":\""
+ branchName
+ "\",\"title\":\"test summary\"}")));

Expand Down Expand Up @@ -788,7 +790,7 @@ private void testBranchNameFromUser(String branchNameFromUser, String expectedBr
+ " \"body\": \"test summary\""
+ "}",
MockRequestAssertion.equals(
"{\"base\":\"other\",\"body\":\"test summary\\n\",\"head\":\""
"{\"base\":\"other\",\"body\":\"test summary\\n\",\"draft\":false,\"head\":\""
+ expectedBranchName
+ "\",\"title\":\"test summary\"}")));

Expand Down

0 comments on commit f7d7714

Please sign in to comment.