use of org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider in project component-runtime by Talend.
the class Github method load.
public Collection<Contributor> load() {
final String token = "Basic " + Base64.getEncoder().encodeToString((user + ':' + password).getBytes(StandardCharsets.UTF_8));
final Client client = ClientBuilder.newClient().register(new JsonbJaxrsProvider<>());
final WebTarget gravatarBase = client.target(Gravatars.GRAVATAR_BASE);
final ForkJoinPool pool = new ForkJoinPool(Math.max(4, Runtime.getRuntime().availableProcessors() * 8));
try {
return pool.submit(() -> contributors(client, token, "https://api.github.com/repos/talend/component-runtime/contributors").parallel().collect(toMap(e -> normalizeLogin(e.login), identity(), (c1, c2) -> {
c1.contributions += c2.contributions;
return c1;
})).values().stream().map(contributor -> {
if (contributor.url == null) {
try {
final Contributor gravatar = Gravatars.loadGravatar(gravatarBase, contributor.email);
return Contributor.builder().name(contributor.name).commits(contributor.contributions).description(gravatar.getDescription()).gravatar(gravatar.getGravatar()).build();
} catch (final Exception e) {
log.warn(e.getMessage(), e);
return new Contributor(contributor.email, contributor.email, "", Gravatars.url(contributor.email), contributor.contributions);
}
}
final GithubUser user = client.target(contributor.url).request(APPLICATION_JSON_TYPE).header("Authorization", token).get(GithubUser.class);
return Contributor.builder().id(contributor.login).name(ofNullable(user.name).orElse(contributor.name)).description((user.bio == null ? "" : user.bio) + (user.blog != null && !user.blog.trim().isEmpty() && (user.bio == null || !user.bio.contains(user.blog)) ? "\n\nBlog: " + user.blog : "")).commits(contributor.contributions).gravatar(ofNullable(contributor.avatarUrl).orElseGet(() -> {
final String gravatarId = contributor.gravatarId == null || contributor.gravatarId.isEmpty() ? contributor.email : contributor.gravatarId;
try {
final Contributor gravatar = Gravatars.loadGravatar(gravatarBase, gravatarId);
return gravatar.getGravatar();
} catch (final Exception e) {
log.warn(e.getMessage(), e);
return Gravatars.url(gravatarId);
}
})).build();
}).filter(Objects::nonNull).sorted(comparing(Contributor::getCommits).reversed()).collect(toList())).get(15, MINUTES);
} catch (final ExecutionException ee) {
if (WebApplicationException.class.isInstance(ee.getCause())) {
final Response response = WebApplicationException.class.cast(ee.getCause()).getResponse();
if (response != null && response.getEntity() != null) {
log.error(response.readEntity(String.class));
}
}
throw new IllegalStateException(ee.getCause());
} catch (final InterruptedException | TimeoutException e) {
throw new IllegalStateException(e);
} finally {
client.close();
pool.shutdownNow();
}
}
use of org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider in project meecrowave by apache.
the class MeecrowaveClientLifecycleListenerTest method autoClose.
@Test
public void autoClose() throws NoSuchFieldException, IllegalAccessException {
try (final Meecrowave meecrowave = new Meecrowave(new Meecrowave.Builder().randomHttpPort().includePackages(MeecrowaveClientLifecycleListenerTest.class.getName())).bake()) {
final Field jsonbs = JohnzonCdiExtension.class.getDeclaredField("jsonbs");
jsonbs.setAccessible(true);
final BeanManager beanManager = CDI.current().getBeanManager();
final JohnzonCdiExtension extensionInstance = JohnzonCdiExtension.class.cast(beanManager.getContext(ApplicationScoped.class).get(beanManager.resolve(beanManager.getBeans(JohnzonCdiExtension.class))));
final Collection<?> o = Collection.class.cast(jsonbs.get(extensionInstance));
{
// ensure server is init whatever test suite we run in
final Client client = ClientBuilder.newClient();
get(meecrowave, client);
client.close();
}
final int origin = o.size();
final Client client = ClientBuilder.newClient();
final JsonbJaxrsProvider<?> provider = new JsonbJaxrsProvider<>();
client.register(provider);
get(meecrowave, client);
assertEquals(origin + 1, o.size());
client.close();
assertEquals(origin, o.size());
}
}
use of org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider in project component-runtime by Talend.
the class Generator method generatedJira.
private static void generatedJira(final File generatedDir, final String username, final String password, final String version) {
if (username == null || username.trim().isEmpty() || "skip".equals(username)) {
log.error("No JIRA credentials, will skip changelog generation");
return;
}
final String project = "TCOMP";
final String jiraBase = "https://jira.talendforge.org";
final File file = new File(generatedDir, "generated_changelog.adoc");
final Client client = ClientBuilder.newClient().register(new JsonbJaxrsProvider<>());
final String auth = "Basic " + Base64.getEncoder().encodeToString((username + ':' + password).getBytes(StandardCharsets.UTF_8));
try {
final WebTarget restApi = client.target(jiraBase + "/rest/api/2").property("http.connection.timeout", 60000L);
final List<JiraVersion> versions = restApi.path("project/{project}/versions").resolveTemplate("project", project).request(APPLICATION_JSON_TYPE).header("Authorization", auth).get(new GenericType<List<JiraVersion>>() {
});
final String currentVersion = version.replace("-SNAPSHOT", "");
final List<JiraVersion> loggedVersions = versions.stream().filter(v -> (v.isReleased() || jiraVersionMatches(currentVersion, v.getName()))).collect(toList());
if (loggedVersions.isEmpty()) {
try (final PrintStream stream = new PrintStream(new WriteIfDifferentStream(file))) {
stream.println("No version found.");
}
return;
}
final String jql = "project=" + project + " AND labels=\"changelog\"" + loggedVersions.stream().map(v -> "fixVersion=" + v.getName()).collect(joining(" OR ", " AND (", ")"));
final Function<Long, JiraIssues> searchFrom = startAt -> restApi.path("search").queryParam("jql", jql).queryParam("startAt", startAt).request(APPLICATION_JSON_TYPE).header("Authorization", auth).get(JiraIssues.class);
final Function<JiraIssues, Stream<JiraIssues>> paginate = new Function<JiraIssues, Stream<JiraIssues>>() {
@Override
public Stream<JiraIssues> apply(final JiraIssues issues) {
final long nextStartAt = issues.getStartAt() + issues.getMaxResults();
final Stream<JiraIssues> fetched = Stream.of(issues);
return issues.getTotal() > nextStartAt ? Stream.concat(fetched, apply(searchFrom.apply(nextStartAt))) : fetched;
}
};
final Set<String> includeStatus = Stream.of("closed", "resolved", "development done", "qa done", "done").collect(toSet());
final Map<String, TreeMap<String, List<JiraIssue>>> issues = Stream.of(searchFrom.apply(0L)).flatMap(paginate).flatMap(i -> ofNullable(i.getIssues()).map(Collection::stream).orElseGet(Stream::empty)).filter(issue -> includeStatus.contains(issue.getFields().getStatus().getName().toLowerCase(ENGLISH))).flatMap(i -> i.getFields().getFixVersions().stream().map(v -> Pair.of(v, i))).collect(groupingBy(pair -> pair.getKey().getName(), () -> new TreeMap<>(versionComparator()), groupingBy(pair -> pair.getValue().getFields().getIssuetype().getName(), TreeMap::new, collectingAndThen(mapping(Pair::getValue, toList()), l -> {
l.sort(comparing(JiraIssue::getKey));
return l;
}))));
final String changelog = issues.entrySet().stream().map(versionnedIssues -> new StringBuilder("\n\n== Version ").append(versionnedIssues.getKey()).append(versionnedIssues.getValue().entrySet().stream().collect((Supplier<StringBuilder>) StringBuilder::new, (builder, issuesByType) -> builder.append("\n\n=== ").append(issuesByType.getKey()).append("\n\n").append(issuesByType.getValue().stream().collect((Supplier<StringBuilder>) StringBuilder::new, // useful
(a, i) -> a.append("- link:").append(jiraBase).append("/browse/").append(i.getKey()).append("[").append(i.getKey()).append("^]").append(": ").append(i.getFields().getSummary()).append("\n"), StringBuilder::append)), StringBuilder::append))).collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString();
try (final PrintStream stream = new PrintStream(new WriteIfDifferentStream(file))) {
stream.println(changelog);
}
} finally {
client.close();
}
}
Aggregations