Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5e59885

Browse filesBrowse files
bitwisemannyatda0116
authored andcommitted
Additional cleanup and code coverage
1 parent 1564259 commit 5e59885
Copy full SHA for 5e59885

File tree

Expand file treeCollapse file tree

4 files changed

+46
-45
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+46
-45
lines changed

‎src/main/java/org/kohsuke/github/GHPullRequest.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHPullRequest.java
+23-21Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -650,34 +650,21 @@ public enum MergeMethod {
650650
* @throws IOException
651651
* the io exception
652652
*/
653-
public void requestEnableAutoMerge(String authorEmail,
653+
public void enablePullRequestAutoMerge(String authorEmail,
654654
String clientMutationId,
655655
String commitBody,
656656
String commitHeadline,
657657
String expectedHeadOid,
658658
MergeMethod mergeMethod) throws IOException {
659659

660660
StringBuilder inputBuilder = new StringBuilder();
661-
inputBuilder.append(" pullRequestId: \"").append(this.getNodeId()).append("\"");
662-
663-
if (authorEmail != null) {
664-
inputBuilder.append(" authorEmail: \"").append(authorEmail).append("\"");
665-
}
666-
if (clientMutationId != null) {
667-
inputBuilder.append(" clientMutationId: \"").append(clientMutationId).append("\"");
668-
}
669-
if (commitBody != null) {
670-
inputBuilder.append(" commitBody: \"").append(commitBody).append("\"");
671-
}
672-
if (commitHeadline != null) {
673-
inputBuilder.append(" commitHeadline: \"").append(commitHeadline).append("\"");
674-
}
675-
if (expectedHeadOid != null) {
676-
inputBuilder.append(" expectedHeadOid: \"").append(expectedHeadOid).append("\"");
677-
}
678-
if (mergeMethod != null) {
679-
inputBuilder.append(" mergeMethod: ").append(mergeMethod);
680-
}
661+
addParameter(inputBuilder, "pullRequestId", this.getNodeId());
662+
addOptionalParameter(inputBuilder, "authorEmail", authorEmail);
663+
addOptionalParameter(inputBuilder, "clientMutationId", clientMutationId);
664+
addOptionalParameter(inputBuilder, "commitBody", commitBody);
665+
addOptionalParameter(inputBuilder, "commitHeadline", commitHeadline);
666+
addOptionalParameter(inputBuilder, "expectedHeadOid", expectedHeadOid);
667+
addOptionalParameter(inputBuilder, "mergeMethod", mergeMethod);
681668

682669
String graphqlBody = "mutation EnableAutoMerge { enablePullRequestAutoMerge(input: {" + inputBuilder + "}) { "
683670
+ "pullRequest { id } } }";
@@ -687,6 +674,21 @@ public void requestEnableAutoMerge(String authorEmail,
687674
refresh();
688675
}
689676

677+
private void addOptionalParameter(StringBuilder inputBuilder, String name, Object value) {
678+
if (value != null) {
679+
addParameter(inputBuilder, name, value);
680+
}
681+
}
682+
683+
private void addParameter(StringBuilder inputBuilder, String name, Object value) {
684+
Objects.requireNonNull(value);
685+
String formatString = " %s: \"%s\"";
686+
if (value instanceof Enum) {
687+
formatString = " %s: %s";
688+
}
689+
690+
inputBuilder.append(String.format(formatString, name, value));
691+
}
690692
/**
691693
* The status of auto merging a {@linkplain GHPullRequest}.
692694
*

‎src/main/java/org/kohsuke/github/internal/graphql/response/GHGraphQLResponse.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/internal/graphql/response/GHGraphQLResponse.java
+19-20Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.annotation.JsonProperty;
55
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
66

7+
import java.util.Collections;
78
import java.util.List;
89
import java.util.stream.Collectors;
910

@@ -31,56 +32,54 @@ public class GHGraphQLResponse<T> {
3132
@JsonCreator
3233
@SuppressFBWarnings(value = { "EI_EXPOSE_REP2" }, justification = "Spotbugs also doesn't like this")
3334
public GHGraphQLResponse(@JsonProperty("data") T data, @JsonProperty("errors") List<GraphQLError> errors) {
35+
if (errors == null) {
36+
errors = Collections.emptyList();
37+
}
3438
this.data = data;
35-
this.errors = errors;
39+
this.errors = Collections.unmodifiableList(errors);
3640
}
3741

3842
/**
39-
* @return request is succeeded
43+
* @return request is succeeded. True when error list is empty.
4044
*/
41-
public Boolean isSuccessful() {
42-
return errors == null || errors.isEmpty();
45+
public boolean isSuccessful() {
46+
return errors.isEmpty();
4347
}
4448

4549
/**
4650
* @return GraphQL success response
4751
*/
4852
public T getData() {
4953
if (!isSuccessful()) {
50-
throw new RuntimeException("This response is Errors occurred response");
54+
throw new RuntimeException("Response not successful, data invalid");
5155
}
5256

5357
return data;
5458
}
5559

5660
/**
57-
* @return GraphQL error messages from Github Response
61+
* @return GraphQL error messages from Github Response. Empty list when no errors occurred.
5862
*/
5963
public List<String> getErrorMessages() {
60-
if (isSuccessful()) {
61-
throw new RuntimeException("No errors occurred");
62-
}
63-
64-
return errors.stream().map(GraphQLError::getErrorMessage).collect(Collectors.toList());
64+
return errors.stream().map(GraphQLError::getMessage).collect(Collectors.toList());
6565
}
6666

6767
/**
6868
* A error of GraphQL response. Minimum implementation for GraphQL error.
6969
*/
70+
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD", "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" },
71+
justification = "JSON API")
7072
private static class GraphQLError {
73+
private String message;
7174

72-
private final String errorMessage;
73-
74-
@JsonCreator
75-
public GraphQLError(@JsonProperty("message") String errorMessage) {
76-
this.errorMessage = errorMessage;
77-
}
78-
79-
public String getErrorMessage() {
80-
return errorMessage;
75+
public String getMessage() {
76+
return message;
8177
}
8278
}
8379

80+
/**
81+
* A GraphQL response with basic Object data type.
82+
*/
8483
public static class ObjectResponse extends GHGraphQLResponse<Object> {
8584
/**
8685
* {@inheritDoc}

‎src/test/java/org/kohsuke/github/GHPullRequestTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/GHPullRequestTest.java
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,7 @@ public void enablePullRequestAutoMerge() throws IOException {
10531053

10541054
GHPullRequest pullRequest = gitHub.getRepository("seate/for-test").getPullRequest(9);
10551055

1056-
pullRequest.requestEnableAutoMerge(authorEmail,
1056+
pullRequest.enablePullRequestAutoMerge(authorEmail,
10571057
clientMutationId,
10581058
commitBody,
10591059
commitTitle,
@@ -1084,12 +1084,12 @@ public void enablePullRequestAutoMergeFailure() throws IOException {
10841084
GHPullRequest pullRequest = gitHub.getRepository("seate/for-test").getPullRequest(9);
10851085

10861086
try {
1087-
pullRequest.requestEnableAutoMerge(authorEmail,
1087+
pullRequest.enablePullRequestAutoMerge(authorEmail,
10881088
clientMutationId,
10891089
commitBody,
10901090
commitTitle,
10911091
expectedCommitHeadOid,
1092-
GHPullRequest.MergeMethod.MERGE);
1092+
null);
10931093
} catch (IOException e) {
10941094
assertThat(e.getMessage(), containsString("does not have a verified email"));
10951095
}

‎src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/enablePullRequestAutoMergeFailure/mappings/5-graphql.json

Copy file name to clipboardExpand all lines: src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/enablePullRequestAutoMergeFailure/mappings/5-graphql.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"bodyPatterns": [
1313
{
14-
"equalToJson": "{\"query\":\"mutation EnableAutoMerge { enablePullRequestAutoMerge(input: { pullRequestId: \\\"PR_kwDON6BPMc6Ox8DC\\\" authorEmail: \\\"failureEmail@gmail.com\\\" clientMutationId: \\\"github-api\\\" commitBody: \\\"This is commit body.\\\" commitHeadline: \\\"This is commit title.\\\" expectedHeadOid: \\\"4888b44d7204dd05680e90159af839c8b1194b6d\\\" mergeMethod: MERGE}) { pullRequest { id } } }\"}",
14+
"equalToJson": "{\"query\":\"mutation EnableAutoMerge { enablePullRequestAutoMerge(input: { pullRequestId: \\\"PR_kwDON6BPMc6Ox8DC\\\" authorEmail: \\\"failureEmail@gmail.com\\\" clientMutationId: \\\"github-api\\\" commitBody: \\\"This is commit body.\\\" commitHeadline: \\\"This is commit title.\\\" expectedHeadOid: \\\"4888b44d7204dd05680e90159af839c8b1194b6d\\\"}) { pullRequest { id } } }\"}",
1515
"ignoreArrayOrder": true,
1616
"ignoreExtraElements": false
1717
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.