Skip to content

Commit

Permalink
Merge pull request #21 from gsmet/check-run-url
Browse files Browse the repository at this point in the history
Include a link to build scans check run
  • Loading branch information
gsmet authored Nov 16, 2023
2 parents ac71ef5 + d2bfe97 commit 70eda44
Showing 1 changed file with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
public class InjectBuildScansAction {

private static final String WORKFLOW_RUN_ID_MARKER = "<!-- Quarkus-GitHub-Bot/workflow-run-id:%1$s -->";
private static final String BUILD_SCANS_CHECK_RUN_MARKER = "<!-- Quarkus-GitHub-Bot/build-scans-check-run -->";
private static final String BUILD_SCANS = "Build scans";

@Inject
Expand Down Expand Up @@ -78,8 +79,8 @@ void injectBuildScans(Context context, Commands commands, Inputs inputs, GitHub
return;
}

createBuildScansOutput(commands, workflowRun, statuses);
updateComment(commands, pullRequest, workflowRun, buildScanMapping);
Optional<GHCheckRun> buildScansCheckRun = createBuildScansOutput(commands, workflowRun, statuses);
updateComment(commands, pullRequest, workflowRun, buildScanMapping, buildScansCheckRun);

// note for future self: it is not possible to update an existing check run created by another GitHub App
} catch (IOException e) {
Expand All @@ -88,7 +89,7 @@ void injectBuildScans(Context context, Commands commands, Inputs inputs, GitHub
}

private void updateComment(Commands commands, GHPullRequest pullRequest, GHWorkflowRun workflowRun,
Map<String, String> buildScanMapping) {
Map<String, String> buildScanMapping, Optional<GHCheckRun> buildScansCheckRun) {
try {
Optional<GHIssueComment> reportCommentCandidate = getPullRequestComment(commands, workflowRun, pullRequest);

Expand All @@ -105,14 +106,21 @@ private void updateComment(Commands commands, GHPullRequest pullRequest, GHWorkf
return line.replace(":construction:", "[:mag:](" + buildScanEntry.getValue() + ")");
}
}

return line;
}).collect(Collectors.joining("\n"));

if (buildScansCheckRun.isPresent()) {
updatedCommentBody = updatedCommentBody.replace(BUILD_SCANS_CHECK_RUN_MARKER,
"You can also consult the [Develocity build scans](" + buildScansCheckRun.get().getHtmlUrl() + ").");
}

if (!updatedCommentBody.equals(reportComment.getBody())) {
reportComment.update(updatedCommentBody);
}
} catch (Exception e) {
commands.error("Unable to update the PR comment: " + e.getMessage());
e.printStackTrace();
}
}

Expand Down Expand Up @@ -172,28 +180,30 @@ public Optional<GHIssueComment> getReportComment() {
}
}

private void createBuildScansOutput(Commands commands, GHWorkflowRun workflowRun, BuildScanStatuses statuses) {
private Optional<GHCheckRun> createBuildScansOutput(Commands commands, GHWorkflowRun workflowRun, BuildScanStatuses statuses) {
try {
Output output = new Output(BUILD_SCANS, BUILD_SCANS);
Output output = new Output(BUILD_SCANS, "You will find below links to the Develocity build scans for this workflow run.");

StringBuilder buildScans = new StringBuilder();
buildScans.append("| Status | Name | Build scan |\n");
buildScans.append("| :-: | -- | :-: |\n");

for (BuildScanStatus build : statuses.builds) {
buildScans.append("| ").append(getConclusionEmoji(build.status)).append(" | ").append(build.jobName)
.append(" | [:mag:](").append(build.buildScanLink).append(") |");
.append(" | [:mag:](").append(build.buildScanLink).append(") |\n");
}

output.withText(buildScans.toString());

workflowRun.getRepository().createCheckRun(BUILD_SCANS, workflowRun.getHeadSha())
return Optional.of(workflowRun.getRepository().createCheckRun(BUILD_SCANS, workflowRun.getHeadSha())
.add(output)
.withConclusion(GHCheckRun.Conclusion.NEUTRAL)
.withCompletedAt(new Date())
.create();
.create());
} catch (Exception e) {
commands.error("Unable to create a check run with build scans: " + e.getMessage());
e.printStackTrace();
return Optional.empty();
}
}

Expand Down

0 comments on commit 70eda44

Please sign in to comment.