diff --git a/igor-web/src/main/groovy/com/netflix/spinnaker/igor/travis/service/TravisService.groovy b/igor-web/src/main/groovy/com/netflix/spinnaker/igor/travis/service/TravisService.groovy index 41984405b..e50dae5cc 100644 --- a/igor-web/src/main/groovy/com/netflix/spinnaker/igor/travis/service/TravisService.groovy +++ b/igor-web/src/main/groovy/com/netflix/spinnaker/igor/travis/service/TravisService.groovy @@ -131,10 +131,31 @@ class TravisService implements BuildService { List getBuilds(String inputRepoSlug) { String repoSlug = cleanRepoSlug(inputRepoSlug) + String branch = branchFromRepoSlug(inputRepoSlug) + boolean tagsVirtualBranch = branchIsTagsVirtualBranch(inputRepoSlug) Builds builds = travisClient.builds(getAccessToken(), repoSlug) List list = new ArrayList() builds.builds.each { build -> - list.add getGenericBuild(build, repoSlug) + if (tagsVirtualBranch) { + Commit commit = builds.commits.find{ + it.id == build.commitId + } + if (commit?.isTag()) { + list.add(getGenericBuild(build, repoSlug)) + } + } else if (branch.length() == 0) { + list.add(getGenericBuild(build, repoSlug)) + } else { + Commit commit = builds.commits.find{ + it.id == build.commitId + } + if (!commit) { + log.info("${groupKey}:${repoSlug}:${build.number} - Could not find commit for build.") + list.add(getGenericBuild(build, repoSlug)) + } else if (commit.branch.equalsIgnoreCase(branch) && build.pullRequest == branchIsPullRequestVirtualBranch(inputRepoSlug)){ + list.add(getGenericBuild(build, repoSlug)) + } + } } return list } @@ -291,13 +312,25 @@ class TravisService implements BuildService { } } - protected String cleanRepoSlug(String inputRepoSlug) { + protected static String cleanRepoSlug(String inputRepoSlug) { def parts = inputRepoSlug.tokenize('/') return "${parts[0]}/${parts[1]}" } - protected String branchFromRepoSlug(String inputRepoSlug) { - return inputRepoSlug.tokenize('/').drop(2).join('/') - "pull_request_" + protected static String branchFromRepoSlug(String inputRepoSlug) { + String branch = extractBranchFromRepoSlug(inputRepoSlug) - ~/^pull_request_/ + return branch.equalsIgnoreCase('tags') ? '' : branch + } + + protected static boolean branchIsTagsVirtualBranch(String inputRepoSlug) { + return extractBranchFromRepoSlug(inputRepoSlug).equalsIgnoreCase("tags") + } + protected static boolean branchIsPullRequestVirtualBranch(String inputRepoSlug) { + return extractBranchFromRepoSlug(inputRepoSlug).startsWith("pull_request_") + } + + private static String extractBranchFromRepoSlug(String inputRepoSlug) { + inputRepoSlug.tokenize('/').drop(2).join('/') } private void setAccessToken() { diff --git a/igor-web/src/test/groovy/com/netflix/spinnaker/igor/travis/client/TravisClientSpec.groovy b/igor-web/src/test/groovy/com/netflix/spinnaker/igor/travis/client/TravisClientSpec.groovy index c430880b4..b86838fab 100644 --- a/igor-web/src/test/groovy/com/netflix/spinnaker/igor/travis/client/TravisClientSpec.groovy +++ b/igor-web/src/test/groovy/com/netflix/spinnaker/igor/travis/client/TravisClientSpec.groovy @@ -329,7 +329,7 @@ class TravisClientSpec extends Specification { Builds builds = client.builds("someToken", "org/repo", 39) then: - builds.commits.first().isTag() == true + builds.commits.first().isTag() } diff --git a/igor-web/src/test/groovy/com/netflix/spinnaker/igor/travis/service/TravisServiceSpec.groovy b/igor-web/src/test/groovy/com/netflix/spinnaker/igor/travis/service/TravisServiceSpec.groovy index 39bf830ff..79fd239a0 100644 --- a/igor-web/src/test/groovy/com/netflix/spinnaker/igor/travis/service/TravisServiceSpec.groovy +++ b/igor-web/src/test/groovy/com/netflix/spinnaker/igor/travis/service/TravisServiceSpec.groovy @@ -25,6 +25,7 @@ import com.netflix.spinnaker.igor.travis.client.model.Builds import com.netflix.spinnaker.igor.travis.client.model.Commit import spock.lang.Shared import spock.lang.Specification +import spock.lang.Unroll class TravisServiceSpec extends Specification{ @@ -59,34 +60,61 @@ class TravisServiceSpec extends Specification{ 1 * build.timestamp() >> 1458051084000 } + @Unroll def "cleanRepoSlug(repoSlug)"() { - when: - String repoSlug = service.cleanRepoSlug(inputRepoSlug) - - then: - repoSlug == expectedRepoSlug + expect: + service.cleanRepoSlug(inputRepoSlug) == expectedRepoSlug where: - inputRepoSlug | expectedRepoSlug - "my-org/repo" | "my-org/repo" - "my-org/repo/branch" | "my-org/repo" - "my-org/repo/branch/with/slashes" | "my-org/repo" + inputRepoSlug || expectedRepoSlug + "my-org/repo" || "my-org/repo" + "my-org/repo/branch" || "my-org/repo" + "my-org/repo/branch/with/slashes" || "my-org/repo" } + @Unroll def "branchFromRepoSlug(repoSlug)"() { - when: - String branch = service.branchFromRepoSlug(inputRepoSlug) + expect: + service.branchFromRepoSlug(inputRepoSlug) == expectedBranch - then: - branch == expectedBranch + where: + inputRepoSlug || expectedBranch + "my-org/repo" || "" + "my-org/repo/branch" || "branch" + "my-org/repo/branch/with/slashes" || "branch/with/slashes" + "m/r/some_pull_request_in_name" || "some_pull_request_in_name" + "my-org/repo/pull_request_master" || "master" + "my-org/repo/tags" || "" + } + + @Unroll + def "branchIsTagsVirtualBranch(repoSlug)"() { + expect: + service.branchIsTagsVirtualBranch(inputRepoSlug) == expectedBranch + + where: + inputRepoSlug || expectedBranch + "my-org/repo" || false + "my-org/repo/branch" || false + "my-org/repo/branch/with/slashes" || false + "my-org/repo/pull_request_master" || false + "my-org/repo/tags" || true + } + + @Unroll + def "branchIsPullRequestVirtualBranch(repoSlug)"() { + expect: + service.branchIsPullRequestVirtualBranch(inputRepoSlug) == expectedBranch where: - inputRepoSlug | expectedBranch - "my-org/repo" | "" - "my-org/repo/branch" | "branch" - "my-org/repo/branch/with/slashes" | "branch/with/slashes" - "my-org/repo/pull_request_master" | "master" + inputRepoSlug || expectedBranch + "my-org/repo" || false + "my-org/repo/branch" || false + "my-org/repo/branch/with/slashes" || false + "my-org/repo/pull_request_master" || true + "m/r/some_pull_request_in_name" || false + "my-org/repo/tags" || false } def "getCommit(repoSlug, buildNumber)"() { @@ -102,7 +130,7 @@ class TravisServiceSpec extends Specification{ Commit fetchedCommit = service.getCommit("org/repo", 38) then: - fetchedCommit.isTag() == true + fetchedCommit.isTag() 1 * client.accessToken("someToken") >> accessToken 1 * client.builds("token someToken", "org/repo", 38) >> builds 2 * builds.commits >> [commit]