Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #51 from wcm-io-devops/feature/use-branches-from-s…
Browse files Browse the repository at this point in the history
…cm-object-when-available

Use branches from scm object to avoid problems when building the pipeline library by itself
  • Loading branch information
Tobias Richter authored Feb 18, 2020
2 parents b5d3b22 + a37e98b commit 5f26928
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
9 changes: 9 additions & 0 deletions test/io/wcm/testing/jenkins/pipeline/BasicStepsMock.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ package io.wcm.testing.jenkins.pipeline

import com.lesfurets.jenkins.unit.PipelineTestHelper
import hudson.AbortException
import hudson.plugins.git.GitSCM
import io.wcm.devops.jenkins.pipeline.environment.EnvironmentConstants
import io.wcm.testing.jenkins.pipeline.recorder.StepRecorder

import static io.wcm.testing.jenkins.pipeline.StepConstants.CHECKOUT
Expand Down Expand Up @@ -54,6 +56,13 @@ class BasicStepsMock {
this.context = context

this.context.getPipelineTestHelper().registerAllowedMethod(CHECKOUT, [Map.class], { LinkedHashMap incomingCall -> this.context.getStepRecorder().record(CHECKOUT, incomingCall) })
this.context.getPipelineTestHelper().registerAllowedMethod(CHECKOUT, [GitSCM.class], { GitSCM scmObject ->
this.context.getStepRecorder().record(CHECKOUT, scmObject)
return [
(EnvironmentConstants.GIT_BRANCH): this.context.getEnvVars().getProperty(EnvironmentConstants.GIT_BRANCH),
(EnvironmentConstants.GIT_URL): this.context.getEnvVars().getProperty(EnvironmentConstants.GIT_URL)
]
})

this.context.getPipelineTestHelper().registerAllowedMethod(DIR, [String.class, Closure.class], dirCallback)

Expand Down
27 changes: 27 additions & 0 deletions test/vars/checkoutScm/CheckoutScmIT.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,23 @@
*/
package vars.checkoutScm

import hudson.plugins.git.BranchSpec
import hudson.plugins.git.GitSCM
import io.wcm.devops.jenkins.pipeline.environment.EnvironmentConstants
import io.wcm.testing.jenkins.pipeline.LibraryIntegrationTestBase
import io.wcm.devops.jenkins.pipeline.utils.ConfigConstants
import org.junit.Test
import org.mockito.Mockito
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer

import static io.wcm.testing.jenkins.pipeline.StepConstants.CHECKOUT
import static io.wcm.testing.jenkins.pipeline.recorder.StepRecorderAssert.assertNone
import static io.wcm.testing.jenkins.pipeline.recorder.StepRecorderAssert.assertOnce
import static org.junit.Assert.assertEquals
import static org.mockito.ArgumentMatchers.any
import static org.mockito.ArgumentMatchers.eq
import static org.mockito.Mockito.when

class CheckoutScmIT extends LibraryIntegrationTestBase {

Expand Down Expand Up @@ -140,5 +150,22 @@ class CheckoutScmIT extends LibraryIntegrationTestBase {
assertEquals("[email protected]/group/project1.git", userRemoteConfig.get("url"))
}

@Test
void shouldUseBranchFromScmVar() {
this.setEnv(EnvironmentConstants.GIT_BRANCH, null)
GitSCM mockedGitSCM = Mockito.mock(GitSCM.class)
List<BranchSpec> mockedBranchSpecs = []
mockedBranchSpecs.push(new BranchSpec("feature/branchName"))
this.getBinding().setProperty("scm", mockedGitSCM)

when(mockedGitSCM.getBranches()).thenReturn(mockedBranchSpecs)

Map result = loadAndExecuteScript("vars/checkoutScm/jobs/checkoutWithScmVarJob.groovy")
GitSCM actualMockedGitSCM = (GitSCM) assertOnce(CHECKOUT)

assertEquals(mockedGitSCM, actualMockedGitSCM)

assertEquals("origin/feature/branchName", this.getEnv(EnvironmentConstants.GIT_BRANCH),)
}

}
39 changes: 39 additions & 0 deletions test/vars/checkoutScm/jobs/checkoutWithScmVarJob.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*-
* #%L
* wcm.io
* %%
* Copyright (C) 2017 - 2020 wcm.io DevOps
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

package vars.checkoutScm.jobs

import static io.wcm.devops.jenkins.pipeline.utils.ConfigConstants.*

/**
* Executes a custom checkout with all supported configuration options that should not use the provided credentialsId
*
* @return The script
* @see vars.checkoutScm.CheckoutScmIT
*/
def execute() {
return checkoutScm([
(SCM): [
(SCM_USE_SCM_VAR): true
]
])
}

return this
40 changes: 40 additions & 0 deletions vars/checkoutScm.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
* limitations under the License.
* #L%
*/

import hudson.plugins.git.UserRemoteConfig
import hudson.tasks.UserAvatarResolver
import io.wcm.devops.jenkins.pipeline.credentials.Credential
import io.wcm.devops.jenkins.pipeline.credentials.CredentialConstants
import io.wcm.devops.jenkins.pipeline.credentials.CredentialParser
import io.wcm.devops.jenkins.pipeline.environment.EnvironmentConstants
import io.wcm.devops.jenkins.pipeline.environment.EnvironmentUtils
import io.wcm.devops.jenkins.pipeline.utils.ConfigConstants
import io.wcm.devops.jenkins.pipeline.utils.PatternMatcher
Expand All @@ -46,6 +50,7 @@ import org.jenkinsci.plugins.workflow.cps.DSL
* extensions: LocalBranch
*
* @param config configuration object
* @return The SCM Checkout result
*/
def call(Map config) {
Logger log = new Logger(this)
Expand Down Expand Up @@ -82,6 +87,8 @@ def call(Map config) {

// set the scm url to environment variable SCM_URL
setScmUrl(config)

return scmCheckoutResult
}

/**
Expand All @@ -90,7 +97,40 @@ def call(Map config) {
* @return result of checkout step
*/
Object checkoutWithScmVar() {
Logger log = new Logger("checkoutScm.checkoutWithScmVar")
List scmBranches = scm.getBranches()

String remoteName = "origin";
for (UserRemoteConfig remote in scm.getUserRemoteConfigs()) {
remoteName = remote.getName()
if (remoteName == null || remoteName.isEmpty()) {
remoteName = "origin";
}
}

Map result = checkout scm
String checkoutBranchName = result[EnvironmentConstants.GIT_BRANCH]

// check for a differing branch name. This may occur when the pipeline library is building itself and was checked out before by the folder shared libraries
// and is now checked out for the build
if (result && scmBranches && scmBranches.size() == 1) {
String scmBranchName = scmBranches[0]
log.debug("Branch name from scm object", scmBranchName)

// remove */ prefix
scmBranchName = scmBranchName.replace("*/","")
log.debug("Cleaned branch name", scmBranchName)

scmBranchName = remoteName + "/" + scmBranchName
log.debug("Branchname with origin", scmBranchName)
log.debug("checkoutBranchName", checkoutBranchName)

if (scmBranchName != checkoutBranchName) {
log.debug("branchName from SCM object ('$scmBranchName') does not match checkoutBranchName ('$checkoutBranchName'), apply fix")
result[EnvironmentConstants.GIT_BRANCH] = scmBranchName
}
}

return result
}

Expand Down

0 comments on commit 5f26928

Please sign in to comment.