Skip to content

Commit

Permalink
feat(announce): Add an HTTP announcer. Resolves #921
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Oct 27, 2022
1 parent a5012d3 commit 08d13b8
Show file tree
Hide file tree
Showing 16 changed files with 926 additions and 6 deletions.
Expand Up @@ -83,6 +83,7 @@ protected void execute() {
mappings.put("Map<String, HttpDownloader>", "HttpDownloaderMap");
mappings.put("Map<String, SftpDownloader>", "SftpDownloaderMap");
mappings.put("Map<String, ScpDownloader>", "ScpDownloaderMap");
mappings.put("Map<String, HttpAnnouncer>", "HttpAnnouncerMap");

try {
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON);
Expand Down
Expand Up @@ -42,6 +42,7 @@ public class Announce extends AbstractModelObject<Announce> implements Domain, A
private final Discussions discussions = new Discussions();
private final Gitter gitter = new Gitter();
private final GoogleChat googleChat = new GoogleChat();
private final HttpAnnouncers http = new HttpAnnouncers();
private final Mail mail = new Mail();
private final Mastodon mastodon = new Mastodon();
private final Mattermost mattermost = new Mattermost();
Expand All @@ -65,6 +66,7 @@ public void freeze() {
discussions.freeze();
gitter.freeze();
googleChat.freeze();
http.freeze();
mail.freeze();
mastodon.freeze();
mattermost.freeze();
Expand All @@ -87,6 +89,7 @@ public void merge(Announce announce) {
setDiscussions(announce.discussions);
setGitter(announce.gitter);
setGoogleChat(announce.googleChat);
setConfiguredHttp(announce.http);
setMail(announce.mail);
setMastodon(announce.mastodon);
setMattermost(announce.mattermost);
Expand Down Expand Up @@ -251,6 +254,26 @@ public void setTwitter(Twitter twitter) {
this.twitter.merge(twitter);
}

public HttpAnnouncers getConfiguredHttp() {
return this.http;
}

void setConfiguredHttp(HttpAnnouncers https) {
this.http.merge(https);
}

public Map<String, HttpAnnouncer> getHttp() {
return this.http.getHttpAnnouncers();
}

public void setHttp(Map<String, HttpAnnouncer> https) {
this.http.setHttpAnnouncers(https);
}

public void addHttpAnnouncer(HttpAnnouncer http) {
this.http.addHttpAnnouncer(http);
}

public Webhooks getConfiguredWebhooks() {
return this.webhooks;
}
Expand Down Expand Up @@ -289,6 +312,7 @@ public Map<String, Object> asMap(boolean full) {
map.putAll(discussions.asMap(full));
map.putAll(gitter.asMap(full));
map.putAll(googleChat.asMap(full));
map.putAll(http.asMap(full));
map.putAll(mail.asMap(full));
map.putAll(mastodon.asMap(full));
map.putAll(mattermost.asMap(full));
Expand Down Expand Up @@ -330,6 +354,8 @@ private <A extends Announcer> A resolveAnnouncer(String name) {
return (A) getGitter();
case GoogleChat.NAME:
return (A) getGoogleChat();
case HttpAnnouncers.NAME:
return (A) getConfiguredHttp();
case Mail.NAME:
return (A) getMail();
case Mastodon.NAME:
Expand Down Expand Up @@ -362,6 +388,7 @@ public static Set<String> supportedAnnouncers() {
set.add(Discussions.NAME);
set.add(Gitter.NAME);
set.add(GoogleChat.NAME);
set.add(HttpAnnouncers.NAME);
set.add(Mail.NAME);
set.add(Mastodon.NAME);
set.add(Mattermost.NAME);
Expand Down
@@ -0,0 +1,218 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2022 The JReleaser authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jreleaser.model;

import org.jreleaser.bundle.RB;
import org.jreleaser.util.Env;
import org.jreleaser.util.JReleaserException;

import java.io.IOException;
import java.io.Reader;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.jreleaser.util.Constants.HIDE;
import static org.jreleaser.util.Constants.KEY_TAG_NAME;
import static org.jreleaser.util.Constants.UNSET;
import static org.jreleaser.util.MustacheUtils.applyTemplate;
import static org.jreleaser.util.MustacheUtils.applyTemplates;
import static org.jreleaser.util.StringUtils.isNotBlank;
import static org.jreleaser.util.Templates.resolveTemplate;

/**
* @author Andres Almiray
* @since 1.3.0
*/
public class HttpAnnouncer extends AbstractAnnouncer<HttpAnnouncer> implements Http {
private final Map<String, String> headers = new LinkedHashMap<>();
private String url;
private String username;
private String password;
private Authorization authorization;
private Method method;
private String payload;
private String payloadTemplate;

public HttpAnnouncer() {
super("");
}

@Override
public void merge(HttpAnnouncer http) {
freezeCheck();
super.merge(http);
this.url = merge(this.url, http.url);
this.username = merge(this.username, http.username);
this.password = merge(this.password, http.password);
this.authorization = merge(this.authorization, http.authorization);
this.method = merge(this.method, http.method);
this.payload = merge(this.payload, http.payload);
this.payloadTemplate = merge(this.payloadTemplate, http.payloadTemplate);
setHeaders(merge(this.headers, http.headers));
}

@Override
public String getPrefix() {
return "http";
}

public Authorization resolveAuthorization() {
if (null == authorization) {
authorization = Authorization.NONE;
}

return authorization;
}

public String getResolvedUsername() {
return Env.env("HTTP_" + Env.toVar(name) + "_USERNAME", username);
}

public String getResolvedPassword() {
return Env.env("HTTP_" + Env.toVar(name) + "_PASSWORD", password);
}

public String getResolvedUrl(JReleaserContext context) {
Map<String, Object> props = context.fullProps();
applyTemplates(props, getResolvedExtraProperties());
return resolveTemplate(url, props);
}

public String getResolvedPayload(JReleaserContext context) {
Map<String, Object> props = context.fullProps();
applyTemplates(props, getResolvedExtraProperties());
return resolveTemplate(payload, props);
}

public String getResolvedPayloadTemplate(JReleaserContext context, Map<String, Object> extraProps) {
Map<String, Object> props = context.fullProps();
applyTemplates(props, getResolvedExtraProperties());
props.put(KEY_TAG_NAME, context.getModel().getRelease().getGitService()
.getEffectiveTagName(context.getModel()));
props.putAll(extraProps);

Path templatePath = context.getBasedir().resolve(payloadTemplate);
try {
Reader reader = java.nio.file.Files.newBufferedReader(templatePath);
return applyTemplate(reader, props);
} catch (IOException e) {
throw new JReleaserException(RB.$("ERROR_unexpected_error_reading_template",
context.relativizeToBasedir(templatePath)));
}
}

public void setName(String name) {
freezeCheck();
this.name = name;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
freezeCheck();
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
freezeCheck();
this.password = password;
}

public Authorization getAuthorization() {
return authorization;
}

public void setAuthorization(Authorization authorization) {
freezeCheck();
this.authorization = authorization;
}

public void setAuthorization(String authorization) {
freezeCheck();
this.authorization = Authorization.of(authorization);
}

public Method getMethod() {
return method;
}

public void setMethod(Method method) {
freezeCheck();
this.method = method;
}

public void setMethod(String method) {
freezeCheck();
this.method = Method.of(method);
}

public Map<String, String> getHeaders() {
return freezeWrap(headers);
}

public void setHeaders(Map<String, String> headers) {
freezeCheck();
this.headers.putAll(headers);
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
freezeCheck();
this.url = url;
}

public String getPayload() {
return payload;
}

public void setPayload(String payload) {
freezeCheck();
this.payload = payload;
}

public String getPayloadTemplate() {
return payloadTemplate;
}

public void setPayloadTemplate(String payloadTemplate) {
freezeCheck();
this.payloadTemplate = payloadTemplate;
}

@Override
protected void asMap(Map<String, Object> props, boolean full) {
props.put("url", url);
props.put("authorization", authorization);
props.put("method", method);
props.put("username", isNotBlank(getResolvedUsername()) ? HIDE : UNSET);
props.put("password", isNotBlank(getResolvedPassword()) ? HIDE : UNSET);
props.put("headers", headers);
props.put("payload", payload);
props.put("payloadTemplate", payloadTemplate);
}
}
@@ -0,0 +1,92 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2022 The JReleaser authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jreleaser.model;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Andres Almiray
* @since 1.3.0
*/
public class HttpAnnouncers extends AbstractAnnouncer<HttpAnnouncers> {
public static final String NAME = "http";
private final Map<String, HttpAnnouncer> https = new LinkedHashMap<>();

public HttpAnnouncers() {
super(NAME);
}

@Override
public void freeze() {
super.freeze();
https.values().forEach(HttpAnnouncer::freeze);
}

@Override
public void merge(HttpAnnouncers http) {
freezeCheck();
super.merge(http);
setHttpAnnouncers(mergeModel(this.https, http.https));
}

public List<HttpAnnouncer> getActiveHttpAnnouncers() {
return https.values().stream()
.filter(HttpAnnouncer::isEnabled)
.collect(Collectors.toList());
}

public Map<String, HttpAnnouncer> getHttpAnnouncers() {
return freezeWrap(https);
}

public void setHttpAnnouncers(Map<String, HttpAnnouncer> https) {
freezeCheck();
this.https.clear();
this.https.putAll(https);
}

public void addHttpAnnouncer(HttpAnnouncer http) {
freezeCheck();
this.https.put(http.getName(), http);
}

@Override
public Map<String, Object> asMap(boolean full) {
if (!full && !isEnabled()) return Collections.emptyMap();

Map<String, Object> props = new LinkedHashMap<>();
asMap(props, full);

Map<String, Object> map = new LinkedHashMap<>();
map.put(getName(), props);
return map;
}

@Override
protected void asMap(Map<String, Object> props, boolean full) {
this.https.values()
.stream()
.filter(HttpAnnouncer::isEnabled)
.map(d -> d.asMap(full))
.forEach(props::putAll);
}
}

0 comments on commit 08d13b8

Please sign in to comment.