|
| 1 | +package org.springframework.security.convention.versions; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.time.Duration; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Optional; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +import com.apollographql.apollo.ApolloCall; |
| 10 | +import com.apollographql.apollo.ApolloClient; |
| 11 | +import com.apollographql.apollo.api.Input; |
| 12 | +import com.apollographql.apollo.api.Response; |
| 13 | +import com.apollographql.apollo.exception.ApolloException; |
| 14 | +import okhttp3.Interceptor; |
| 15 | +import okhttp3.OkHttpClient; |
| 16 | +import okhttp3.Request; |
| 17 | +import org.jetbrains.annotations.NotNull; |
| 18 | +import reactor.core.publisher.Mono; |
| 19 | +import reactor.util.retry.RetrySpec; |
| 20 | + |
| 21 | +public class GitHubApi { |
| 22 | + |
| 23 | + private final ApolloClient apolloClient; |
| 24 | + |
| 25 | + public GitHubApi(String githubToken) { |
| 26 | + if (githubToken == null) { |
| 27 | + throw new IllegalArgumentException("githubToken is required. You can set it using -PgitHubAccessToken="); |
| 28 | + } |
| 29 | + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); |
| 30 | + clientBuilder.addInterceptor(new AuthorizationInterceptor(githubToken)); |
| 31 | + this.apolloClient = ApolloClient.builder() |
| 32 | + .serverUrl("https://api.github.com/graphql") |
| 33 | + .okHttpClient(clientBuilder.build()) |
| 34 | + .build(); |
| 35 | + } |
| 36 | + |
| 37 | + public Mono<FindCreateIssueResult> findCreateIssueInput(String owner, String name, String milestone) { |
| 38 | + String label = "\"type: dependency-upgrade\""; |
| 39 | + FindCreateIssueInputQuery findCreateIssueInputQuery = new FindCreateIssueInputQuery(owner, name, Input.optional(label), Input.optional(milestone)); |
| 40 | + return Mono.create( sink -> this.apolloClient.query(findCreateIssueInputQuery) |
| 41 | + .enqueue(new ApolloCall.Callback<>() { |
| 42 | + @Override |
| 43 | + public void onResponse(@NotNull Response<FindCreateIssueInputQuery.Data> response) { |
| 44 | + if (response.hasErrors()) { |
| 45 | + sink.error(new RuntimeException(response.getErrors().stream().map(e -> e.getMessage()).collect(Collectors.joining(" ")))); |
| 46 | + } else { |
| 47 | + FindCreateIssueInputQuery.Data data = response.getData(); |
| 48 | + FindCreateIssueInputQuery.Repository repository = data.repository(); |
| 49 | + List<String> labels = repository.labels().nodes().stream().map(FindCreateIssueInputQuery.Node::id).collect(Collectors.toList()); |
| 50 | + if (labels.isEmpty()) { |
| 51 | + sink.error(new IllegalArgumentException("Could not find label for " + label)); |
| 52 | + return; |
| 53 | + } |
| 54 | + Optional<String> firstMilestoneId = repository.milestones().nodes().stream().map(FindCreateIssueInputQuery.Node1::id).findFirst(); |
| 55 | + if (!firstMilestoneId.isPresent()) { |
| 56 | + sink.error(new IllegalArgumentException("Could not find OPEN milestone id for " + milestone)); |
| 57 | + return; |
| 58 | + } |
| 59 | + String milestoneId = firstMilestoneId.get(); |
| 60 | + String repositoryId = repository.id(); |
| 61 | + String assigneeId = data.viewer().id(); |
| 62 | + sink.success(new FindCreateIssueResult(repositoryId, labels, milestoneId, assigneeId)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onFailure(@NotNull ApolloException e) { |
| 68 | + sink.error(e); |
| 69 | + } |
| 70 | + })); |
| 71 | + } |
| 72 | + |
| 73 | + public static class FindCreateIssueResult { |
| 74 | + private final String repositoryId; |
| 75 | + private final List<String> labelIds; |
| 76 | + private final String milestoneId; |
| 77 | + private final String assigneeId; |
| 78 | + |
| 79 | + public FindCreateIssueResult(String repositoryId, List<String> labelIds, String milestoneId, String assigneeId) { |
| 80 | + this.repositoryId = repositoryId; |
| 81 | + this.labelIds = labelIds; |
| 82 | + this.milestoneId = milestoneId; |
| 83 | + this.assigneeId = assigneeId; |
| 84 | + } |
| 85 | + |
| 86 | + public String getRepositoryId() { |
| 87 | + return repositoryId; |
| 88 | + } |
| 89 | + |
| 90 | + public List<String> getLabelIds() { |
| 91 | + return labelIds; |
| 92 | + } |
| 93 | + |
| 94 | + public String getMilestoneId() { |
| 95 | + return milestoneId; |
| 96 | + } |
| 97 | + |
| 98 | + public String getAssigneeId() { |
| 99 | + return assigneeId; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public Mono<RateLimitQuery.RateLimit> findRateLimit() { |
| 104 | + return Mono.create( sink -> this.apolloClient.query(new RateLimitQuery()) |
| 105 | + .enqueue(new ApolloCall.Callback<>() { |
| 106 | + @Override |
| 107 | + public void onResponse(@NotNull Response<RateLimitQuery.Data> response) { |
| 108 | + if (response.hasErrors()) { |
| 109 | + sink.error(new RuntimeException(response.getErrors().stream().map(e -> e.getMessage()).collect(Collectors.joining(" ")))); |
| 110 | + } else { |
| 111 | + sink.success(response.getData().rateLimit()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onFailure(@NotNull ApolloException e) { |
| 117 | + sink.error(e); |
| 118 | + } |
| 119 | + })); |
| 120 | + } |
| 121 | + |
| 122 | + public Mono<Integer> createIssue(String repositoryId, String title, List<String> labelIds, String milestoneId, String assigneeId) { |
| 123 | + CreateIssueInputMutation createIssue = new CreateIssueInputMutation.Builder() |
| 124 | + .repositoryId(repositoryId) |
| 125 | + .title(title) |
| 126 | + .labelIds(labelIds) |
| 127 | + .milestoneId(milestoneId) |
| 128 | + .assigneeId(assigneeId) |
| 129 | + .build(); |
| 130 | + return Mono.create( sink -> this.apolloClient.mutate(createIssue) |
| 131 | + .enqueue(new ApolloCall.Callback<>() { |
| 132 | + @Override |
| 133 | + public void onResponse(@NotNull Response<CreateIssueInputMutation.Data> response) { |
| 134 | + if (response.hasErrors()) { |
| 135 | + String message = response.getErrors().stream().map(e -> e.getMessage() + " " + e.getCustomAttributes() + " " + e.getLocations()).collect(Collectors.joining(" ")); |
| 136 | + if (message.contains("was submitted too quickly")) { |
| 137 | + sink.error(new SubmittedTooQuick(message)); |
| 138 | + } else { |
| 139 | + sink.error(new RuntimeException(message)); |
| 140 | + } |
| 141 | + } else { |
| 142 | + sink.success(response.getData().createIssue().issue().number()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void onFailure(@NotNull ApolloException e) { |
| 148 | + sink.error(e); |
| 149 | + } |
| 150 | + })) |
| 151 | + .retryWhen( |
| 152 | + RetrySpec.fixedDelay(3, Duration.ofMinutes(1)) |
| 153 | + .filter(SubmittedTooQuick.class::isInstance) |
| 154 | + .doBeforeRetry(r -> System.out.println("Pausing for 1 minute and then retrying due to receiving \"submitted too quickly\" error from GitHub API")) |
| 155 | + ) |
| 156 | + .cast(Integer.class); |
| 157 | + } |
| 158 | + |
| 159 | + public static class SubmittedTooQuick extends RuntimeException { |
| 160 | + public SubmittedTooQuick(String message) { |
| 161 | + super(message); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static class AuthorizationInterceptor implements Interceptor { |
| 166 | + |
| 167 | + private final String token; |
| 168 | + |
| 169 | + public AuthorizationInterceptor(String token) { |
| 170 | + this.token = token; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public okhttp3.Response intercept(Chain chain) throws IOException { |
| 175 | + Request request = chain.request().newBuilder() |
| 176 | + .addHeader("Authorization", "Bearer " + this.token).build(); |
| 177 | + return chain.proceed(request); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments