Skip to content

Commit

Permalink
#415: Make Github check name consistent with Sonarqube commercial edi…
Browse files Browse the repository at this point in the history
…tion

The check name for decorating Github Pull Requests is currently based on the project name, which prevents setting a global policy for an action having completed on all projects based on a single name. To bring the plugin in-line with the commercial edition, a static name is being used during decoration unless the project is declared as a mono-repo, in which case the project name will be added to the check name.
  • Loading branch information
jackylamhk authored Nov 25, 2024
1 parent 3ca9bae commit d4298a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

public class GithubPullRequestDecorator implements PullRequestBuildStatusDecorator {

private static final String DEFAULT_CHECK_RUN_NAME = "SonarQube Code Analysis";
private final GithubClientFactory githubClientFactory;
private final ReportGenerator reportGenerator;
private final MarkdownFormatterFactory markdownFormatterFactory;
Expand All @@ -71,7 +72,8 @@ public DecorationResult decorateQualityGateStatus(AnalysisDetails analysisDetail
GitHub github = githubClientFactory.createClient(almSettingDto, projectAlmSettingDto);
GHRepository repository = github.getRepository(projectAlmSettingDto.getAlmRepo());

GHPullRequest pullRequest = createCheckRun(repository, analysisDetails, Optional.ofNullable(projectAlmSettingDto.getSummaryCommentEnabled()).orElse(false));
GHPullRequest pullRequest = createCheckRun(repository, analysisDetails, projectAlmSettingDto.getMonorepo(),
Optional.ofNullable(projectAlmSettingDto.getSummaryCommentEnabled()).orElse(false));

return DecorationResult.builder()
.withPullRequestUrl(pullRequest.getHtmlUrl().toExternalForm())
Expand All @@ -88,7 +90,8 @@ public List<ALM> alm() {
}


private GHPullRequest createCheckRun(GHRepository repository, AnalysisDetails analysisDetails, boolean postSummaryComment) throws IOException {
private GHPullRequest createCheckRun(GHRepository repository, AnalysisDetails analysisDetails,
boolean isMonorepo, boolean postSummaryComment) throws IOException {
AnalysisSummary analysisSummary = reportGenerator.createAnalysisSummary(analysisDetails);
String summary = analysisSummary.format(markdownFormatterFactory);

Expand All @@ -102,7 +105,10 @@ private GHPullRequest createCheckRun(GHRepository repository, AnalysisDetails an
);
}

repository.createCheckRun(String.format("%s Sonarqube Results", analysisDetails.getAnalysisProjectName()), analysisDetails.getCommitSha())
String checkRunName = isMonorepo
? String.format("[%s] %s", analysisDetails.getAnalysisProjectName(), DEFAULT_CHECK_RUN_NAME)
: DEFAULT_CHECK_RUN_NAME;
repository.createCheckRun(checkRunName, analysisDetails.getCommitSha())
.withStartedAt(analysisDetails.getAnalysisDate())
.withCompletedAt(Date.from(clock.instant()))
.withStatus(GHCheckRun.Status.COMPLETED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class GithubPullRequestDecoratorTest {
@BeforeEach
void setUp() throws IOException {
when(projectAlmSettingDto.getAlmRepo()).thenReturn("alm-repo");
when(projectAlmSettingDto.getMonorepo()).thenReturn(false);
when(analysisDetails.getPullRequestId()).thenReturn("123");
when(analysisDetails.getAnalysisDate()).thenReturn(Date.from(clock.instant()));
when(analysisDetails.getAnalysisId()).thenReturn("analysis-id");
Expand Down Expand Up @@ -153,7 +154,7 @@ void shouldDecoratePullRequestWithCorrectAnalysisAndSummaryCommentWhenEnabled()
DecorationResult decorationResult = testCase.decorateQualityGateStatus(analysisDetails, almSettingDto, projectAlmSettingDto);

verify(gitHub).getRepository("alm-repo");
verify(repository).createCheckRun("Project Name Sonarqube Results", "commit-sha");
verify(repository).createCheckRun("SonarQube Code Analysis", "commit-sha");

ArgumentCaptor<GHCheckRunBuilder.Output> outputCaptor = ArgumentCaptor.captor();

Expand Down Expand Up @@ -214,7 +215,7 @@ void shouldDecoratePullRequestWithCorrectAnalysisAndNoSummaryCommentWhenDisabled
DecorationResult decorationResult = testCase.decorateQualityGateStatus(analysisDetails, almSettingDto, projectAlmSettingDto);

verify(gitHub).getRepository("alm-repo");
verify(repository).createCheckRun("Project Name Sonarqube Results", "commit-sha");
verify(repository).createCheckRun("SonarQube Code Analysis", "commit-sha");

ArgumentCaptor<GHCheckRunBuilder.Output> outputCaptor = ArgumentCaptor.captor();

Expand Down

0 comments on commit d4298a8

Please sign in to comment.