|
| 1 | +/* |
| 2 | + * Copyright 2019-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.gradle.sagan; |
| 18 | + |
| 19 | +import com.google.gson.Gson; |
| 20 | +import okhttp3.*; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Base64; |
| 24 | + |
| 25 | +/** |
| 26 | + * Implements necessary calls to the Sagan API See https://github.com/spring-io/sagan/blob/master/sagan-site/src/docs/asciidoc/index.adoc |
| 27 | + */ |
| 28 | +public class SaganApi { |
| 29 | + private String baseUrl = "https://spring.io/api"; |
| 30 | + |
| 31 | + private OkHttpClient client; |
| 32 | + private Gson gson = new Gson(); |
| 33 | + |
| 34 | + public SaganApi(String gitHubToken) { |
| 35 | + this.client = new OkHttpClient.Builder() |
| 36 | + .addInterceptor(new BasicInterceptor("not-used", gitHubToken)) |
| 37 | + .build(); |
| 38 | + } |
| 39 | + |
| 40 | + public void setBaseUrl(String baseUrl) { |
| 41 | + this.baseUrl = baseUrl; |
| 42 | + } |
| 43 | + |
| 44 | + public void createReleaseForProject(Release release, String projectName) { |
| 45 | + String url = this.baseUrl + "/projects/" + projectName + "/releases"; |
| 46 | + String releaseJsonString = gson.toJson(release); |
| 47 | + RequestBody body = RequestBody.create(MediaType.parse("application/json"), releaseJsonString); |
| 48 | + Request request = new Request.Builder() |
| 49 | + .url(url) |
| 50 | + .post(body) |
| 51 | + .build(); |
| 52 | + try { |
| 53 | + Response response = this.client.newCall(request).execute(); |
| 54 | + if (!response.isSuccessful()) { |
| 55 | + throw new RuntimeException("Could not create release " + release + ". Got response " + response); |
| 56 | + } |
| 57 | + } catch (IOException fail) { |
| 58 | + throw new RuntimeException("Could not create release " + release, fail); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public void deleteReleaseForProject(String release, String projectName) { |
| 63 | + String url = this.baseUrl + "/projects/" + projectName + "/releases/" + release; |
| 64 | + Request request = new Request.Builder() |
| 65 | + .url(url) |
| 66 | + .delete() |
| 67 | + .build(); |
| 68 | + try { |
| 69 | + Response response = this.client.newCall(request).execute(); |
| 70 | + if (!response.isSuccessful()) { |
| 71 | + throw new RuntimeException("Could not delete release " + release + ". Got response " + response); |
| 72 | + } |
| 73 | + } catch (IOException fail) { |
| 74 | + throw new RuntimeException("Could not delete release " + release, fail); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static class BasicInterceptor implements Interceptor { |
| 79 | + |
| 80 | + private final String token; |
| 81 | + |
| 82 | + public BasicInterceptor(String username, String token) { |
| 83 | + this.token = Base64.getEncoder().encodeToString((username + ":" + token).getBytes()); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public okhttp3.Response intercept(Chain chain) throws IOException { |
| 88 | + Request request = chain.request().newBuilder() |
| 89 | + .addHeader("Authorization", "Basic " + this.token).build(); |
| 90 | + return chain.proceed(request); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments