Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't fail if there is no draft release #413

Merged
merged 7 commits into from Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 39 additions & 2 deletions test/groovy/BuildDockerAndPublishImageStepTests.groovy
Expand Up @@ -35,7 +35,7 @@ class BuildDockerAndPublishImageStepTests extends BaseTest {
@Rule
public ExpectedException thrown = ExpectedException.none()

String shellMock(String command) {
String shellMock(String command, Boolean failing = false) {
switch (command) {
case {command.contains('git tag --list')}:
return defaultGitTag
Expand All @@ -51,6 +51,10 @@ class BuildDockerAndPublishImageStepTests extends BaseTest {
break
case {command.contains('gh api ${GH_RELEASES_API_URI}')}:
case {command.contains('gh api $env:GH_RELEASES_API_URI')}:
if (failing) {
// No draft release found
return ''
}
return defaultReleaseId
break
default:
Expand Down Expand Up @@ -155,7 +159,7 @@ class BuildDockerAndPublishImageStepTests extends BaseTest {
return assertMethodCallContainsPattern('stage','GitHub Release') \
&& assertMethodCallContainsPattern('withCredentials', 'GITHUB_TOKEN') \
&& assertMethodCallContainsPattern('withCredentials', 'GITHUB_USERNAME') \
&& (assertMethodCallContainsPattern('sh', 'gh api ${GH_RELEASES_API_URI}') || assertMethodCallContainsPattern('powershell', 'gh api $env:GH_RELEASES_API_URI'))
&& (assertMethodCallContainsPattern('sh', 'gh api -X PATCH') || assertMethodCallContainsPattern('powershell', 'gh api -X PATCH'))
}

@Test
Expand Down Expand Up @@ -355,6 +359,39 @@ class BuildDockerAndPublishImageStepTests extends BaseTest {
verifyMocks()
}

@Test
void itBuildsAndDeploysButNoReleaseWhenTriggeredByTagAndSemVerEnabledButNoReleaseDrafter() throws Exception {
helper.registerAllowedMethod('sh', [Map.class], { m ->
return shellMock(m.script, true)
})
helper.registerAllowedMethod('powershell', [Map.class], { m ->
return shellMock(m.script, true)
})

def script = loadScript(scriptName)
lemeurherve marked this conversation as resolved.
Show resolved Hide resolved
mockTag()
withMocks{
script.call(testImageName, [
automaticSemanticVersioning: true,
gitCredentials: 'git-itbuildsanddeploysandreleaseswhentriggeredbytagandsemverenabled',
])
}
printCallStack()
// Then we expect a successful build
assertJobStatusSuccess()
// With the common workflow run as expected
assertTrue(assertBaseWorkflow())
assertTrue(assertMethodCallContainsPattern('node', 'docker'))
// And the deploy step called for latest
assertTrue(assertMakeDeploy("${fullTestImageName}:${defaultGitTag}"))
// And the release is created (tag triggering the build)
assertFalse(assertReleaseCreated())
// But no tag pushed
assertFalse(assertTagPushed(defaultGitTag))
// And all mocked/stubbed methods have to be called
verifyMocks()
}

@Test
void itDeploysWithCorrectNameWhenTriggeredByTagAndImagenameHasTag() throws Exception {
def script = loadScript(scriptName)
Expand Down
29 changes: 19 additions & 10 deletions vars/buildDockerAndPublishImage.groovy
Expand Up @@ -227,24 +227,33 @@ def call(String imageName, Map userConfig=[:]) {
withCredentials([
usernamePassword(credentialsId: "${finalConfig.gitCredentials}", passwordVariable: 'GITHUB_TOKEN', usernameVariable: 'GITHUB_USERNAME')
]) {
final String origin = sh(returnStdout: true, script: 'git remote get-url origin').trim() - '.git'
String origin = ''
if (isUnix()) {
origin = sh(returnStdout: true, script: 'git remote get-url origin').trim() - '.git'
} else {
origin = powershell(returnStdout: true, script: 'git remote get-url origin').trim() - '.git'
}
final String org = origin.split('/')[3]
final String repository = origin.split('/')[4]
final String ghReleasesApiUri = "/repos/${org}/${repository}/releases"
withEnv(["GH_RELEASES_API_URI=${ghReleasesApiUri}"]) {
String release = ''
if (isUnix()) {
release = sh(returnStdout: true, script: 'gh api ${GH_RELEASES_API_URI} | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\'').trim()
release = sh(returnStdout: true, script: 'gh api ${GH_RELEASES_API_URI} | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\' || echo \'\'').trim()
} else {
release = powershell(returnStdout: true, script: 'gh api $env:GH_RELEASES_API_URI | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\'').trim()
release = powershell(returnStdout: true, script: 'gh api $env:GH_RELEASES_API_URI | jq -e -r \'[ .[] | select(.draft == true and .name == "next").id] | max\' || echo \'\'').trim()
}
withEnv(["GH_NEXT_RELEASE_URI=${ghReleasesApiUri}/${release}"]) {
if (isUnix()) {
sh 'gh api -X PATCH -F draft=false -F name="${TAG_NAME}" -F tag_name="${TAG_NAME}" "${GH_NEXT_RELEASE_URI}"'
} else {
powershell 'gh api -X PATCH -F draft=false -F name="$env:TAG_NAME" -F tag_name="$env:TAG_NAME" "$env:GH_NEXT_RELEASE_URI"'
}
} // withEnv
if (release) {
withEnv(["GH_NEXT_RELEASE_URI=${ghReleasesApiUri}/${release}"]) {
if (isUnix()) {
sh 'gh api -X PATCH -F draft=false -F name="${TAG_NAME}" -F tag_name="${TAG_NAME}" "${GH_NEXT_RELEASE_URI}"'
lemeurherve marked this conversation as resolved.
Show resolved Hide resolved
} else {
powershell 'gh api -X PATCH -F draft=false -F name="$env:TAG_NAME" -F tag_name="$env:TAG_NAME" "$env:GH_NEXT_RELEASE_URI"'
}
} // withEnv
} else {
echo "No draft release found for ${org}/${repository}"
} // if
} // withEnv
} // withCredentials
} // stage
Expand Down