|
| 1 | +//usr/bin/env jbang "$0" "$@" ; exit $? |
| 2 | + |
| 3 | +//DEPS org.kohsuke:github-api:1.101 |
| 4 | +//DEPS info.picocli:picocli:4.2.0 |
| 5 | + |
| 6 | +import org.kohsuke.github.GitHub; |
| 7 | +import org.kohsuke.github.GitHubBuilder; |
| 8 | +import org.kohsuke.github.GHRepository; |
| 9 | +import org.kohsuke.github.GHIssue; |
| 10 | +import org.kohsuke.github.GHIssueComment; |
| 11 | +import org.kohsuke.github.GHIssueState; |
| 12 | +import picocli.CommandLine; |
| 13 | +import picocli.CommandLine.Command; |
| 14 | +import picocli.CommandLine.Option; |
| 15 | +import picocli.CommandLine.Parameters; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.UncheckedIOException; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | + |
| 22 | +@Command(name = "report", mixinStandardHelpOptions = true, |
| 23 | + description = "Takes care of updating the appropriate issue depending on the status of the build") |
| 24 | +class Report implements Runnable { |
| 25 | + |
| 26 | + @Option(names = "token", description = "Github token to use when calling the Github API") |
| 27 | + private String token; |
| 28 | + |
| 29 | + @Option(names = "status", description = "The status of the CI run") |
| 30 | + private String status; |
| 31 | + |
| 32 | + @Option(names = "issueRepo", description = "The repository where the issue resides (i.e. quarkusio/quarkus)") |
| 33 | + private String issueRepo; |
| 34 | + |
| 35 | + @Option(names = "issueNumber", description = "The issue to update") |
| 36 | + private Integer issueNumber; |
| 37 | + |
| 38 | + @Option(names = "thisRepo", description = "The repository for which we are reporting the CI status") |
| 39 | + private String thisRepo; |
| 40 | + |
| 41 | + @Option(names = "runId", description = "The ID of the Github Action run for which we are reporting the CI status") |
| 42 | + private String runId; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void run() { |
| 46 | + try { |
| 47 | + final boolean succeed = "success".equalsIgnoreCase(status); |
| 48 | + if ("cancelled".equalsIgnoreCase(status)) { |
| 49 | + System.out.println("Job status is `cancelled` - exiting"); |
| 50 | + System.exit(0); |
| 51 | + } |
| 52 | + |
| 53 | + System.out.println(String.format("The CI build had status %s.", status)); |
| 54 | + |
| 55 | + final GitHub github = new GitHubBuilder().withOAuthToken(token).build(); |
| 56 | + final GHRepository repository = github.getRepository(issueRepo); |
| 57 | + |
| 58 | + final GHIssue issue = repository.getIssue(issueNumber); |
| 59 | + if (issue == null) { |
| 60 | + System.out.println(String.format("Unable to find the issue %s in project %s", issueNumber, issueRepo)); |
| 61 | + System.exit(-1); |
| 62 | + } else { |
| 63 | + System.out.println(String.format("Report issue found: %s - %s", issue.getTitle(), issue.getHtmlUrl().toString())); |
| 64 | + System.out.println(String.format("The issue is currently %s", issue.getState().toString())); |
| 65 | + } |
| 66 | + |
| 67 | + if (succeed) { |
| 68 | + if (issue != null && isOpen(issue)) { |
| 69 | + // close issue with a comment |
| 70 | + final GHIssueComment comment = issue.comment(String.format("Build fixed:\n* Link to latest CI run: https://github.com/%s/actions/runs/%s", thisRepo, runId)); |
| 71 | + issue.close(); |
| 72 | + System.out.println(String.format("Comment added on issue %s - %s, the issue has also been closed", issue.getHtmlUrl().toString(), comment.getHtmlUrl().toString())); |
| 73 | + } else { |
| 74 | + System.out.println("Nothing to do - the build passed and the issue is already closed"); |
| 75 | + } |
| 76 | + } else { |
| 77 | + if (isOpen(issue)) { |
| 78 | + final GHIssueComment comment = issue.comment(String.format("The build is still failing:\n* Link to latest CI run: https://github.com/%s/actions/runs/%s", thisRepo, runId)); |
| 79 | + System.out.println(String.format("Comment added on issue %s - %s", issue.getHtmlUrl().toString(), comment.getHtmlUrl().toString())); |
| 80 | + } else { |
| 81 | + issue.reopen(); |
| 82 | + final GHIssueComment comment = issue.comment(String.format("Unfortunately, the build failed:\n* Link to latest CI run: https://github.com/%s/actions/runs/%s", thisRepo, runId)); |
| 83 | + System.out.println(String.format("Comment added on issue %s - %s, the issue has been re-opened", issue.getHtmlUrl().toString(), comment.getHtmlUrl().toString())); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + catch (IOException e) { |
| 88 | + throw new UncheckedIOException(e); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static boolean isOpen(GHIssue issue) { |
| 93 | + return (issue.getState() == GHIssueState.OPEN); |
| 94 | + } |
| 95 | + |
| 96 | + public static void main(String... args) { |
| 97 | + int exitCode = new CommandLine(new Report()).execute(args); |
| 98 | + System.exit(exitCode); |
| 99 | + } |
| 100 | +} |
0 commit comments