Skip to content

Commit 6cf65e0

Browse files
committed
Tidy up GraphQL methods and classes
1 parent b99a876 commit 6cf65e0

File tree

6 files changed

+28
-165
lines changed

6 files changed

+28
-165
lines changed

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

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
package org.kohsuke.github;
2525

26-
import com.fasterxml.jackson.annotation.JsonCreator;
27-
import com.fasterxml.jackson.annotation.JsonProperty;
2826
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2927
import org.apache.commons.lang3.StringUtils;
3028

@@ -631,74 +629,6 @@ public enum MergeMethod {
631629
REBASE
632630
}
633631

634-
/**
635-
* Get pull request id for GraphQL
636-
*
637-
* @return The pull request id for GraphQL
638-
* @throws IOException
639-
* the io exception
640-
*/
641-
public String getGraphqlPullRequestId() throws IOException {
642-
if (owner == null) {
643-
throw new IllegalStateException("Repository owner is required to get the pull request ID");
644-
}
645-
String repositoryName = owner.getName();
646-
String ownerName = owner.getOwnerName();
647-
648-
String graphqlBody = String.format(
649-
"query GetPullRequestID { repository(name: \"%s\", owner: \"%s\") { pullRequest(number: %d) { id } } }",
650-
repositoryName,
651-
ownerName,
652-
number);
653-
654-
GetGraphqlPullRequestIdResponse response = root().createGraphQLRequest(graphqlBody)
655-
.fetchGraphQLResponse(GetGraphqlPullRequestIdResponse.class);
656-
657-
return response.getId();
658-
}
659-
660-
/**
661-
* The response from the GraphQL query to get the pull request ID. Minimum required fields are included.
662-
*/
663-
private static class GetGraphqlPullRequestIdResponse {
664-
private final PullRequestOwnerRepository repository;
665-
666-
@JsonCreator
667-
public GetGraphqlPullRequestIdResponse(@JsonProperty("repository") PullRequestOwnerRepository repository) {
668-
this.repository = repository;
669-
}
670-
671-
public String getId() {
672-
return repository.getId();
673-
}
674-
675-
private static class PullRequestOwnerRepository {
676-
private final GraphQLPullRequest pullRequest;
677-
678-
@JsonCreator
679-
public PullRequestOwnerRepository(@JsonProperty("pullRequest") GraphQLPullRequest pullRequest) {
680-
this.pullRequest = pullRequest;
681-
}
682-
683-
public String getId() {
684-
return pullRequest.getId();
685-
}
686-
687-
private static class GraphQLPullRequest {
688-
private final String id;
689-
690-
@JsonCreator
691-
public GraphQLPullRequest(@JsonProperty("id") String id) {
692-
this.id = id;
693-
}
694-
695-
public String getId() {
696-
return id;
697-
}
698-
}
699-
}
700-
}
701-
702632
/**
703633
* Request to enable auto merge for a pull request.
704634
*
@@ -728,7 +658,7 @@ public void requestEnableAutoMerge(String authorEmail,
728658
MergeMethod mergeMethod) throws IOException {
729659

730660
StringBuilder inputBuilder = new StringBuilder();
731-
inputBuilder.append(" pullRequestId: \"").append(getGraphqlPullRequestId()).append("\"");
661+
inputBuilder.append(" pullRequestId: \"").append(this.getNodeId()).append("\"");
732662

733663
if (authorEmail != null) {
734664
inputBuilder.append(" authorEmail: \"").append(authorEmail).append("\"");

src/main/java/org/kohsuke/github/GitHubResponse.java

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.fasterxml.jackson.databind.*;
55
import org.apache.commons.io.IOUtils;
66
import org.kohsuke.github.connector.GitHubConnectorResponse;
7-
import org.kohsuke.github.graphql.response.GHGraphQLResponse;
87

98
import java.io.IOException;
109
import java.io.InputStream;
@@ -136,45 +135,6 @@ static <T> T parseBody(GitHubConnectorResponse connectorResponse, T instance) th
136135
}
137136
}
138137

139-
/**
140-
* Parses a {@link GitHubConnectorResponse} body into a new instance of {@code GHGraphQLResponse<T>}.
141-
*
142-
* @param <T>
143-
* the type
144-
* @param connectorResponse
145-
* the response info to parse.
146-
* @param type
147-
* the type to be constructed in GraphQLResponse.
148-
* @return GHGraphQLResponse
149-
*
150-
* @throws IOException
151-
* if there is an I/O Exception.
152-
*/
153-
@CheckForNull
154-
static <T> GHGraphQLResponse<T> parseGraphQLBody(GitHubConnectorResponse connectorResponse, Class<T> type)
155-
throws IOException {
156-
if (connectorResponse.statusCode() == HTTP_NO_CONTENT) {
157-
if (type != null && type.isArray()) {
158-
T emptyArray = type.cast(Array.newInstance(type.getComponentType(), 0));
159-
return new GHGraphQLResponse<>(emptyArray, null);
160-
} else {
161-
return new GHGraphQLResponse<>(null, null);
162-
}
163-
}
164-
165-
String data = getBodyAsString(connectorResponse);
166-
try {
167-
ObjectReader objectReader = GitHubClient.getMappingObjectReader(connectorResponse);
168-
JavaType targetType = objectReader.getTypeFactory().constructParametricType(GHGraphQLResponse.class, type);
169-
ObjectReader targetReader = objectReader.forType(targetType);
170-
return targetReader.readValue(data);
171-
} catch (JsonMappingException | JsonParseException e) {
172-
String message = "Failed to deserialize: " + data;
173-
LOGGER.log(Level.FINE, message);
174-
throw e;
175-
}
176-
}
177-
178138
/**
179139
* Gets the body of the response as a {@link String}.
180140
*

src/main/java/org/kohsuke/github/Requester.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.apache.commons.io.IOUtils;
2828
import org.kohsuke.github.connector.GitHubConnectorResponse;
2929
import org.kohsuke.github.function.InputStreamFunction;
30-
import org.kohsuke.github.graphql.response.GHGraphQLResponse;
30+
import org.kohsuke.github.internal.graphql.response.GHGraphQLResponse;
3131

3232
import java.io.ByteArrayInputStream;
3333
import java.io.IOException;
@@ -111,7 +111,7 @@ public <T> T fetchInto(@Nonnull T existingInstance) throws IOException {
111111
* the io exception
112112
*/
113113
public void sendGraphQL() throws IOException {
114-
fetchGraphQLResponse(Object.class);
114+
fetchGraphQL(GHGraphQLResponse.ObjectResponse.class);
115115
}
116116

117117
/**
@@ -125,10 +125,8 @@ public void sendGraphQL() throws IOException {
125125
* @throws IOException
126126
* if the server returns 4xx/5xx responses.
127127
*/
128-
public <T> T fetchGraphQLResponse(@Nonnull Class<T> type) throws IOException {
129-
GHGraphQLResponse<T> response = client
130-
.sendRequest(this, connectorResponse -> GitHubResponse.parseGraphQLBody(connectorResponse, type))
131-
.body();
128+
public <T extends GHGraphQLResponse<S>, S> S fetchGraphQL(@Nonnull Class<T> type) throws IOException {
129+
T response = fetch(type);
132130

133131
if (!response.isSuccessful()) {
134132
throw new IOException("GraphQL request failed by:" + response.getErrorMessages());

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.kohsuke.github.graphql.response;
1+
package org.kohsuke.github.internal.graphql.response;
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -20,7 +20,7 @@ public class GHGraphQLResponse<T> {
2020

2121
private final T data;
2222

23-
private final List<GHGraphQLError> errors;
23+
private final List<GraphQLError> errors;
2424

2525
/**
2626
* @param data
@@ -30,7 +30,7 @@ public class GHGraphQLResponse<T> {
3030
*/
3131
@JsonCreator
3232
@SuppressFBWarnings(value = { "EI_EXPOSE_REP2" }, justification = "Spotbugs also doesn't like this")
33-
public GHGraphQLResponse(@JsonProperty("data") T data, @JsonProperty("errors") List<GHGraphQLError> errors) {
33+
public GHGraphQLResponse(@JsonProperty("data") T data, @JsonProperty("errors") List<GraphQLError> errors) {
3434
this.data = data;
3535
this.errors = errors;
3636
}
@@ -61,23 +61,34 @@ public List<String> getErrorMessages() {
6161
throw new RuntimeException("No errors occurred");
6262
}
6363

64-
return errors.stream().map(GHGraphQLError::getErrorMessage).collect(Collectors.toList());
64+
return errors.stream().map(GraphQLError::getErrorMessage).collect(Collectors.toList());
6565
}
6666

6767
/**
6868
* A error of GraphQL response. Minimum implementation for GraphQL error.
6969
*/
70-
private static class GHGraphQLError {
70+
private static class GraphQLError {
7171

7272
private final String errorMessage;
7373

7474
@JsonCreator
75-
public GHGraphQLError(@JsonProperty("message") String errorMessage) {
75+
public GraphQLError(@JsonProperty("message") String errorMessage) {
7676
this.errorMessage = errorMessage;
7777
}
7878

7979
public String getErrorMessage() {
8080
return errorMessage;
8181
}
8282
}
83+
84+
public static class ObjectResponse extends GHGraphQLResponse<Object> {
85+
/**
86+
* {@inheritDoc}
87+
*/
88+
@JsonCreator
89+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP2" }, justification = "Spotbugs also doesn't like this")
90+
public ObjectResponse(@JsonProperty("data") Object data, @JsonProperty("errors") List<GraphQLError> errors) {
91+
super(data, errors);
92+
}
93+
}
8394
}

src/main/resources/META-INF/native-image/org.kohsuke/github-api/reflect-config.json

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6750,7 +6750,7 @@
67506750
"allDeclaredClasses": true
67516751
},
67526752
{
6753-
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse",
6753+
"name": "org.kohsuke.github.internal.graphql.response.GHGraphQLResponse",
67546754
"allPublicFields": true,
67556755
"allDeclaredFields": true,
67566756
"queryAllPublicConstructors": true,
@@ -6765,7 +6765,7 @@
67656765
"allDeclaredClasses": true
67666766
},
67676767
{
6768-
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse$PullRequestOwnerRepository",
6768+
"name": "org.kohsuke.github.internal.graphql.response.GHGraphQLResponse$GraphQLError",
67696769
"allPublicFields": true,
67706770
"allDeclaredFields": true,
67716771
"queryAllPublicConstructors": true,
@@ -6780,37 +6780,7 @@
67806780
"allDeclaredClasses": true
67816781
},
67826782
{
6783-
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse$PullRequestOwnerRepository$GraphQLPullRequest",
6784-
"allPublicFields": true,
6785-
"allDeclaredFields": true,
6786-
"queryAllPublicConstructors": true,
6787-
"queryAllDeclaredConstructors": true,
6788-
"allPublicConstructors": true,
6789-
"allDeclaredConstructors": true,
6790-
"queryAllPublicMethods": true,
6791-
"queryAllDeclaredMethods": true,
6792-
"allPublicMethods": true,
6793-
"allDeclaredMethods": true,
6794-
"allPublicClasses": true,
6795-
"allDeclaredClasses": true
6796-
},
6797-
{
6798-
"name": "org.kohsuke.github.graphql.response.GHGraphQLResponse",
6799-
"allPublicFields": true,
6800-
"allDeclaredFields": true,
6801-
"queryAllPublicConstructors": true,
6802-
"queryAllDeclaredConstructors": true,
6803-
"allPublicConstructors": true,
6804-
"allDeclaredConstructors": true,
6805-
"queryAllPublicMethods": true,
6806-
"queryAllDeclaredMethods": true,
6807-
"allPublicMethods": true,
6808-
"allDeclaredMethods": true,
6809-
"allPublicClasses": true,
6810-
"allDeclaredClasses": true
6811-
},
6812-
{
6813-
"name": "org.kohsuke.github.graphql.response.GHGraphQLResponse$GHGraphQLError",
6783+
"name": "org.kohsuke.github.internal.graphql.response.GHGraphQLResponse$ObjectResponse",
68146784
"allPublicFields": true,
68156785
"allDeclaredFields": true,
68166786
"queryAllPublicConstructors": true,

src/main/resources/META-INF/native-image/org.kohsuke/github-api/serialization-config.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,18 +1353,12 @@
13531353
"name": "org.kohsuke.github.GHPullRequest$EnablePullRequestAutoMergeResponse$EnablePullRequestAutoMerge$EnablePullRequestAutoMergePullRequest"
13541354
},
13551355
{
1356-
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse"
1356+
"name": "org.kohsuke.github.internal.graphql.response.GHGraphQLResponse"
13571357
},
13581358
{
1359-
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse$PullRequestOwnerRepository"
1359+
"name": "org.kohsuke.github.internal.graphql.response.GHGraphQLResponse$GraphQLError"
13601360
},
13611361
{
1362-
"name": "org.kohsuke.github.GHPullRequest$GetGraphqlPullRequestIdResponse$PullRequestOwnerRepository$GraphQLPullRequest"
1363-
},
1364-
{
1365-
"name": "org.kohsuke.github.graphql.response.GHGraphQLResponse"
1366-
},
1367-
{
1368-
"name": "org.kohsuke.github.graphql.response.GHGraphQLResponse$GHGraphQLError"
1362+
"name": "org.kohsuke.github.internal.graphql.response.GHGraphQLResponse$ObjectResponse"
13691363
}
13701364
]

0 commit comments

Comments
 (0)