|
| 1 | +/* |
| 2 | + * Copyright 2005-2022 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 | + * http://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 | +package org.springframework.ws.support; |
| 17 | + |
| 18 | +import net.minidev.json.JSONArray; |
| 19 | + |
| 20 | +import java.time.Duration; |
| 21 | +import java.time.LocalDate; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import org.springframework.hateoas.IanaLinkRelations; |
| 26 | +import org.springframework.hateoas.Links; |
| 27 | +import org.springframework.http.HttpEntity; |
| 28 | +import org.springframework.http.HttpHeaders; |
| 29 | +import org.springframework.web.reactive.function.client.WebClient; |
| 30 | + |
| 31 | +import com.jayway.jsonpath.JsonPath; |
| 32 | + |
| 33 | +/** |
| 34 | + * Little helper to build a changelog from the tickets of a particular milestone. |
| 35 | + * |
| 36 | + * @author Oliver Gierke |
| 37 | + * @author Greg Turnquist |
| 38 | + */ |
| 39 | +class ChangelogCreator { |
| 40 | + |
| 41 | + private static final int MILESTONE_ID = 91; |
| 42 | + private static final String URI_TEMPLATE = "https://api.github.com/repos/spring-projects/spring-ws/issues?milestone={id}&state=closed"; |
| 43 | + |
| 44 | + public static void main(String... args) { |
| 45 | + |
| 46 | + /* |
| 47 | + * If you run into github rate limiting issues, you can always use a Github Personal Token by adding |
| 48 | + * {@code .header(HttpHeaders.AUTHORIZATION, "token your-github-token")} to the webClient call. |
| 49 | + */ |
| 50 | + |
| 51 | + WebClient webClient = WebClient.create(); |
| 52 | + |
| 53 | + try { |
| 54 | + HttpEntity<String> response = webClient // |
| 55 | + .get().uri(URI_TEMPLATE, MILESTONE_ID) // |
| 56 | + // .header(HttpHeaders.AUTHORIZATION, "token <plugin in a personal token and uncomment>") // |
| 57 | + .exchangeToMono(clientResponse -> clientResponse.toEntity(String.class)) // |
| 58 | + .block(Duration.ofSeconds(10)); |
| 59 | + |
| 60 | + boolean keepChecking = true; |
| 61 | + boolean printHeader = true; |
| 62 | + |
| 63 | + while (keepChecking) { |
| 64 | + |
| 65 | + readPage(response.getBody(), printHeader); |
| 66 | + printHeader = false; |
| 67 | + |
| 68 | + List<String> linksInHeader = response.getHeaders().get(HttpHeaders.LINK); |
| 69 | + Links links = linksInHeader == null ? Links.NONE : Links.parse(linksInHeader.get(0)); |
| 70 | + |
| 71 | + if (links.getLink(IanaLinkRelations.NEXT).isPresent()) { |
| 72 | + |
| 73 | + response = webClient // |
| 74 | + .get().uri(links.getRequiredLink(IanaLinkRelations.NEXT).expand().getHref()) // |
| 75 | + .exchangeToMono(clientResponse -> clientResponse.toEntity(String.class)) // |
| 76 | + .block(Duration.ofSeconds(10)); |
| 77 | + |
| 78 | + } else { |
| 79 | + keepChecking = false; |
| 80 | + } |
| 81 | + } |
| 82 | + } catch (Exception e) { |
| 83 | + System.out.println("Couldn't retrieve MILESTONE " + MILESTONE_ID); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static void readPage(String content, boolean header) { |
| 88 | + |
| 89 | + JsonPath titlePath = JsonPath.compile("$[*].title"); |
| 90 | + JsonPath idPath = JsonPath.compile("$[*].number"); |
| 91 | + |
| 92 | + JSONArray titles = titlePath.read(content); |
| 93 | + Iterator<Object> ids = ((JSONArray) idPath.read(content)).iterator(); |
| 94 | + |
| 95 | + if (header) { |
| 96 | + System.out.println( |
| 97 | + "Changes in version " + JsonPath.read(content, "$[0].milestone.title") + " (" + LocalDate.now() + ")"); |
| 98 | + System.out.println("----------------------------------------"); |
| 99 | + } |
| 100 | + |
| 101 | + for (Object title : titles) { |
| 102 | + |
| 103 | + String format = String.format("- #%s - %s", ids.next(), title.toString().replaceAll("`", "")); |
| 104 | + System.out.println(format.endsWith(".") ? format : format.concat(".")); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments