Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements RFC 7 #26

Merged
merged 2 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions org/all-prs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ export const rfc5 = rfc("No PR is too small to warrant a paragraph or two of sum
}
})

// https:/artsy/artsy-danger/issues/7
export const rfc7 = rfc("Hook commit contexts to GitHub PR/Issue labels", async () => {
const pr = danger.github.thisPR
const commitLabels = danger.git.commits
.map(c => c.message)
.filter(m => m.startsWith("[") && m.includes("]"))
.map(m => (m.match(/\[(.*)\]/) as any)[1]) // Guaranteed to match based on filter above.
if (commitLabels.length > 0) {
const api = danger.github.api
const githubLabels = await api.issues.getLabels({ owner: pr.owner, repo: pr.repo })
const matchingLabels = githubLabels.data
.map((l: any) => l.name)
.filter((l: string) => commitLabels.find(cl => l === cl))
if (matchingLabels.length > 0) {
await api.issues.addLabels({ owner: pr.owner, repo: pr.repo, number: pr.number, labels: matchingLabels })
}
}
})

// https:/artsy/artsy-danger/issues/13
export const rfc13 = rfc("Always ensure we assign someone, so that our Slackbot work correctly", () => {
const pr = danger.github.pr
Expand Down
85 changes: 85 additions & 0 deletions tests/rfc_7.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
jest.mock("danger", () => jest.fn())
import * as danger from "danger"
const dm = danger as any

import { rfc7 } from "../org/all-prs"

beforeEach(() => {
dm.danger = {
git: {},
github: {
api: {
issues: {
getLabels: jest.fn(),
addLabels: jest.fn()
}
},
thisPR: {
owner: 'artsy',
repo: 'eigen',
number: 1234
}
}
}
})

it("bails without commit labels", () => {
dm.danger.git.commits = [
"Implementing something",
"Adding tests",
"Changelog entry"
].map((message) => ({ message }))
return rfc7().then(() => {
expect(dm.danger.github.api.issues.getLabels).not.toHaveBeenCalled()
})
})

describe("with commit labels", () => {
beforeEach(() => {
dm.danger.git.commits = [
"[Auctions] Implementing something",
"[Auctions] Adding tests",
"[Oops] Changelog entry"
].map((message) => ({ message }))
})

it("retrieves labels from the GitHub api", () => {
dm.danger.github.api.issues.getLabels.mockImplementationOnce(() => ({ data: [] }))
return rfc7().then(() => {
expect(dm.danger.github.api.issues.getLabels).toHaveBeenCalledWith({
owner: 'artsy', repo: 'eigen'
})
})
})

describe("with no matching GitHub labels", () => {
it("does not add any labels", () => {
dm.danger.github.api.issues.getLabels.mockImplementationOnce(() => ({
data: [
{ name: "wontfix" },
{ name: "Messaging" }
]
}))
return rfc7().then(() => {
expect(dm.danger.github.api.issues.addLabels).not.toHaveBeenCalled()
})
})
})

describe("with matching GitHub labels", () => {
it("adds GitHub labels that match commit labels", () => {
dm.danger.github.api.issues.getLabels.mockImplementationOnce(() => ({
data: [
{ name: "wontfix" },
{ name: "Messaging" },
{ name: "Auctions" }
]
}))
return rfc7().then(() => {
expect(dm.danger.github.api.issues.addLabels).toHaveBeenCalledWith({
owner: 'artsy', repo: 'eigen', number: 1234, labels: ['Auctions']
})
})
})
})
})