89 lines
3.5 KiB
Java
89 lines
3.5 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>Aug 5, 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;
|
||
|
var issueDescription = issueProposal.getDescription();
|
||
|
// Appending the submitter name, if this option was enabled
|
||
|
issueDescription += issueProposal.getSubmitterName() != null
|
||
|
? (issueDescription.endsWith("\n") || issueDescription.endsWith("<br>") ? "" : "<br>")
|
||
|
+ String.format("Submitted by user %s.", issueProposal.getSubmitterName())
|
||
|
: "";
|
||
|
// Markdown does not support "\n". It uses "<br>"
|
||
|
issueDescription = issueDescription.replaceAll("\n", "<br>");
|
||
|
// We do not want any Windows artifacts to remain as that may cause problems
|
||
|
issueDescription = issueDescription.replaceAll("\r", "");
|
||
|
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(),
|
||
|
issueDescription,
|
||
|
// 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 but received status code %d - Request params:title=%s,description=%s,json=%s",
|
||
|
status,
|
||
|
issueProposal.get(),
|
||
|
issueDescription,
|
||
|
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;
|
||
|
}
|
||
|
}
|