Skip to content

Commit

Permalink
Filter build lists from travis (#94)
Browse files Browse the repository at this point in the history
This makes the build list from travis only show builds relevant to your choosen branch
when you trigger pipelines from deck.

* If you have choosen to listen to a specific branch, only builds of that branch shows up.
* If you have choosen to listen to the virtual branch tags, only tag builds shows up.
* If you listen to PRs against a specific branch, only PR builds shows up.
* If you listen to all builds (org/repo), all the builds show up.
  • Loading branch information
gardleopard authored and lwander committed May 17, 2016
1 parent cbd8566 commit be6fb13
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,31 @@ class TravisService implements BuildService {

List<GenericBuild> getBuilds(String inputRepoSlug) {
String repoSlug = cleanRepoSlug(inputRepoSlug)
String branch = branchFromRepoSlug(inputRepoSlug)
boolean tagsVirtualBranch = branchIsTagsVirtualBranch(inputRepoSlug)
Builds builds = travisClient.builds(getAccessToken(), repoSlug)
List<GenericBuild> list = new ArrayList<GenericBuild>()
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
}
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)"() {
Expand All @@ -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]
Expand Down

0 comments on commit be6fb13

Please sign in to comment.