Skip to content

Commit

Permalink
Use apollo 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Dec 15, 2024
1 parent 418ab33 commit 8e2f4d2
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 112 deletions.
18 changes: 12 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'java'
id 'application'
id 'com.apollographql.apollo' version '2.5.14'
id 'com.apollographql.apollo' version "$apollo"
id 'com.github.johnrengelman.shadow' version '7.+'
}

Expand Down Expand Up @@ -40,16 +40,22 @@ dependencies {

implementation 'com.mojang:brigadier:1.0.18'

implementation 'com.apollographql.apollo:apollo-runtime:2.5.14' // Apollo (GraphQL)
implementation 'com.apollographql.apollo:apollo-rx3-support:2.5.14' // Apollo support for RxJava3
implementation "com.apollographql.apollo:apollo-api:$apollo" // Apollo (GraphQL)

implementation 'org.eclipse.jgit:org.eclipse.jgit:6.10.0.202406032230-r'
}

apollo {
customTypeMapping = [
'URI' : 'java.net.URI'
]
service('service') {
packageName = 'com.github.api'
generateKotlinModels = false
generatePrimitiveTypes = true
languageVersion = '17'
addTypename = 'ifPolymorphic'
classesForEnumsMatching.set([])
mapScalar('URI', 'java.net.URI')
}
generateSourcesDuringGradleSync.set(true)
}

shadowJar {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apollo=4.1.0
8 changes: 8 additions & 0 deletions src/main/java/net/neoforged/automation/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.neoforged.automation;

import com.github.api.GetPullRequestQuery;
import com.mojang.brigadier.CommandDispatcher;
import io.javalin.Javalin;
import net.neoforged.automation.command.Commands;
Expand Down Expand Up @@ -51,6 +52,13 @@ public static void main(String[] args) throws IOException, NoSuchAlgorithmExcept
.start(startupConfig.getInt("port", 8080));

LOGGER.warn("Started up! Logged as {} on GitHub", GitHubAccessor.getApp(gitHub).getSlug());

System.out.println(GitHubAccessor.graphQl(gitHub, GetPullRequestQuery
.builder()
.owner("neoforged")
.name("neoforge")
.number(1771)
.build()));
}

public static WebhookHandler setupWebhookHandlers(StartupConfiguration startupConfig, WebhookHandler handler, Configuration.RepoLocation location) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
Expand Down
47 changes: 0 additions & 47 deletions src/main/java/net/neoforged/automation/util/ApolloReader.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ private Set<String> getClosingIssueLabels(GitHub gitHub, GHPullRequest pr) throw
.owner(pr.getRepository().getOwnerName())
.number(pr.getNumber())
.build())
.repository()
.pullRequest()
.fragments()
.pullRequestInfo()
.closingIssuesReferences()
.nodes()) {
var issue = issueNode.fragments().issueInfo();
issue.labels().nodes().forEach(n -> labels.add(n.name().toLowerCase(Locale.ROOT)));
.repository
.pullRequest
.pullRequestInfo
.closingIssuesReferences
.nodes) {
var issue = issueNode.issueInfo;
issue.labels.nodes.forEach(n -> labels.add(n.name.toLowerCase(Locale.ROOT)));
}
return labels;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public static void checkPR(GitHub gitHub, GHPullRequest pullRequest) throws Exce
.name(pullRequest.getRepository().getName())
.number(pullRequest.getNumber())
.build())
.repository()
.pullRequest().fragments().pullRequestInfo()) == MergeableState.UNKNOWN) {
.repository
.pullRequest.pullRequestInfo) == MergeableState.UNKNOWN) {
Thread.sleep(PR_BASE_TIME * 1000);
}
}
Expand All @@ -70,10 +70,10 @@ public static void checkPRConflicts(GitHub gitHub, GHRepository repository, Stri
.name(repository.getName())
.baseRef(branchName)
.states(List.of(PullRequestState.OPEN))
.build()).repository().pullRequests()
.nodes();
.build()).repository.pullRequests
.nodes;

final long unknownAmount = prs.stream().filter(it -> it.fragments().pullRequestInfo().mergeable() == MergeableState.UNKNOWN).count();
final long unknownAmount = prs.stream().filter(it -> it.pullRequestInfo.mergeable == MergeableState.UNKNOWN).count();
if (unknownAmount > 0) {
// If we don't know the status of one or more PRs, give GitHub some time to think.
SERVICE.schedule(() -> {
Expand All @@ -87,26 +87,26 @@ public static void checkPRConflicts(GitHub gitHub, GHRepository repository, Stri
}

for (final var node : prs) {
checkConflict(gitHub, node.fragments().pullRequestInfo());
checkConflict(gitHub, node.pullRequestInfo);
}

IN_PROGRESS_REPOS.remove(repository.getFullName());
}

public static MergeableState checkConflict(GitHub gitHub, PullRequestInfo info) throws IOException {
final boolean hasLabel = info.labels().nodes().stream().anyMatch(node -> node.name().equalsIgnoreCase(Label.NEEDS_REBASE.getLabelName()));
final MergeableState state = info.mergeable();
final boolean hasLabel = info.labels.nodes.stream().anyMatch(node -> node.name.equalsIgnoreCase(Label.NEEDS_REBASE.getLabelName()));
final MergeableState state = info.mergeable;
if (hasLabel && state == MergeableState.CONFLICTING) return state; // We have conflicts and the PR has the label already

final int number = info.number();
final GHRepository repo = gitHub.getRepository(info.repository().nameWithOwner());
final int number = info.number;
final GHRepository repo = gitHub.getRepository(info.repository.nameWithOwner);
final GHPullRequest pr = repo.getPullRequest(number);

// If the PR is mergeable but behind and it has a keep-rebased label, we shall rebase it
if (state == MergeableState.MERGEABLE && info.viewerCanUpdateBranch()) {
if (state == MergeableState.MERGEABLE && info.viewerCanUpdateBranch) {
var conf = Configuration.get(repo);
for (PullRequestInfo.Node node : info.labels().nodes()) {
if (conf.getLabelOfType(node.name(), KeepRebasedHandler.class) != null) {
for (PullRequestInfo.Node node : info.labels.nodes) {
if (conf.getLabelOfType(node.name, KeepRebasedHandler.class) != null) {
pr.updateBranch();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ public void handle(GitHub gitHub, GHEventPayload.Status payload) throws Exceptio
.owner(payload.getRepository().getOwnerName())
.number(pr.getNumber())
.build())
.repository()
.pullRequest()
.fragments()
.pullRequestInfo()
.closingIssuesReferences()
.nodes()) {
repo.getIssue(issueNode.fragments().issueInfo().number())
.repository
.pullRequest
.pullRequestInfo
.closingIssuesReferences
.nodes) {
repo.getIssue(issueNode.issueInfo.number)
.comment(baseIssueComment);
closedIssues.add(issueNode.fragments().issueInfo().number());
closedIssues.add(issueNode.issueInfo.number);
}
}

Expand Down
56 changes: 26 additions & 30 deletions src/main/java/org/kohsuke/github/GitHubAccessor.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package org.kohsuke.github;

import com.apollographql.apollo.ApolloClient;
import com.apollographql.apollo.api.CustomTypeAdapter;
import com.apollographql.apollo.api.CustomTypeValue;
import com.apollographql.apollo.api.Error;
import com.apollographql.apollo.api.Adapter;
import com.apollographql.apollo.api.CustomScalarAdapters;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.Response;
import com.apollographql.apollo.exception.ApolloException;
import com.apollographql.apollo.api.Operations;
import com.apollographql.apollo.api.json.BufferedSinkJsonWriter;
import com.apollographql.apollo.api.json.BufferedSourceJsonReader;
import com.apollographql.apollo.api.json.JsonReader;
import com.apollographql.apollo.api.json.JsonWriter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectReader;
import net.neoforged.automation.util.ApolloReader;
import net.neoforged.automation.util.AuthUtil;
import okio.Buffer;
import okio.Okio;
import org.apache.commons.io.input.ReaderInputStream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -20,31 +22,26 @@
import java.io.StringReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class GitHubAccessor {

public static final ApolloClient CLIENT = ApolloClient.builder()
.serverUrl("https://api.github.com/graphql")
.callFactory(request -> null)
.addCustomTypeAdapter(com.github.api.type.CustomType.URI, new CustomTypeAdapter<URI>() {

public static final CustomScalarAdapters ADAPTERS = new CustomScalarAdapters.Builder()
.add(com.github.api.type.URI.type, new Adapter<URI>() {
@Override
public CustomTypeValue<?> encode(URI uri) {
return new CustomTypeValue.GraphQLString(uri.toString());
public void toJson(@NotNull JsonWriter jsonWriter, @NotNull CustomScalarAdapters customScalarAdapters, URI uri) throws IOException {
jsonWriter.value(uri.toString());
}

@Override
public URI decode(CustomTypeValue<?> customTypeValue) {
return URI.create(customTypeValue.value.toString());
public URI fromJson(@NotNull JsonReader jsonReader, @NotNull CustomScalarAdapters customScalarAdapters) throws IOException {
return URI.create(jsonReader.nextString());
}
}).build();
public static final ApolloReader READER = ApolloReader.ofClient(CLIENT);
})
.build();

public static ObjectReader objectReader(GitHub gitHub) {
return GitHubClient.getMappingObjectReader(gitHub);
Expand All @@ -69,16 +66,15 @@ public static <T> T graphQl(GitHub gitHub, String query, InputStreamFunction<T>
}

@NotNull
@SuppressWarnings("unchecked")
public static <T> T graphQl(GitHub gitHub, Operation<?, T, ?> call) throws IOException {
final Response<T> response = graphQl(gitHub, call.composeRequestBody().utf8(), in -> (Response<T>) READER.read(call, in));
final T res = response.getData();
if (res == null) {
throw new ApolloException(Objects.<List<Error>>requireNonNullElse(response.getErrors(), List.of()).stream()
.map(Error::toString).collect(Collectors.joining("; ")));
} else {
return res;
}
public static <T extends Operation.Data> T graphQl(GitHub gitHub, Operation<T> call) throws IOException {
var buf = new Buffer();
var writer = new BufferedSinkJsonWriter(buf);
Operations.composeJsonRequest(call, writer, ADAPTERS);
var query = buf.readUtf8();
return graphQl(gitHub, query, in -> {
var reader = new BufferedSourceJsonReader(Okio.buffer(Okio.source(in)));
return Operations.parseResponse(call, reader, null, ADAPTERS).data;
});
}

public static void lock(GHIssue issue, @Nullable LockReason reason) throws IOException {
Expand Down

0 comments on commit 8e2f4d2

Please sign in to comment.