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

Commit

Permalink
fixes branch name to be empty when current branch belongs to a PR (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaquimmnetto authored Jan 14, 2022
1 parent eccf330 commit 9452517
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import spock.lang.Unroll
import wooga.gradle.dotnetsonar.tasks.utils.PluginIntegrationSpec
import wooga.gradle.dotnetsonar.utils.FakeExecutable

import static wooga.gradle.dotnetsonar.utils.SpecFakes.argReflectingFakeExecutable
import static wooga.gradle.dotnetsonar.utils.SpecUtils.rootCause

class SonarScannerBeginTaskIntegrationSpec extends PluginIntegrationSpec {

@GithubRepository(
Expand All @@ -35,16 +32,6 @@ class SonarScannerBeginTaskIntegrationSpec extends PluginIntegrationSpec {
token = "${testRepo.token}"
}
"""
and: "a pull request open for this repository"
def prBranchName = "realbranch"
testRepo.createBranch(prBranchName)
testRepo.commit("commitmsg", prBranchName)
def pr = testRepo.createPullRequest("Test", prBranchName, testRepo.defaultBranch.name, "description")

and: "a setup PR git repository"
def git = Grgit.init(dir: projectDir)
git.commit(message: "any message")
git.checkout(branch: "PR-${pr.number}", createBranch: true)

when:
def result = runTasksSuccessfully("sonarScannerBegin")
Expand All @@ -54,7 +41,29 @@ class SonarScannerBeginTaskIntegrationSpec extends PluginIntegrationSpec {
def companyName = testRepo.fullName.split("/")[0]
execResults.args.contains("-n:${testRepo.name}".toString())
execResults.args.contains("-k:${companyName}_${testRepo.name}".toString())
execResults.args.contains("-d:sonar.branch.name=${prBranchName}".toString())
}

def "task sets branch property name accordingly with its PR or non-PR status"() {
given: "a sonar scanner executable"
def fakeSonarScannerExec = argReflectingFakeExecutable("sonarscanner", 0)
and: "a set up sonar scanner extension"
buildFile << forceAddObjectsToExtension(fakeSonarScannerExec)

when:
def result = runTasksSuccessfully("sonarScannerBegin")

then:
def execResults = FakeExecutable.lastExecutionResults(result)
if(expectedBranchProperty) {
execResults.args.contains("-d:sonar.branch.name=${expectedBranchProperty}".toString())
} else {
!execResults.args.contains("-d:sonar.branch.name")
}
where:
branchName | expectedBranchProperty
"name" | "name"
"PR-10" | null
"PR-fe" | "PR-fe"
}

def "task executes sonar scanner tool begin command with extension properties"() {
Expand Down Expand Up @@ -172,3 +181,6 @@ class SonarScannerBeginTaskIntegrationSpec extends PluginIntegrationSpec {
e.message.contains("exit value 1")
}
}
import static wooga.gradle.dotnetsonar.utils.SpecFakes.argReflectingFakeExecutable

import static wooga.gradle.dotnetsonar.utils.SpecUtils.rootCause
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ class DotNetSonarqubePlugin implements Plugin<Project> {
def keyProvider = companyNameProvider.map { comp ->
return repoNameProvider.map {repoName -> "${comp}_${repoName}"}.getOrNull()
}
def branchProvider = localBranchProviderWithPR(project, githubExt).map { it.trim().isEmpty() ? null : it }
//branch.name should be empty if this branch is from a PR, so that the github PR can be set up.
def branchProvider = githubExt.branchName.
map {it -> it.empty? null : it}.
map{isPR(it)? null : it}
properties.with {
property("sonar.login", System.getenv('SONAR_TOKEN'))
property("sonar.host.url", System.getenv('SONAR_HOST'))
Expand All @@ -74,36 +77,10 @@ class DotNetSonarqubePlugin implements Plugin<Project> {
}
}

static Provider<String> localBranchProviderWithPR(Project project, GithubPluginExtension githubExt) {
def clientProvider = emptyProviderForException(project, githubExt.clientProvider, UncheckedIOException)

return githubExt.branchName.map {currentBranch ->
return clientProvider.map {client ->
def repository = client.getRepository(githubExt.repositoryName.get())
if (currentBranch.toUpperCase().startsWith("PR-")) {
def maybePrNumber = currentBranch.replace("PR-", "").trim()
if (maybePrNumber.isNumber()) {
def prNumber = Integer.valueOf(maybePrNumber)
return repository.getPullRequest(prNumber).head.ref
}
return null
}
}.getOrElse(currentBranch)
}
//PR branches are identified as PR-XX where XX is any number.
static boolean isPR(String currentBranch) {
def maybePrNumber = currentBranch.replace("PR-", "").trim()
return currentBranch.toUpperCase().startsWith("PR-") && maybePrNumber.isNumber()
}

protected static <T> Provider<T> emptyProviderForException(Project project,
Provider<T> provider,
Class<? extends Throwable> exceptionClass) {
return project.provider {
try {
return provider.get()
}catch(Throwable e) {
if(exceptionClass.isInstance(e)) {
return null
}
throw e
}
}
}
}

0 comments on commit 9452517

Please sign in to comment.