Skip to content

Commit

Permalink
Merge branch 'master' into bitbucket-pending
Browse files Browse the repository at this point in the history
  • Loading branch information
orta authored Oct 24, 2018
2 parents b2cf4c9 + d603e31 commit c931a5e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
## Master

- Fix handling a `"pending"` status update properly using Bitbucket API - [@sgtcoolguy][]
- Fix #614 - Posting status updates to Github using issue workflow broken - [@sgtcoolguy][]

# 5.0.1, err. 6.0.0

Expand Down
10 changes: 7 additions & 3 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,21 @@ export class GitHubAPI {
return res.ok ? res.json() : { labels: [] }
}

updateStatus = async (passed: boolean, message: string, url?: string): Promise<any> => {
updateStatus = async (passed: boolean | "pending", message: string, url?: string): Promise<any> => {
const repo = this.repoMetadata.repoSlug

const prJSON = await this.getPullRequestInfo()
const ref = prJSON.head.sha
let state = passed ? "success" : "failure"
if (passed === "pending") {
state = "pending"
}
const res = await this.post(
`repos/${repo}/statuses/${ref}`,
{},
{
state: passed ? "success" : "failure",
context: process.env["PERIL_INTEGRATION_ID"] ? "Peril" : "Danger",
state: state,
context: process.env["PERIL_BOT_USER_ID"] ? "Peril" : "Danger",
target_url: url || "http://danger.systems/js",
description: message,
}
Expand Down
76 changes: 76 additions & 0 deletions source/platforms/github/_tests/_github_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,82 @@ describe("API testing", () => {
await expect(api.postInlinePRComment("", "", "", 0)).rejects.toEqual(expectedJSON)
})

it("updateStatus('pending') success", async () => {
api.fetch = jest.fn().mockReturnValue({ ok: true })
api.getPullRequestInfo = await requestWithFixturedJSON("github_pr.json")

await expect(api.updateStatus("pending", "message")).resolves.toEqual(true)
expect(api.fetch).toHaveBeenCalledWith(
"https://api.github.com/repos/artsy/emission/statuses/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75",
{
headers: {
Authorization: "token ABCDE",
"Content-Type": "application/json",
},
method: "POST",
body: '{"state":"pending","context":"Danger","target_url":"http://danger.systems/js","description":"message"}',
},
undefined
)
})

it("updateStatus(false) success", async () => {
api.fetch = jest.fn().mockReturnValue({ ok: true })
api.getPullRequestInfo = await requestWithFixturedJSON("github_pr.json")

await expect(api.updateStatus(false, "message")).resolves.toEqual(true)
expect(api.fetch).toHaveBeenCalledWith(
"https://api.github.com/repos/artsy/emission/statuses/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75",
{
headers: {
Authorization: "token ABCDE",
"Content-Type": "application/json",
},
method: "POST",
body: '{"state":"failure","context":"Danger","target_url":"http://danger.systems/js","description":"message"}',
},
undefined
)
})

it("updateStatus(true, 'message', 'http://example.org') success", async () => {
api.fetch = jest.fn().mockReturnValue({ ok: true })
api.getPullRequestInfo = await requestWithFixturedJSON("github_pr.json")

await expect(api.updateStatus(true, "message", "http://example.org")).resolves.toEqual(true)
expect(api.fetch).toHaveBeenCalledWith(
"https://api.github.com/repos/artsy/emission/statuses/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75",
{
headers: {
Authorization: "token ABCDE",
"Content-Type": "application/json",
},
method: "POST",
body: '{"state":"success","context":"Danger","target_url":"http://example.org","description":"message"}',
},
undefined
)
})

it("updateStatus(true) failed", async () => {
api.fetch = jest.fn().mockReturnValue({ ok: false })
api.getPullRequestInfo = await requestWithFixturedJSON("github_pr.json")

await expect(api.updateStatus(true, "message")).resolves.toEqual(false)
expect(api.fetch).toHaveBeenCalledWith(
"https://api.github.com/repos/artsy/emission/statuses/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75",
{
headers: {
Authorization: "token ABCDE",
"Content-Type": "application/json",
},
method: "POST",
body: '{"state":"success","context":"Danger","target_url":"http://danger.systems/js","description":"message"}',
},
undefined
)
})

it("deleteCommentWithID", async () => {
api.fetch = jest.fn().mockReturnValue({ status: 204 })
await api.deleteCommentWithID(123)
Expand Down
27 changes: 2 additions & 25 deletions source/platforms/github/comms/issueCommenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,8 @@ export const GitHubIssueCommenter = (api: GitHubAPI) => {
* Fails the current build, if status setting succeeds
* then return true.
*/

updateStatus: async (passed: boolean | "pending", message: string, url?: string): Promise<boolean> => {
const ghAPI = api.getExternalAPI()

const prJSON = await api.getPullRequestInfo()
const ref = prJSON.head
try {
await ghAPI.repos.createStatus({
repo: ref.repo.name,
owner: ref.repo.owner.login,
sha: ref.sha,
state: passed ? "success" : "failure",
context: process.env["PERIL_BOT_USER_ID"] ? "Peril" : "Danger",
target_url: url || "http://danger.systems/js",
description: message,
})
return true
} catch (error) {
// @ts-ignore
if (global.verbose) {
console.log("Got an error with creating a commit status", error)
}
return false
}
},
updateStatus: async (passed: boolean | "pending", message: string, url?: string): Promise<boolean> =>
api.updateStatus(passed, message, url),

/**
* Gets inline comments for current PR
Expand Down

0 comments on commit c931a5e

Please sign in to comment.