diff --git a/Jenkinsfile b/Jenkinsfile index 5e9546e3..ddc03e69 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,8 +1,9 @@ def configurations = [ - [ platform: "linux", jdk: "11" ], - [ platform: "windows", jdk: "11" ] + [platform: 'linux', jdk: 17], + [platform: 'windows', jdk: 11], ] buildPlugin(failFast: false, configurations: configurations, + useContainerAgent: true, checkstyle: [qualityGates: [[threshold: 1, type: 'NEW', unstable: true]]], pmd: [qualityGates: [[threshold: 1, type: 'NEW', unstable: true]]] ) diff --git a/pom.xml b/pom.xml index 7e9ecde8..6a0326ac 100644 --- a/pom.xml +++ b/pom.xml @@ -1,12 +1,12 @@ - + 4.0.0 - org.jvnet.hudson.plugins - analysis-pom - 5.17.0 - + org.jenkins-ci.plugins + plugin + 4.60 + io.jenkins.plugins @@ -15,28 +15,12 @@ hpi GitHub Checks plugin - Allows users to publish GitHub checks - 8 1.0.20 -SNAPSHOT - + 2.361.4 true - - - 1.7.2 - 2.9.0 - 2.9.3 - - - 9.4.51.v20230217 - 2.14.2 - 2.35.0 - 29.0-jre - 9.2 - 2.37.0 - 4.5.14 @@ -48,7 +32,7 @@ https://github.com/jenkinsci/${project.artifactId}-plugin - scm:git:git://github.com/jenkinsci/${project.artifactId}-plugin.git + scm:git:https://github.com/jenkinsci/${project.artifactId}-plugin.git scm:git:git@github.com:jenkinsci/${project.artifactId}-plugin.git https://github.com/jenkinsci/${project.artifactId}-plugin ${scmTag} @@ -57,42 +41,28 @@ - org.eclipse.jetty - jetty-client - ${jetty.version} + io.jenkins.tools.bom + bom-2.361.x + 2000.v4677a_6e0ffea + import + pom - - io.jenkins.plugins - plugin-util-api - ${plugin-util-api.version} - - - io.jenkins.plugins - plugin-util-api - ${plugin-util-api.version} - test - test-jar - - io.jenkins.plugins checks-api - ${checks-api.version} org.jenkins-ci.plugins github-branch-source - ${github-branch-source.version} org.jenkins-ci.plugins github-api - 1.117 org.jenkins-ci.plugins.workflow @@ -103,129 +73,19 @@ workflow-job - - - com.github.tomakehurst - wiremock-jre8 - ${wiremock.version} - test - - - com.fasterxml.jackson.core - jackson-core - ${jackson.version} - test - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - test - - - com.fasterxml.jackson.core - jackson-annotations - ${jackson.version} - test - - - org.eclipse.jetty - jetty-server - ${jetty.version} - test - - - org.eclipse.jetty - jetty-http - ${jetty.version} - test - - - org.eclipse.jetty - jetty-util - ${jetty.version} - test - - - org.eclipse.jetty - jetty-io - ${jetty.version} - test - - - org.eclipse.jetty - jetty-servlet - ${jetty.version} - test - - - org.eclipse.jetty - jetty-webapp - ${jetty.version} - test - - - org.ow2.asm - asm - ${asm.version} - test - - - net.javacrumbs.json-unit - json-unit-core - ${json-unit-core.version} - test - - - org.apache.httpcomponents - httpclient - ${http-client.version} - test - - - com.google.guava - guava - 20.0 - test - org.mockito - mockito-inline - ${mockito.version} + mockito-core test - - org.jenkins-ci.plugins - pipeline-stage-step - 2.5 + org.assertj + assertj-core + 3.23.1 test - - - org.jenkins-ci.plugins.workflow - workflow-durable-task-step - test - - - - - - org.assertj - assertj-assertions-generator-maven-plugin - - - io.jenkins.plugins.checks.github - io.jenkins.plugins.checks.github.status - - io.jenkins.plugins.checks.github.assertions - - - - - repo.jenkins-ci.org diff --git a/src/main/resources/index.jelly b/src/main/resources/index.jelly index fa088d93..0f46e317 100644 --- a/src/main/resources/index.jelly +++ b/src/main/resources/index.jelly @@ -1,4 +1,4 @@
- This plugin provide GitHub Checks API for other plugins. + Allows users to publish GitHub checks.
diff --git a/src/test/java/io/jenkins/plugins/checks/ArchitectureTest.java b/src/test/java/io/jenkins/plugins/checks/ArchitectureTest.java deleted file mode 100644 index 1b0cfe62..00000000 --- a/src/test/java/io/jenkins/plugins/checks/ArchitectureTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package io.jenkins.plugins.checks; - -import com.tngtech.archunit.junit.AnalyzeClasses; -import com.tngtech.archunit.junit.ArchTest; -import com.tngtech.archunit.lang.ArchRule; - -import edu.hm.hafner.util.ArchitectureRules; - -import io.jenkins.plugins.util.PluginArchitectureRules; - -/** - * Defines several architecture rules for the static analysis model and parsers. - * - * @author Ullrich Hafner - */ -@SuppressWarnings("hideutilityclassconstructor") -@AnalyzeClasses(packages = "io.jenkins.plugins..") -class ArchitectureTest { - @ArchTest - static final ArchRule NO_JENKINS_INSTANCE_CALL = PluginArchitectureRules.NO_JENKINS_INSTANCE_CALL; - - @ArchTest - static final ArchRule NO_PUBLIC_TEST_CLASSES = PluginArchitectureRules.NO_PUBLIC_TEST_CLASSES; - - @ArchTest - static final ArchRule NO_TEST_API_CALLED = ArchitectureRules.NO_TEST_API_CALLED; - - @ArchTest - static final ArchRule NO_FORBIDDEN_PACKAGE_ACCESSED = PluginArchitectureRules.NO_FORBIDDEN_PACKAGE_ACCESSED; - - @ArchTest - static final ArchRule NO_FORBIDDEN_CLASSES_CALLED = ArchitectureRules.NO_FORBIDDEN_CLASSES_CALLED; -} diff --git a/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherITest.java b/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherITest.java deleted file mode 100644 index 5060ca5a..00000000 --- a/src/test/java/io/jenkins/plugins/checks/github/GitHubChecksPublisherITest.java +++ /dev/null @@ -1,393 +0,0 @@ -package io.jenkins.plugins.checks.github; - -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Optional; -import java.util.function.Function; -import java.util.logging.Level; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.jvnet.hudson.test.Issue; -import org.jvnet.hudson.test.LoggerRule; -import org.mockito.MockedStatic; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.InjectableValues; -import com.fasterxml.jackson.databind.MapperFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectReader; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; -import com.fasterxml.jackson.databind.introspect.VisibilityChecker; -import com.github.tomakehurst.wiremock.core.WireMockConfiguration; -import com.github.tomakehurst.wiremock.junit.WireMockRule; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - -import org.kohsuke.github.GHCheckRun; -import org.kohsuke.github.GHCheckRunBuilder; -import org.kohsuke.github.GHRepository; -import org.kohsuke.github.GitHub; -import org.jenkinsci.plugins.displayurlapi.ClassicDisplayURLProvider; -import org.jenkinsci.plugins.github_branch_source.Connector; -import org.jenkinsci.plugins.github_branch_source.GitHubAppCredentials; -import org.jenkinsci.plugins.github_branch_source.GitHubSCMSource; -import org.jenkinsci.plugins.github_branch_source.PullRequestSCMRevision; -import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.job.WorkflowJob; -import hudson.model.FreeStyleProject; -import hudson.model.Job; -import hudson.model.Queue; -import hudson.model.Run; -import hudson.util.Secret; -import jenkins.model.ParameterizedJobMixIn; -import jenkins.scm.api.SCMHead; - -import io.jenkins.plugins.checks.api.ChecksAction; -import io.jenkins.plugins.checks.api.ChecksAnnotation.ChecksAnnotationBuilder; -import io.jenkins.plugins.checks.api.ChecksAnnotation.ChecksAnnotationLevel; -import io.jenkins.plugins.checks.api.ChecksConclusion; -import io.jenkins.plugins.checks.api.ChecksDetails; -import io.jenkins.plugins.checks.api.ChecksDetails.ChecksDetailsBuilder; -import io.jenkins.plugins.checks.api.ChecksImage; -import io.jenkins.plugins.checks.api.ChecksOutput.ChecksOutputBuilder; -import io.jenkins.plugins.checks.api.ChecksStatus; -import io.jenkins.plugins.util.IntegrationTestWithJenkinsPerTest; -import io.jenkins.plugins.util.PluginLogger; - -import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.*; -import static io.jenkins.plugins.checks.github.assertions.Assertions.*; -import static org.mockito.Mockito.*; - -/** - * Tests if the {@link GitHubChecksPublisher} actually sends out the requests to GitHub in order to publish the check - * runs. - */ -@RunWith(Parameterized.class) -@SuppressWarnings({"PMD.ExcessiveImports", "checkstyle:ClassDataAbstractionCoupling", "rawtypes", "checkstyle:ClassFanOutComplexity", "checkstyle:JavaNCSS"}) -public class GitHubChecksPublisherITest extends IntegrationTestWithJenkinsPerTest { - - /** - * Provides parameters for tests. - * @return A list of methods used to create GitHubChecksContexts, with which each test should be run. - */ - @Parameterized.Parameters(name = "{0}") - public static Collection contextBuilders() { - return Arrays.asList(new Object[][]{ - {"Freestyle (run)", (Function) GitHubChecksPublisherITest::createGitHubChecksContextWithGitHubSCMFreestyle, false}, - {"Freestyle (job)", (Function) GitHubChecksPublisherITest::createGitHubChecksContextWithGitHubSCMFreestyle, true}, - {"Pipeline (run)", (Function) GitHubChecksPublisherITest::createGitHubChecksContextWithGitHubSCMFromPipeline, false}, - {"Pipeline (job)", (Function) GitHubChecksPublisherITest::createGitHubChecksContextWithGitHubSCMFromPipeline, true} - }); - } - - /** - * Human readable name of the context builder - used only for test name formatting. - */ - @SuppressWarnings("checkstyle:VisibilityModifier") - @Parameterized.Parameter(0) - public String contextBuilderName; - - /** - * Reference to method used to create GitHubChecksContext with either a pipeline or freestyle job. - */ - @SuppressWarnings("checkstyle:VisibilityModifier") - @Parameterized.Parameter(1) - public Function contextBuilder; - - /** - * Create GitHubChecksContext from the job instead of the run. - */ - @SuppressWarnings("checkstyle:VisibilityModifier") - @Parameterized.Parameter(2) - public boolean fromJob; - - /** - * Rule for the log system. - */ - @Rule - public LoggerRule loggerRule = new LoggerRule(); - - /** - * A rule which provides a mock server. - */ - @Rule - public WireMockRule wireMockRule = new WireMockRule( - WireMockConfiguration.options().dynamicPort()); - - /** - * Checks should be published to GitHub correctly when GitHub SCM is found and parameters are correctly set. - */ - @Test - public void shouldPublishGitHubCheckRunCorrectly() { - ChecksDetails details = new ChecksDetailsBuilder() - .withName("Jenkins") - .withStatus(ChecksStatus.COMPLETED) - .withDetailsURL("https://ci.jenkins.io") - .withStartedAt(LocalDateTime.ofEpochSecond(999_999, 0, ZoneOffset.UTC)) - .withCompletedAt(LocalDateTime.ofEpochSecond(999_999, 0, ZoneOffset.UTC)) - .withConclusion(ChecksConclusion.SUCCESS) - .withOutput(new ChecksOutputBuilder() - .withTitle("Jenkins Check") - .withSummary("# A Successful Build") - .withText("## 0 Failures") - .withAnnotations(Arrays.asList( - new ChecksAnnotationBuilder() - .withPath("Jenkinsfile") - .withLine(1) - .withAnnotationLevel(ChecksAnnotationLevel.NOTICE) - .withMessage("say hello to Jenkins") - .withStartColumn(0) - .withEndColumn(20) - .withTitle("Hello Jenkins") - .withRawDetails("a simple echo command") - .build(), - new ChecksAnnotationBuilder() - .withPath("Jenkinsfile") - .withLine(2) - .withAnnotationLevel(ChecksAnnotationLevel.WARNING) - .withMessage("say hello to GitHub Checks API") - .withStartColumn(0) - .withEndColumn(30) - .withTitle("Hello GitHub Checks API") - .withRawDetails("a simple echo command") - .build())) - .withImages(Collections.singletonList( - new ChecksImage("Jenkins", - "https://ci.jenkins.io/static/cd5757a8/images/jenkins-header-logo-v2.svg", - "Jenkins Symbol"))) - .build()) - .withActions(Collections.singletonList( - new ChecksAction("re-run", "re-run Jenkins build", "#0"))) - .build(); - - new GitHubChecksPublisher(contextBuilder.apply(this), - new PluginLogger(getJenkins().createTaskListener().getLogger(), "GitHub Checks"), - wireMockRule.baseUrl()) - .publish(details); - } - - /** - * If exception happens when publishing checks, it should output all parameters of the check to the system log. - */ - @Issue("issue-20") - @Test - public void shouldLogChecksParametersIfExceptionHappensWhenPublishChecks() { - loggerRule.record(GitHubChecksPublisher.class.getName(), Level.WARNING).capture(1); - - ChecksDetails details = new ChecksDetailsBuilder() - .withName("Jenkins") - .withStatus(ChecksStatus.COMPLETED) - .withConclusion(ChecksConclusion.SUCCESS) - .withOutput(new ChecksOutputBuilder() - .withTitle("Jenkins Check") - .withSummary("# A Successful Build") - .withAnnotations(Collections.singletonList( - new ChecksAnnotationBuilder() - .withPath("Jenkinsfile") - .withStartLine(1) - .withEndLine(2) - .withStartColumn(0) - .withEndColumn(20) - .withAnnotationLevel(ChecksAnnotationLevel.WARNING) - .withMessage("say hello to Jenkins") - .build())) - .build()) - .build(); - - new GitHubChecksPublisher(contextBuilder.apply(this), - new PluginLogger(getJenkins().createTaskListener().getLogger(), "GitHub Checks"), - wireMockRule.baseUrl()) - .publish(details); - - assertThat(loggerRule.getRecords().size()).isEqualTo(1); - assertThat(loggerRule.getMessages().get(0)) - .contains("Failed Publishing GitHub checks: ") - .contains("name='Jenkins'") - .contains("status=COMPLETED") - .contains("conclusion=SUCCESS") - .contains("title='Jenkins Check'") - .contains("summary='# A Successful Build'") - .contains("path='Jenkinsfile'") - .contains("startLine=1") - .contains("endLine=2") - .contains("startColumn=0") - .contains("endColumn=20") - .contains("annotationLevel=WARNING") - .contains("message='say hello to Jenkins'"); - } - - /** - * We can't mock the id field on {@link org.kohsuke.github.GHObject}s thanks to {@link com.infradna.tool.bridge_method_injector.WithBridgeMethods}. - * So, create a stub GHCheckRun with the id we want. - * @param id id of check run to spoof - * @return Stubbed {@link GHCheckRun} with only the id of {@link GHCheckRun} set - */ - private GHCheckRun createStubCheckRun(final long id) throws JsonProcessingException { - ObjectMapper mapper = new ObjectMapper(); - mapper.setVisibility(new VisibilityChecker.Std(NONE, NONE, NONE, NONE, ANY)); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true); - mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); - - ObjectReader reader = mapper.reader(new InjectableValues.Std().addValue("org.kohsuke.github.GitHubResponse$ResponseInfo", null)).forType(GHCheckRun.class); - - return reader.readValue(String.format("{\"id\": %d}", id)); - } - - /** - * Test that publishing a second check with the same name will update rather than overwrite the existing check. - */ - @Test - @SuppressFBWarnings(value = "RCN", justification = "False positive of SpotBugs") - public void testChecksPublisherUpdatesCorrectly() throws Exception { - GitHub gitHub = mock(GitHub.class); - GHRepository repository = mock(GHRepository.class); - when(gitHub.getRepository(anyString())).thenReturn(repository); - - long checksId1 = 1000; - long checksId2 = 2000; - - String checksName1 = "Test Updating"; - String checksName2 = "Different Tests"; - - GHCheckRunBuilder createBuilder1 = mock(GHCheckRunBuilder.class, RETURNS_SELF); - GHCheckRunBuilder createBuilder2 = mock(GHCheckRunBuilder.class, RETURNS_SELF); - GHCheckRunBuilder updateBuilder1 = mock(GHCheckRunBuilder.class, RETURNS_SELF); - - GHCheckRun createResult1 = createStubCheckRun(checksId1); - GHCheckRun createResult2 = createStubCheckRun(checksId2); - - doReturn(createResult1).when(createBuilder1).create(); - doReturn(createResult2).when(createBuilder2).create(); - doReturn(createResult1).when(updateBuilder1).create(); - - when(repository.createCheckRun(eq(checksName1), anyString())).thenReturn(createBuilder1); - when(repository.createCheckRun(eq(checksName2), anyString())).thenReturn(createBuilder2); - when(repository.updateCheckRun(checksId1)).thenReturn(updateBuilder1); - - try (MockedStatic connector = mockStatic(Connector.class)) { - connector.when(() -> Connector.connect(anyString(), any(GitHubAppCredentials.class))).thenReturn(gitHub); - - GitHubChecksContext context = contextBuilder.apply(this); - - ChecksDetails details1 = new ChecksDetailsBuilder() - .withName(checksName1) - .withStatus(ChecksStatus.IN_PROGRESS) - .build(); - - GitHubChecksPublisher publisher = new GitHubChecksPublisher(context, - new PluginLogger(getJenkins().createTaskListener().getLogger(), "GitHub Checks"), - "https://github.example.com/" - ); - - assertThat(context.getId(checksName1)).isNotPresent(); - assertThat(context.getId(checksName2)).isNotPresent(); - - publisher.publish(details1); - - verify(createBuilder1, times(1)).create(); - verify(createBuilder2, never()).create(); - verify(updateBuilder1, never()).create(); - - if (fromJob) { - assertThat(context.getId(checksName1)).isNotPresent(); - } - else { - assertThat(context.getId(checksName1)).isPresent().get().isEqualTo(checksId1); - } - assertThat(context.getId(checksName2)).isNotPresent(); - - ChecksDetails details2 = new ChecksDetailsBuilder() - .withName(checksName2) - .withStatus(ChecksStatus.COMPLETED) - .withConclusion(ChecksConclusion.SUCCESS) - .build(); - - publisher.publish(details2); - - verify(createBuilder1, times(1)).create(); - verify(createBuilder2, times(1)).create(); - verify(updateBuilder1, never()).create(); - - if (fromJob) { - assertThat(context.getId(checksName1)).isNotPresent(); - assertThat(context.getId(checksName1)).isNotPresent(); - } - else { - assertThat(context.getId(checksName1)).isPresent().get().isEqualTo(checksId1); - assertThat(context.getId(checksName2)).isPresent().get().isEqualTo(checksId2); - } - - ChecksDetails updateDetails1 = new ChecksDetailsBuilder() - .withName(checksName1) - .withStatus(ChecksStatus.COMPLETED) - .withConclusion(ChecksConclusion.FAILURE) - .build(); - - publisher.publish(updateDetails1); - - verify(createBuilder1, times(fromJob ? 2 : 1)).create(); - verify(createBuilder2, times(1)).create(); - verify(updateBuilder1, times(fromJob ? 0 : 1)).create(); - - if (fromJob) { - assertThat(context.getId(checksName1)).isNotPresent(); - assertThat(context.getId(checksName1)).isNotPresent(); - } - else { - assertThat(context.getId(checksName1)).isPresent().get().isEqualTo(checksId1); - assertThat(context.getId(checksName2)).isPresent().get().isEqualTo(checksId2); - } - } - } - - private GitHubChecksContext createGitHubChecksContextWithGitHubSCMFreestyle() { - FreeStyleProject job = createFreeStyleProject(); - return createGitHubChecksContextWithGitHubSCM(job); - } - - private GitHubChecksContext createGitHubChecksContextWithGitHubSCMFromPipeline() { - WorkflowJob job = createPipeline(); - job.setDefinition(new CpsFlowDefinition("node {}", true)); - return createGitHubChecksContextWithGitHubSCM(job); - } - - private & Queue.Executable, J extends Job & ParameterizedJobMixIn.ParameterizedJob> - GitHubChecksContext createGitHubChecksContextWithGitHubSCM(final J job) { - Run run = buildSuccessfully(job); - - SCMFacade scmFacade = mock(SCMFacade.class); - GitHubSCMSource source = mock(GitHubSCMSource.class); - GitHubAppCredentials credentials = mock(GitHubAppCredentials.class); - SCMHead head = mock(SCMHead.class); - PullRequestSCMRevision revision = mock(PullRequestSCMRevision.class); - ClassicDisplayURLProvider urlProvider = mock(ClassicDisplayURLProvider.class); - - when(source.getCredentialsId()).thenReturn("1"); - when(source.getRepoOwner()).thenReturn("XiongKezhi"); - when(source.getRepository()).thenReturn("Sandbox"); - when(credentials.getPassword()).thenReturn(Secret.fromString("password")); - - when(scmFacade.findGitHubSCMSource(job)).thenReturn(Optional.of(source)); - when(scmFacade.findGitHubAppCredentials(job, "1")).thenReturn(Optional.of(credentials)); - when(scmFacade.findHead(job)).thenReturn(Optional.of(head)); - when(scmFacade.findRevision(source, run)).thenReturn(Optional.of(revision)); - when(scmFacade.findRevision(source, head)).thenReturn(Optional.of(revision)); - when(scmFacade.findHash(revision)).thenReturn(Optional.of("18c8e2fd86e7aa3748e279c14a00dc3f0b963e7f")); - - when(urlProvider.getRunURL(run)).thenReturn("https://ci.jenkins.io"); - when(urlProvider.getJobURL(job)).thenReturn("https://ci.jenkins.io"); - - if (fromJob) { - return GitHubSCMSourceChecksContext.fromJob(job, urlProvider.getJobURL(job), scmFacade); - } - return GitHubSCMSourceChecksContext.fromRun(run, urlProvider.getRunURL(run), scmFacade); - } -} diff --git a/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextITest.java b/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextITest.java deleted file mode 100644 index fd1b7835..00000000 --- a/src/test/java/io/jenkins/plugins/checks/github/GitSCMChecksContextITest.java +++ /dev/null @@ -1,78 +0,0 @@ -package io.jenkins.plugins.checks.github; - -import java.io.IOException; -import java.util.Collections; - -import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.job.WorkflowJob; -import org.junit.Test; - -import io.jenkins.plugins.util.IntegrationTestWithJenkinsPerSuite; -import static org.assertj.core.api.Assertions.*; - -import hudson.model.FreeStyleProject; -import hudson.model.Run; -import hudson.plugins.git.BranchSpec; -import hudson.plugins.git.GitSCM; - -/** - * Integration tests for {@link GitSCMChecksContext}. - */ -public class GitSCMChecksContextITest extends IntegrationTestWithJenkinsPerSuite { - private static final String EXISTING_HASH = "4ecc8623b06d99d5f029b66927438554fdd6a467"; - private static final String HTTP_URL = "https://github.com/jenkinsci/github-checks-plugin.git"; - private static final String CREDENTIALS_ID = "credentials"; - private static final String URL_NAME = "url"; - - /** - * Creates a FreeStyle job that uses {@link hudson.plugins.git.GitSCM} and runs a successful build. - * Then this build is used to create a new {@link GitSCMChecksContext}. So the build actually is not publishing - * the checks we just ensure that we can create the context with the successful build (otherwise we would need - * Wiremock to handle the requests to GitHub). - */ - @Test - public void shouldRetrieveContextFromFreeStyleBuild() throws IOException { - FreeStyleProject job = createFreeStyleProject(); - - BranchSpec branchSpec = new BranchSpec(EXISTING_HASH); - GitSCM scm = new GitSCM(GitSCM.createRepoList(HTTP_URL, CREDENTIALS_ID), - Collections.singletonList(branchSpec), false, Collections.emptyList(), - null, null, Collections.emptyList()); - job.setScm(scm); - - Run run = buildSuccessfully(job); - - GitSCMChecksContext gitSCMChecksContext = new GitSCMChecksContext(run, URL_NAME); - - assertThat(gitSCMChecksContext.getRepository()).isEqualTo("jenkinsci/github-checks-plugin"); - assertThat(gitSCMChecksContext.getHeadSha()).isEqualTo(EXISTING_HASH); - assertThat(gitSCMChecksContext.getCredentialsId()).isEqualTo(CREDENTIALS_ID); - } - - /** - * Creates a pipeline that uses {@link hudson.plugins.git.GitSCM} and runs a successful build. - * Then this build is used to create a new {@link GitSCMChecksContext}. - */ - @Test - public void shouldRetrieveContextFromPipeline() { - WorkflowJob job = createPipeline(); - - job.setDefinition(new CpsFlowDefinition("node {\n" - + " stage ('Checkout') {\n" - + " checkout scm: ([\n" - + " $class: 'GitSCM',\n" - + " userRemoteConfigs: [[credentialsId: '" + CREDENTIALS_ID + "', url: '" + HTTP_URL + "']],\n" - + " branches: [[name: '" + EXISTING_HASH + "']]\n" - + " ])" - + " }\n" - + "}\n", true)); - - Run run = buildSuccessfully(job); - - GitSCMChecksContext gitSCMChecksContext = new GitSCMChecksContext(run, URL_NAME); - - assertThat(gitSCMChecksContext.getRepository()).isEqualTo("jenkinsci/github-checks-plugin"); - assertThat(gitSCMChecksContext.getCredentialsId()).isEqualTo(CREDENTIALS_ID); - assertThat(gitSCMChecksContext.getHeadSha()).isEqualTo(EXISTING_HASH); - } -} diff --git a/src/test/java/io/jenkins/plugins/checks/github/config/GitHubChecksConfigITest.java b/src/test/java/io/jenkins/plugins/checks/github/config/GitHubChecksConfigITest.java deleted file mode 100644 index a114bfb5..00000000 --- a/src/test/java/io/jenkins/plugins/checks/github/config/GitHubChecksConfigITest.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.jenkins.plugins.checks.github.config; - -import hudson.util.StreamTaskListener; -import io.jenkins.plugins.checks.github.GitHubChecksPublisherFactory; -import io.jenkins.plugins.util.IntegrationTestWithJenkinsPerSuite; -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import static org.assertj.core.api.Assertions.assertThat; - -/** - * Integration test for {@link GitHubChecksConfig}. - */ -public class GitHubChecksConfigITest extends IntegrationTestWithJenkinsPerSuite { - /** - * When a job has not {@link org.jenkinsci.plugins.github_branch_source.GitHubSCMSource} or - * {@link hudson.plugins.git.GitSCM}, the default config should be used and no verbose log should be output. - */ - @Test - public void shouldUseDefaultConfigWhenNoSCM() { - ByteArrayOutputStream os = new ByteArrayOutputStream(); - GitHubChecksPublisherFactory.fromJob(createFreeStyleProject(), new StreamTaskListener(os)); - - assertThat(os.toString()).doesNotContain("Causes for no suitable publisher found: "); - } -} \ No newline at end of file diff --git a/src/test/java/io/jenkins/plugins/checks/github/status/GitHubSCMSourceStatusChecksTraitTest.java b/src/test/java/io/jenkins/plugins/checks/github/status/GitHubSCMSourceStatusChecksTraitTest.java deleted file mode 100644 index bcea3ad4..00000000 --- a/src/test/java/io/jenkins/plugins/checks/github/status/GitHubSCMSourceStatusChecksTraitTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.jenkins.plugins.checks.github.status; - -import jenkins.scm.api.SCMHeadObserver; -import jenkins.scm.api.SCMSourceCriteria; -import org.jenkinsci.plugins.github_branch_source.GitHubSCMSourceContext; -import org.junit.jupiter.api.Test; - -import static io.jenkins.plugins.util.assertions.Assertions.assertThat; -import static org.mockito.Mockito.mock; - -class GitHubSCMSourceStatusChecksTraitTest { - @Test - void shouldOnlyApplyTraitConfigurationsToGitHubBranchSourceNotificationsWhenItsNotDisabled() { - GitHubSCMSourceContext context = new GitHubSCMSourceContext(mock(SCMSourceCriteria.class), - mock(SCMHeadObserver.class)); - GitHubSCMSourceStatusChecksTrait trait = new GitHubSCMSourceStatusChecksTrait(); - - // disable notifications, the trait configuration should be ignored - context.withNotificationsDisabled(true); - trait.setSkipNotifications(false); - trait.decorateContext(context); - assertThat(context.notificationsDisabled()).isTrue(); - - // enable notifications, the trait configuration should be applied - context.withNotificationsDisabled(false); - trait.setSkipNotifications(true); - trait.decorateContext(context); - assertThat(context.notificationsDisabled()).isTrue(); - } -}