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

Handle more events than just pull_request #940

Merged
merged 1 commit into from
Nov 4, 2019
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: 12 additions & 7 deletions source/ci_source/providers/GitHubActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ import { readFileSync, existsSync } from "fs"
export class GitHubActions implements CISource {
private event: any

constructor(private readonly env: Env) {
constructor(private readonly env: Env, event: any = undefined) {
orta marked this conversation as resolved.
Show resolved Hide resolved
const { GITHUB_EVENT_PATH } = env
const eventFilePath = GITHUB_EVENT_PATH || "/github/workflow/event.json"

if (existsSync(eventFilePath)) {

if (event !== undefined) {
this.event = event
} else if (existsSync(eventFilePath)) {
const event = readFileSync(eventFilePath, "utf8")
this.event = JSON.parse(event)
}
Expand All @@ -161,21 +163,24 @@ export class GitHubActions implements CISource {
}

get useEventDSL() {
// Support event based PR runs
return this.env.GITHUB_EVENT_NAME !== "pull_request"
return this.event.pull_request !== undefined || this.event.issue !== undefined
}

get pullRequestID(): string {
if (this.env.GITHUB_EVENT_NAME === "pull_request") {
if (this.event.pull_request !== undefined) {
return this.event.pull_request.number
} else if (this.event.issue !== undefined) {
return this.event.issue.number
Copy link
Member Author

@f-meloni f-meloni Nov 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is a little bit controversial, I've added it because technically the PR is a particular type of issue, and then I was thinking to someone that wants to run danger when a pr comment is added, and in that case I think he would have to use issue_comment, pull_request_review and pull_request_review_comment triggers.
(An example of that could be verify that there are no open tasks on the comments
like

  • Remove this commented code

)
In issue_comment there is no pull request JSON, only the issue one https://developer.github.com/v3/activity/events/types/#webhook-payload-example-14

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to remove it if is a bad idea

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to seeing where this goes 👍

}

throw new Error("pullRequestID was called on GitHubActions when it wasn't a PR")
}

get repoSlug(): string {
if (this.env.GITHUB_EVENT_NAME === "pull_request") {
if (this.event.pull_request !== undefined) {
return this.event.pull_request.base.repo.full_name
} else if (this.event.repo !== undefined) {
return this.event.repo.full_name
}

throw new Error("repoSlug was called on GitHubActions when it wasn't a PR")
Expand Down
78 changes: 78 additions & 0 deletions source/ci_source/providers/_tests/_gitHubActions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { GitHubActions } from "../GitHubActions"

const pullRequestEvent = {
pull_request: {
number: "2",
base: {
repo: {
full_name: "danger/danger-js",
},
},
},
}

const issueEvent = {
issue: {
number: "2",
},
repo: {
full_name: "danger/danger-js",
},
}

describe("use event DSL", () => {
it("returns true when event.json contains pull_request data", () => {
const ci = new GitHubActions({}, pullRequestEvent)
expect(ci.useEventDSL).toBeTruthy()
})

it("returns true when event.json contains issue data", () => {
const ci = new GitHubActions({}, issueEvent)
expect(ci.useEventDSL).toBeTruthy()
})

it("returns false when event.json doesn't contain issue or pull_request data", () => {
const ci = new GitHubActions({}, {})
expect(ci.useEventDSL).toBeFalsy()
})
})

describe("pullRequestID", () => {
it("returns the correct id when event.json contains pull_request data", () => {
const ci = new GitHubActions({}, pullRequestEvent)
expect(ci.pullRequestID).toEqual("2")
})

it("returns the correct id when event.json contains issue data", () => {
const ci = new GitHubActions({}, issueEvent)
expect(ci.pullRequestID).toEqual("2")
})

it("throws an error when event.json doesn't contain issue or pull_request data", () => {
const ci = new GitHubActions({}, {})
expect(() => {
// tslint:disable-next-line:no-unused-expression
ci.pullRequestID
}).toThrow()
})
})

describe("repoSlug", () => {
it("returns the correct id when event.json contains pull_request data", () => {
const ci = new GitHubActions({}, pullRequestEvent)
expect(ci.repoSlug).toEqual("danger/danger-js")
})

it("returns the correct id when event.json contains issue data", () => {
const ci = new GitHubActions({}, issueEvent)
expect(ci.repoSlug).toEqual("danger/danger-js")
})

it("throws an error when event.json doesn't contain issue or pull_request data", () => {
const ci = new GitHubActions({}, {})
expect(() => {
// tslint:disable-next-line:no-unused-expression
ci.repoSlug
}).toThrow()
})
})