Skip to content

Commit

Permalink
Merge branch 'master' into fix_ci
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Sep 7, 2019
2 parents 215e79d + 58ec25f commit d6873a9
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 33 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,24 @@

<!-- Your comment above this -->

- Improve docs for GitHub Actions - [@nguyenhuy]

# 9.1.8

- Get GitHub Actions event file pathname from env variable - [@IljaDaderko]

# 9.1.7

- GitHub Actions docs update - [@orta]

# 9.1.6

- Release only made for GitHub Actions - [@orta]

# 9.1.5

- Take commit hash from bitrise env - [@f-meloni]

# 9.1.4

- Use new env `BITBUCKET_REPO_FULL_NAME` in bitbucket pipeline. - [@Soyn]
Expand Down Expand Up @@ -1691,3 +1709,4 @@ Not usable for others, only stubs of classes etc. - [@orta]
[@mrndjo]: https://github.com/mrndjo
[@bigkraig]: https://github.com/bigkraig
[@notjosh]: https://github.com/notjosh
[@iljadaderko]: https://github.com/IljaDaderko
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -2,7 +2,7 @@ FROM node:10-slim

MAINTAINER Orta Therox

LABEL "com.github.actions.name"="Danger JS"
LABEL "com.github.actions.name"="Danger JS Action"
LABEL "com.github.actions.description"="Runs JavaScript/TypeScript Dangerfiles"
LABEL "com.github.actions.icon"="zap"
LABEL "com.github.actions.color"="blue"
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "danger",
"version": "9.1.5",
"version": "9.1.8",
"description": "Unit tests for Team Culture",
"main": "distribution/danger.js",
"typings": "distribution/danger.d.ts",
Expand Down
6 changes: 5 additions & 1 deletion source/ci_source/providers/Bitrise.ts
Expand Up @@ -72,6 +72,10 @@ export class Bitrise implements CISource {
}

get ciRunURL() {
return process.env.BITRISE_PULL_REQUEST
return this.env.BITRISE_PULL_REQUEST
}

get commitHash() {
return this.env.BITRISE_GIT_COMMIT
}
}
137 changes: 112 additions & 25 deletions source/ci_source/providers/GitHubActions.ts
Expand Up @@ -7,45 +7,129 @@ import { readFileSync, existsSync } from "fs"
/**
* ### CI Setup
*
* To use Danger JS with GitHub Actions, you'll need want to set up
* Danger to run on a `pull_request` webhook. To do this, you'll need
* to add/amend your repo's workflow file with something like:
* * <!-- JS --!>
* There are two ways to use Danger with GitHub Actions. If you include Danger as a dev-dependency, then
* you can call danger directly as another build-step after your tests:
*
* ```ruby
* name: Node CI
* on: [pull_request]
*
* jobs:
* test:
* runs-on: ubuntu-latest
*
* steps:
* - uses: actions/checkout@master
* - name: Use Node.js 10.x
* uses: actions/setup-node@v1
* with:
* version: 10.x
* - name: install yarn
* run: npm install -g yarn
* - name: yarn install, build, and test
* run: |
* yarn install --frozen-lockfile
* yarn build
* yarn test
* - name: Danger
* run: yarn danger ci
* env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
* ```
*
* If you are not running in a JavaScript ecosystem, or don't want to include the dependency then
* you can use Danger JS as an action.
*
* ```yml
* name: "Danger JS"
* on: [pull_request]
*
* jobs:
* build:
* name: Danger JS
* runs-on: ubuntu-latest
* steps:
* - uses: actions/checkout@v1
* - name: Danger
* uses: danger/danger-js@9.1.6
* env:
* GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
* ```
* workflow "Dangerfile JS Eval" {
* on = "pull_request"
* resolves = "Danger JS"
* }
*
* action "Danger JS" {
* uses = "danger/danger-js@master"
* secrets = ["GITHUB_TOKEN"]
* }
*
* Note it's likely the version number should change, but you get the point. This will run Danger
* self-encapsulated inside a GitHub action.
*
* <!-- !JS --!>
* <!-- Swift --!>
*
* There are two ways to use Danger with GitHub Actions. If you include Danger as a dependency, then
* you can call danger directly as another build-step after your tests:
*
* ```ruby
* name: CI
* on: [pull_request]
* jobs:
* build:
* runs-on: macos-latest
*
* steps:
* - uses: actions/checkout@master
* - name: Build
* run: swift build
*
* - name: Test
* run: swift test
*
* - name: Danger
* run: danger-swift ci
* env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
* ```
*
* If don't want to include the dependency then you can use Danger Swift via an action.
*
* ```yml
* name: "Danger Swift"
* on: [pull_request]
*
* jobs:
* build:
* name: Danger JS
* runs-on: ubuntu-latest
* steps:
* - uses: actions/checkout@v1
* - name: Danger
* uses: danger/swift@2.0.1
* env:
* GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
* ```
*
* You can pass additional CLI to Danger JS in an action via:
* Note it's likely the version number should change, but you get the point. This will run Danger
* self-encapsulated inside a GitHub action.
*
* <!-- !Swift --!>
*
* You can pass additional CLI to Danger via an action via the args:
*
* ```
* action "Danger JS" {
* [...]
* args = "--dangerfile artsy/peril-settings/org/allPRs.ts"
* }
* - uses: danger/...
* with:
* args: "--dangerfile artsy/peril-settings/org/allPRs.ts"
* ```
*
* This runs the file [`org/allPRs.ts`](https://github.com/artsy/peril-settings/blob/master/org/allPRs.ts)
* from the repo [artsy/peril-settings](https://github.com/artsy/peril-settings).
* from the repo [artsy/peril-settings](https://github.com/artsy/peril-settings). This gives you the ability
* to have Danger acting on non-pull-requests via GitHub Actions.
*
* ### Token Setup
*
* You need to make sure that the secret `"GITHUB_TOKEN"` is
* enabled in your workspace. This is so that Danger can connect
* to GitHub.
*
* ```
* action "Danger JS" {
* uses = "danger/danger-js@master"
* secrets = ["GITHUB_TOKEN"]
* }
* ```yml
* - name: Danger JS
* uses: danger/danger-js@9.1.6
* env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
* ```
*
*/
Expand All @@ -54,8 +138,11 @@ export class GitHubActions implements CISource {
private event: any

constructor(private readonly env: Env) {
if (existsSync("/github/workflow/event.json")) {
const event = readFileSync("/github/workflow/event.json", "utf8")
const { GITHUB_EVENT_PATH } = env
const eventFilePath = GITHUB_EVENT_PATH || "/github/workflow/event.json"

if (existsSync(eventFilePath)) {
const event = readFileSync(eventFilePath, "utf8")
this.event = JSON.parse(event)
}
}
Expand Down
16 changes: 16 additions & 0 deletions source/ci_source/providers/_tests/_bitrise.test.ts
Expand Up @@ -73,3 +73,19 @@ describe(".repoSlug", () => {
expect(bitrise.repoSlug).toEqual("artsy/eigen")
})
})

describe("commit hash", () => {
it("returns correct commit hash when present", () => {
const env = {
...correctEnv,
BITRISE_GIT_COMMIT: "1234abc",
}
const bitrise = new Bitrise(env)
expect(bitrise.commitHash).toEqual("1234abc")
})

it("returns no commit hash when not present", () => {
const bitrise = new Bitrise(correctEnv)
expect(bitrise.commitHash).toBeUndefined()
})
})
2 changes: 1 addition & 1 deletion source/commands/ci/runner.ts
Expand Up @@ -51,7 +51,7 @@ export const runRunner = async (app: SharedCLI, config?: Partial<RunnerConfig>)
if (!platform) {
console.log(chalk.red(`Could not find a source code hosting platform for ${source.name}.`))
console.log(
`Currently Danger JS only supports GitHub and BitBucket Server, if you want other platforms, consider the Ruby version or help add support.`
`Currently Danger JS only supports GitHub, BitBucket Server, Gitlab and Bitbucket Cloud, if you want other platforms, consider the Ruby version or help add support.`
)
process.exitCode = 1
}
Expand Down
10 changes: 6 additions & 4 deletions source/platforms/platform.ts
Expand Up @@ -141,10 +141,12 @@ export function getPlatformForEnv(env: Env, source: CISource): Platform {
// They need to set the token up for GitHub actions to work
if (env["GITHUB_EVENT_NAME"] && !env["GITHUB_TOKEN"]) {
console.error(`You need to add GITHUB_TOKEN to your Danger action in the workflow:
action "${env["GITHUB_ACTION"]}" {
${chalk.green('+ secrets = ["GITHUB_TOKEN"]"')}
}`)
- name: Danger JS
uses: danger/danger-js@X.Y.Z
${chalk.green(`env:
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}`)}
`)
}

// GitHub Platform
Expand Down

0 comments on commit d6873a9

Please sign in to comment.