001package apps.util.issuereporter;
002
003import java.io.File;
004import java.io.IOException;
005import java.net.*;
006import java.nio.charset.StandardCharsets;
007import java.util.*;
008
009import javax.annotation.Nonnull;
010
011import jmri.*;
012import jmri.beans.Bean;
013
014import org.apiguardian.api.API;
015
016/**
017 * Common code for the various types of problem reports.
018 *
019 * @author Randall Wood Copyright 2020
020 */
021@API(status = API.Status.INTERNAL, consumers = "apps.util.issuereporter.*")
022public abstract class IssueReport extends Bean {
023
024    protected String title;
025    protected String body;
026    protected boolean tooLong = false;
027
028    // package protected
029    IssueReport(String title, String body) {
030        this.title = title;
031        this.body = body;
032    }
033
034    /**
035     * Submit a report.
036     *
037     * @param repository the GitHub repository to submit to
038     * @return the URI to submit the report to
039     * @throws URISyntaxException      if unable to create URI for issue
040     * @throws IOException             if unable to connect to GitHub
041     * @throws IssueReport414Exception if report is too long
042     */
043    @Nonnull
044    public URI submit(GitHubRepository repository) throws URISyntaxException, IOException, IssueReport414Exception {
045        prepare();
046        URI uri = new URI(String.format("https://github.com/%s/%s/issues/new?title=%s;body=%s",
047                repository.getOwner(),
048                repository.getName(),
049                URLEncoder.encode(title, StandardCharsets.UTF_8.toString()),
050                URLEncoder.encode(body, StandardCharsets.UTF_8.toString())));
051        HttpURLConnection request = (HttpURLConnection) uri.toURL().openConnection();
052        request.setRequestMethod("GET");
053        request.connect();
054        if (request.getResponseCode() != 200) {
055            tooLong = true;
056            throw new IssueReport414Exception();
057        }
058        return uri;
059    }
060
061    /**
062     * Prepare a report.
063     */
064    protected abstract void prepare();
065
066    @Nonnull
067    public List<File> getAttachments() {
068        return new ArrayList<>();
069    }
070
071    /**
072     * Get the simple context (JMRI version, Java version, and OS)
073     *
074     * @return the context
075     */
076    @Nonnull
077    public String getSimpleContext() {
078        return Bundle.getMessage(Locale.US, "issue.context",
079                Application.getApplicationName(),
080                Version.name(),
081                System.getProperty("java.vm.name"),
082                System.getProperty("java.vm.vendor"),
083                System.getProperty("java.vm.version"),
084                System.getProperty("java.vm.info"),
085                System.getProperty("os.name"),
086                System.getProperty("os.version"),
087                System.getProperty("os.arch"));
088    }
089
090    @Nonnull
091    public String getIssueFooter() {
092        return Bundle.getMessage(Locale.US, "issue.footer");
093    }
094
095    @Nonnull
096    public String getBody() {
097        return body;
098    }
099}