diff --git a/Jenkinsfile b/Jenkinsfile index 77c9ed7..71c719b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,4 +1,67 @@ #!/usr/bin/env groovy +@Library('github.com/zanata/zanata-pipeline-library@master') -/* `buildPlugin` step provided by: https://github.com/jenkins-infra/pipeline-library */ -buildPlugin() +/* Only keep the 10 most recent builds. */ +def projectProperties = [ + [ + $class: 'BuildDiscarderProperty', + strategy: [$class: 'LogRotator', numToKeepStr: '10'] + ], +] + +properties(projectProperties) + +/* Upto stash stage should fail fast: + * Failed and stop the build + * Yet able to create report + */ +try { + timestamps { + node { + ansicolor { + stage('Checkout') { + info.printNode() + notify.started() + + // Shallow Clone does not work with RHEL7, which use git-1.8.3 + // https://issues.jenkins-ci.org/browse/JENKINS-37229 + checkout scm + } + + // Build and Unit Tests + // The result is archived + stage('Build') { + info.printNode() + info.printEnv() + def testReports = 'target/surefire-reports/TEST-*.xml' + def hpiFiles = 'target/*.hpi' + + withEnv(["MVN_HOME=${ tool name: 'mvn', type: 'hudson.tasks.Maven$MavenInstallation' }"]) { + sh '$MVN_HOME/bin/mvn clean verify' + } + + junit allowEmptyResults: true, + keepLongStdio: false, + testDataPublishers: [[$class: 'StabilityTestDataPublisher']], + testResults: "**/${testReports}" + + // notify if compile+unit test successful + notify.testResults(null) + archive "**/${hpiFiles},**/target/site/jacoco/**" + } + + stage('Report') { + // this is not working properly yet + // step([$class: 'JacocoPublisher']) + } + } + } + } +} catch (e) { + notify.failed() + junit allowEmptyResults: true, + keepLongStdio: false, + testDataPublishers: [[$class: 'StabilityTestDataPublisher']], + testResults: "**/${testReports}" + throw e +} \ No newline at end of file diff --git a/Jenkinsfile_jenkinsci b/Jenkinsfile_jenkinsci new file mode 100644 index 0000000..77c9ed7 --- /dev/null +++ b/Jenkinsfile_jenkinsci @@ -0,0 +1,4 @@ +#!/usr/bin/env groovy + +/* `buildPlugin` step provided by: https://github.com/jenkins-infra/pipeline-library */ +buildPlugin() diff --git a/src/main/java/org/jenkinsci/plugins/zanata/ZanataCliBuilder.java b/src/main/java/org/jenkinsci/plugins/zanata/ZanataCliBuilder.java index f123352..f2df382 100644 --- a/src/main/java/org/jenkinsci/plugins/zanata/ZanataCliBuilder.java +++ b/src/main/java/org/jenkinsci/plugins/zanata/ZanataCliBuilder.java @@ -29,6 +29,7 @@ of this software and associated documentation files (the "Software"), to deal import hudson.FilePath; import hudson.Util; import hudson.model.Computer; +import hudson.model.Executor; import hudson.model.Item; import hudson.model.Job; import hudson.model.Queue; @@ -148,8 +149,9 @@ public void perform(Run build, FilePath workspace, Launcher launcher, TaskL // build.getEnvironment(listener) doesn't seem to get current node's environment. // This will work and inherit the global JAVA_HOME env - if (build.getExecutor() != null) { - Computer owner = build.getExecutor().getOwner(); + Executor executor = build.getExecutor(); + if (executor != null) { + Computer owner = executor.getOwner(); EnvVars envForNode = owner.buildEnvironment(listener); envs.putAll(envForNode); } diff --git a/src/test/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStepBasicsTest.java b/src/test/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStepBasicsTest.java index 92efc89..7726046 100644 --- a/src/test/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStepBasicsTest.java +++ b/src/test/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStepBasicsTest.java @@ -1,14 +1,19 @@ package org.jenkinsci.plugins.zanata.zanatareposync; +import static hudson.model.Item.EXTENDED_READ; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; import org.junit.Before; import org.junit.Test; import org.jvnet.hudson.test.WithoutJenkins; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import hudson.model.AbstractProject; +import hudson.model.Item; +import hudson.security.Permission; import hudson.util.FormValidation; import hudson.util.ListBoxModel; @@ -79,4 +84,36 @@ public void testPushTypeOptions() { assertThat(options.stream().map(option -> option.value)) .contains("source", "trans", "both"); } + + @Test + @WithoutJenkins + public void credentialIdIsOkWhenContextIsNotNullAndDoesNotHaveExtendedReadPermission() { + ZanataSyncStep.DescriptorImpl descriptor = + new ZanataSyncStep.DescriptorImpl(); + AbstractProject context = Mockito.mock(AbstractProject.class); + when(context.hasPermission(EXTENDED_READ)).thenReturn(false); + String url = "http://localhost"; + String credentialId = "some_id"; + assertThat(descriptor.doCheckZanataCredentialsId( + context, url, credentialId).kind) + .isEqualTo(FormValidation.Kind.OK); + } + + @Test + @WithoutJenkins + public void credentialIdIsOkWhenContextHasExtendedReadPermissionButURLOrValueIsNull() { + ZanataSyncStep.DescriptorImpl descriptor = + new ZanataSyncStep.DescriptorImpl(); + AbstractProject context = Mockito.mock(AbstractProject.class); + when(context.hasPermission(EXTENDED_READ)).thenReturn(true); + assertThat(descriptor.doCheckZanataCredentialsId( + context, null, null).kind) + .isEqualTo(FormValidation.Kind.OK); + assertThat(descriptor.doCheckZanataCredentialsId( + context, "http://localhost", + null).kind).isEqualTo(FormValidation.Kind.OK); + assertThat(descriptor.doCheckZanataCredentialsId( + context, null, + "some_id").kind).isEqualTo(FormValidation.Kind.OK); + } } diff --git a/src/test/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStepTest.java b/src/test/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStepTest.java index 74546ae..53671ea 100644 --- a/src/test/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStepTest.java +++ b/src/test/java/org/jenkinsci/plugins/zanata/zanatareposync/ZanataSyncStepTest.java @@ -1,15 +1,20 @@ package org.jenkinsci.plugins.zanata.zanatareposync; +import static hudson.model.Item.EXTENDED_READ; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; import java.io.IOException; import java.util.List; import org.junit.Test; +import org.mockito.Mockito; +import hudson.model.AbstractProject; import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; import hudson.model.Result; +import hudson.util.FormValidation; import hudson.util.ListBoxModel; /** @@ -24,6 +29,30 @@ */ public class ZanataSyncStepTest extends WithJenkins { + @Test + public void testCheckCredentialId() throws IOException { + ZanataSyncStep.DescriptorImpl descriptor = + new ZanataSyncStep.DescriptorImpl(); + AbstractProject context = Mockito.mock(AbstractProject.class); + when(context.hasPermission(EXTENDED_READ)).thenReturn(true); + + String url = "http://localhost"; + String credentialId = "some_id"; + assertThat(descriptor.doCheckZanataCredentialsId( + context, url, credentialId).kind) + .isEqualTo(FormValidation.Kind.WARNING); + + // now we insert a credential with that id + String username = "user"; + String password = "s3cr3t"; + addUsernamePasswordCredential(username, password, credentialId, + ""); + + assertThat(descriptor.doCheckZanataCredentialsId( + context, url, credentialId).kind) + .isEqualTo(FormValidation.Kind.OK); + } + @Test public void testCredentialsOptions() throws IOException { String username = "user";