diff --git a/README.md b/README.md index c5217f4..50ea51c 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,7 @@ via [@tupilabs](https://twitter.com/tupilabs) ### Release 4.0 (????-??-??) 1. [JENKINS-64023](https://issues.jenkins-ci.org/browse/JENKINS-64023) - org.apache.commons.jelly.JellyTagException: jar:file:/var/lib/jenkins/plugins/testlink/WEB-INF/lib/testlink.jar!/hudson/plugins/testlink/TestLinkResult/index.jelly:6:57: No page found 'sidepanel.jelly' for class hudson.plugins.testlink.TestLinkResult +2. Added Credentials Plugin, and Plain Credentials Plugin as dependency, storing devKey in a string credential now, instead of using plain text ### Release 3.16 (2019-02-07) diff --git a/pom.xml b/pom.xml index 1d7fef2..22e75bf 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 3.0 + 4.12 @@ -25,10 +25,12 @@ UTF-8 - 2.60.3 + 2.249.3 8 - false + false + Max + Medium @@ -251,12 +253,10 @@ commons-io commons-io - 2.6 commons-codec commons-codec - 1.11 xml-apis @@ -265,18 +265,6 @@ jar compile - - org.hamcrest - hamcrest-core - 1.3 - test - - - org.hamcrest - hamcrest-library - 1.3 - test - org.mockito mockito-all @@ -293,6 +281,16 @@ junit 1.11 + + org.jenkins-ci.plugins + credentials + 2.3.13 + + + org.jenkins-ci.plugins + plain-credentials + 1.7 + diff --git a/src/main/java/hudson/plugins/testlink/TestLinkBuilder.java b/src/main/java/hudson/plugins/testlink/TestLinkBuilder.java index 1e721fc..65d7ab3 100644 --- a/src/main/java/hudson/plugins/testlink/TestLinkBuilder.java +++ b/src/main/java/hudson/plugins/testlink/TestLinkBuilder.java @@ -29,12 +29,18 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.lang3.StringUtils; +import org.jenkinsci.plugins.plaincredentials.StringCredentials; import org.kohsuke.stapler.DataBoundConstructor; +import com.cloudbees.plugins.credentials.CredentialsMatcher; +import com.cloudbees.plugins.credentials.CredentialsMatchers; +import com.cloudbees.plugins.credentials.CredentialsProvider; + import br.eti.kinoshita.testlinkjavaapi.TestLinkAPI; import br.eti.kinoshita.testlinkjavaapi.constants.ExecutionStatus; import br.eti.kinoshita.testlinkjavaapi.model.Build; @@ -57,8 +63,10 @@ import hudson.plugins.testlink.result.TestCaseWrapper; import hudson.plugins.testlink.util.Messages; import hudson.plugins.testlink.util.TestLinkHelper; +import hudson.security.ACL; import hudson.tasks.BuildStep; import hudson.tasks.Builder; +import jenkins.model.Jenkins; /** * A builder to add a TestLink build step. @@ -94,6 +102,19 @@ public TestLinkBuilder(String testLinkName, String testProjectName, failIfNoResults, failOnNotRun, resultSeekers); } + /** + * Get the credential ID. + * @param credentialId credential ID + * @return either the credential with the devKey, or {@code null} + */ + private Optional getCredential(String credentialId) { + final List lookupCredentials = CredentialsProvider.lookupCredentials( + StringCredentials.class, Jenkins.getInstance(), ACL.SYSTEM, null, null); + CredentialsMatcher credentialsMatcher = CredentialsMatchers.withId(credentialId); + return Optional.of(CredentialsMatchers.firstOrNull(lookupCredentials, + credentialsMatcher)); + } + /** * Called when the job is executed. */ @@ -113,12 +134,18 @@ public boolean perform(AbstractBuild build, Launcher launcher, throw new AbortException(Messages.TestLinkBuilder_InvalidTLAPI()); } + final String credentialsId = installation.getCredentialsId(); + final String testLinkDevKey = this + .getCredential(credentialsId) + .orElseThrow(() -> new AbortException("Invalid credential ID " + credentialsId)) + .getSecret() + .getPlainText(); + TestLinkHelper.setTestLinkJavaAPIProperties(installation.getTestLinkJavaAPIProperties(), listener); final TestLinkSite testLinkSite; final TestCaseWrapper[] automatedTestCases; final String testLinkUrl = installation.getUrl(); - final String testLinkDevKey = installation.getDevKey(); TestPlan testPlan; listener.getLogger().println(Messages.TestLinkBuilder_UsedTLURL(testLinkUrl)); diff --git a/src/main/java/hudson/plugins/testlink/TestLinkInstallation.java b/src/main/java/hudson/plugins/testlink/TestLinkInstallation.java index 48fef52..526ce0c 100644 --- a/src/main/java/hudson/plugins/testlink/TestLinkInstallation.java +++ b/src/main/java/hudson/plugins/testlink/TestLinkInstallation.java @@ -24,9 +24,19 @@ package hudson.plugins.testlink; import java.io.Serializable; +import java.util.List; +import org.jenkinsci.plugins.plaincredentials.StringCredentials; import org.kohsuke.stapler.DataBoundConstructor; +import com.cloudbees.plugins.credentials.CredentialsMatcher; +import com.cloudbees.plugins.credentials.CredentialsMatchers; +import com.cloudbees.plugins.credentials.CredentialsProvider; + +import hudson.security.ACL; +import hudson.util.Secret; +import jenkins.model.Jenkins; + /** * Represents the TestLink installation in Hudson global configuration. * @@ -48,9 +58,9 @@ public class TestLinkInstallation implements Serializable { private String url; /** - * A valid user dev key + * Jenkins credentials ID (must contain the API Key) */ - private String devKey; + private String credentialsId; /** * TestLink Java API properties @@ -58,10 +68,10 @@ public class TestLinkInstallation implements Serializable { private String testLinkJavaAPIProperties; @DataBoundConstructor - public TestLinkInstallation(String name, String url, String devKey, String testLinkJavaAPIProperties) { + public TestLinkInstallation(String name, String url, String credentialsId, String testLinkJavaAPIProperties) { this.name = name; this.url = url; - this.devKey = devKey; + this.credentialsId = credentialsId; this.testLinkJavaAPIProperties = testLinkJavaAPIProperties; } @@ -73,8 +83,21 @@ public String getUrl() { return this.url; } + public String getCredentialsId() { + return credentialsId; + } + public String getDevKey() { - return this.devKey; + List lookupCredentials = CredentialsProvider.lookupCredentials( + StringCredentials.class, Jenkins.getInstance(), ACL.SYSTEM, null, null); + CredentialsMatcher credentialsMatcher = CredentialsMatchers.withId(credentialsId); + StringCredentials devKeyCredential = CredentialsMatchers.firstOrNull(lookupCredentials, + credentialsMatcher); + if (devKeyCredential != null) { + final Secret secret = devKeyCredential.getSecret(); + return secret.getPlainText(); + } + return null; } public String getTestLinkJavaAPIProperties() { diff --git a/src/main/resources/hudson/plugins/testlink/TestLinkBuilder/global.jelly b/src/main/resources/hudson/plugins/testlink/TestLinkBuilder/global.jelly index 73c5f2d..4ec4622 100644 --- a/src/main/resources/hudson/plugins/testlink/TestLinkBuilder/global.jelly +++ b/src/main/resources/hudson/plugins/testlink/TestLinkBuilder/global.jelly @@ -1,5 +1,5 @@ - + - - + + diff --git a/src/test/java/hudson/plugins/testlink/TestTestLinkBuilderInstallation.java b/src/test/java/hudson/plugins/testlink/TestTestLinkBuilderInstallation.java index 74e982c..43b1014 100644 --- a/src/test/java/hudson/plugins/testlink/TestTestLinkBuilderInstallation.java +++ b/src/test/java/hudson/plugins/testlink/TestTestLinkBuilderInstallation.java @@ -24,38 +24,62 @@ package hudson.plugins.testlink; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import java.io.IOException; + +import org.jenkinsci.plugins.plaincredentials.StringCredentials; +import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl; +import org.junit.Rule; import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +import com.cloudbees.plugins.credentials.CredentialsProvider; +import com.cloudbees.plugins.credentials.CredentialsScope; +import com.cloudbees.plugins.credentials.CredentialsStore; +import com.cloudbees.plugins.credentials.domains.Domain; + +import hudson.util.Secret; /** * Tests the TestLinkBuilderInstallation class. - * + * * @see {@link TestLinkInstallation} - * + * * @author Bruno P. Kinoshita - http://www.kinoshita.eti.br * @since 2.1 */ public class TestTestLinkBuilderInstallation { + @Rule + public JenkinsRule r = new JenkinsRule(); + /** * Tests with a TestLinkBuilderInstallation object. + * @throws IOException when adding a credentials, if it fails */ @Test - public void testInstallation() + public void testInstallation() throws IOException { - TestLinkInstallation inst = + final String credentialsId = "tl-test-api-key"; + final CredentialsStore store = CredentialsProvider.lookupStores(r.getInstance()).iterator().next(); + final Secret secret = Secret.fromString("068848"); + final StringCredentials credentials = new StringCredentialsImpl( + CredentialsScope.GLOBAL, + credentialsId, + "Test devKey used for unit tests", + secret); + store.addCredentials(Domain.global(), credentials); + final TestLinkInstallation inst = new TestLinkInstallation( "TestLink 1.9.1", "http://localhost/testlink-1.9.1/lib/api/xml-rpc.php", - "068848", ""); - - assertNotNull( inst ); - - assertEquals( inst.getName(), "TestLink 1.9.1" ); - assertEquals( inst.getUrl(), "http://localhost/testlink-1.9.1/lib/api/xml-rpc.php" ); - assertEquals( inst.getDevKey(), "068848" ); + credentialsId, + ""); + + assertEquals(inst.getName(), "TestLink 1.9.1"); + assertEquals(inst.getUrl(), "http://localhost/testlink-1.9.1/lib/api/xml-rpc.php"); + assertEquals(inst.getDevKey(), "068848"); } - + }