Skip to content

Commit

Permalink
Use pullRequestParser in TeamCity provider
Browse files Browse the repository at this point in the history
If one uses Bitbucket Server + TeamCity, parsing of the
PULL_REQUEST_URL environment variable will be incorrect.

It seems `pullRequestParser` module already does a pretty good
job in figuring out what's what, so use it in TeamCity provider implementation
  • Loading branch information
markelog committed Jun 13, 2018
1 parent 79d1881 commit a9ac649
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
29 changes: 14 additions & 15 deletions source/ci_source/providers/TeamCity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Env, CISource } from "../ci_source"
import { ensureEnvKeysExist } from "../ci_source_helpers"
import { pullRequestParser } from "../../platforms/pullRequestParser"

/**
*
Expand Down Expand Up @@ -35,25 +36,23 @@ export class TeamCity implements CISource {
return ensureEnvKeysExist(this.env, mustHave)
}

private _prParseURL(): { owner?: string; reponame?: string; id?: string } {
const prUrl = this.env.PULL_REQUEST_URL || ""
const splitSlug = prUrl.split("/")
if (splitSlug.length === 7) {
const owner = splitSlug[3]
const reponame = splitSlug[4]
const id = splitSlug[6]
return { owner, reponame, id }
get pullRequestID(): string {
const parts = pullRequestParser(this.env.PULL_REQUEST_URL || "")

if (parts === null) {
return ""
}
return {}
}

get pullRequestID(): string {
const { id } = this._prParseURL()
return id || ""
return parts.pullRequestNumber
}

get repoSlug(): string {
const { owner, reponame } = this._prParseURL()
return owner && reponame ? `${owner}/${reponame}` : ""
const parts = pullRequestParser(this.env.PULL_REQUEST_URL || "")

if (parts === null) {
return ""
}

return parts.repo
}
}
16 changes: 16 additions & 0 deletions source/ci_source/providers/_tests/_teamcity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,27 @@ describe(".pullRequestID", () => {
})
expect(teamcity.pullRequestID).toEqual("541")
})

it("pulls it out of the env for Bitbucket Server", () => {
const teamcity = new TeamCity({
PULL_REQUEST_URL: "https://stash.test.com/projects/POR/repos/project/pull-requests/32304/overview",
})

expect(teamcity.pullRequestID).toEqual("32304")
})
})

describe(".repoSlug", () => {
it("derives it from the PR Url", () => {
const teamcity = new TeamCity(correctEnv)
expect(teamcity.repoSlug).toEqual("danger/danger-js")
})

it("derives it from the PR Url for Bitbucket Server", () => {
const teamcity = new TeamCity({
PULL_REQUEST_URL: "https://stash.test.com/projects/POR/repos/project/pull-requests/32304/overview",
})

expect(teamcity.repoSlug).toEqual("projects/POR/repos/project")
})
})

0 comments on commit a9ac649

Please sign in to comment.