80 lines
2.9 KiB
Java
80 lines
2.9 KiB
Java
package envoy.server.processors;
|
|
|
|
import java.io.IOException;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
import envoy.event.IssueProposal;
|
|
import envoy.server.net.ObjectWriteProxy;
|
|
import envoy.util.EnvoyLog;
|
|
|
|
/**
|
|
* This processor handles incoming {@link IssueProposal}s and automatically
|
|
* creates a new issue on the gitea site, if not disabled by its administrator.
|
|
* <p>
|
|
* Project: <strong>server</strong><br>
|
|
* File: <strong>IssueProposalProcessor.java</strong><br>
|
|
* Created: <strong>05.08.2020</strong><br>
|
|
*
|
|
* @author Leon Hofmeister
|
|
* @since Envoy Server v0.2-beta
|
|
*/
|
|
public class IssueProposalProcessor implements ObjectProcessor<IssueProposal> {
|
|
|
|
private static boolean issueReportingEnabled = true;
|
|
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 (!issueReportingEnabled) return;
|
|
try {
|
|
final var url = new URL(
|
|
"https://git.kske.dev/api/v1/repos/zdm/envoy/issues?access_token=6d8ec2a72d64cbaf6319434aa2e7caf0130701b3");
|
|
final var connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("POST");
|
|
connection.setRequestProperty("Content-Type", "application/json; utf-8");
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
connection.setDoOutput(true);
|
|
|
|
final var json = String.format("{\"title\":\"%s\",\"body\":\"%s\",\"labels\":[240, %d]}",
|
|
issueProposal.get(),
|
|
issueProposal.getDescription(),
|
|
// Label 240 should be user-made, label 117 bug and label 119 feature
|
|
issueProposal.isBug() ? 117 : 119);
|
|
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:title=%s,description=%s,json=%s",
|
|
url,
|
|
status,
|
|
issueProposal.get(),
|
|
issueProposal.getDescription(),
|
|
json));
|
|
} catch (final IOException e) {
|
|
logger.log(Level.WARNING, "An error occurred while creating an issue: ", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return whether issue reporting is enabled
|
|
* @since Envoy Server v0.2-beta
|
|
*/
|
|
public static boolean isIssueReportingEnabled() { return issueReportingEnabled; }
|
|
|
|
/**
|
|
* @param issueReportingEnabled whether issue reporting should be enabled - true
|
|
* by default
|
|
* @since Envoy Server v0.2-beta
|
|
*/
|
|
public static void setIssueReportingEnabled(boolean issueReportingEnabled) {
|
|
IssueProposalProcessor.issueReportingEnabled = issueReportingEnabled;
|
|
}
|
|
}
|