68 lines
2.4 KiB
Java
68 lines
2.4 KiB
Java
package envoy.server.processors;
|
|
|
|
import static envoy.server.Startup.config;
|
|
|
|
import java.io.IOException;
|
|
import java.net.*;
|
|
import java.util.logging.*;
|
|
|
|
import envoy.event.IssueProposal;
|
|
import envoy.util.EnvoyLog;
|
|
|
|
import envoy.server.net.ObjectWriteProxy;
|
|
|
|
/**
|
|
* This processor handles incoming {@link IssueProposal}s and automatically creates a new issue on
|
|
* the gitea site, if not disabled by its administrator.
|
|
*
|
|
* @author Leon Hofmeister
|
|
* @since Envoy Server v0.2-beta
|
|
*/
|
|
public final class IssueProposalProcessor implements ObjectProcessor<IssueProposal> {
|
|
|
|
private static final Logger logger = EnvoyLog.getLogger(IssueProposalProcessor.class);
|
|
|
|
@Override
|
|
public void process(IssueProposal issueProposal, long socketID, ObjectWriteProxy writeProxy)
|
|
throws IOException {
|
|
// Do nothing if manually disabled
|
|
if (!config.isIssueReportingEnabled())
|
|
return;
|
|
try {
|
|
final var url = new URL(config.getIssueReportingURL());
|
|
final var connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("POST");
|
|
connection.setRequestProperty("Content-Type", "application/json; utf-8");
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
// Two types of authorization are currently supported: access token as
|
|
// query param or access token as authorization header
|
|
final var authenticationToken = config.getIssueAuthToken();
|
|
if (!authenticationToken.equals("")) {
|
|
final String auth = "token " + authenticationToken;
|
|
connection.setRequestProperty("Authorization", auth);
|
|
}
|
|
connection.setDoOutput(true);
|
|
|
|
final var json = String.format("{\"title\":\"%s\",\"body\":\"%s\",\"labels\":[%s, %s]}",
|
|
issueProposal.get(),
|
|
issueProposal.getDescription(),
|
|
config.getUserMadeLabel(),
|
|
issueProposal.isBug() ? config.getBugLabel() : config.getFeatureLabel());
|
|
try (final var os = connection.getOutputStream()) {
|
|
final byte[] input = json.getBytes("utf-8");
|
|
os.write(input, 0, input.length);
|
|
}
|
|
final var status = connection.getResponseCode();
|
|
if (status == 201)
|
|
logger.log(Level.INFO, "Successfully created an issue");
|
|
else
|
|
logger.log(Level.WARNING,
|
|
String.format(
|
|
"Tried creating an issue for %s but received status code %d - Request params:%s",
|
|
url, status, json));
|
|
} catch (final IOException e) {
|
|
logger.log(Level.WARNING, "An error occurred while creating an issue: ", e);
|
|
}
|
|
}
|
|
}
|