Skip to content

Commit

Permalink
Merge pull request #516 from azz/bitbucket-server
Browse files Browse the repository at this point in the history
WIP: Add Bitbucket Server Support
  • Loading branch information
orta committed Mar 15, 2018
2 parents a2966c4 + 2a0451c commit 9bf089a
Show file tree
Hide file tree
Showing 62 changed files with 4,286 additions and 202 deletions.
46 changes: 43 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,47 @@

## Master

* Improvements to the Flow definition file. [@orta][]
* Improve path generator for danger-runner.
* Update the PR DSL to include bots. [@orta][]
* Add BitBucket Server support.

To use Danger JS with BitBucket Server: you'll need to create a new account for Danger to use,
then set the following environment variables on your CI:

* `DANGER_BITBUCKETSERVER_HOST` = The root URL for your server, e.g. `https://bitbucket.mycompany.com`.
* `DANGER_BITBUCKETSERVER_USERNAME` = The username for the account used to comment.
* `DANGER_BITBUCKETSERVER_PASSWORD` = The password for the account used to comment.

Then you will have a fully fleshed out `danger.bitbucket_server` object in your Dangerfile to work with,
for example:

```ts
import { danger, warn } from "danger"

if (danger.bitbucket_server.pr.title.includes("WIP")) {
warn("PR is considered WIP")
}
```

The DSL is fully fleshed out, you can see all the details inside the [Danger JS Reference][ref],
but the summary is:

```ts
danger.bitbucket_server.
/** The pull request and repository metadata */
metadata: RepoMetaData
/** The related JIRA issues */
issues: JIRAIssue[]
/** The PR metadata */
pr: BitBucketServerPRDSL
/** The commits associated with the pull request */
commits: BitBucketServerCommit[]
/** The comments on the pull request */
comments: BitBucketServerPRActivity[]
/** The activities such as OPENING, CLOSING, MERGING or UPDATING a pull request */
activities: BitBucketServerPRActivity[]
```

* [@azz][]

* Don't check for same user ID on comment when running as a GitHub App. [@tibdex][]

## 3.1.7
Expand Down Expand Up @@ -909,4 +947,6 @@ Not usable for others, only stubs of classes etc. - [@orta][]
[@wizardishungry]: https://github.com/wizardishungry
[@hongrich]: https://github.com/hongrich
[@peterjgrainger]: https://github.com/peterjgrainger
[@azz]: https://github.com/azz
[@mifi]: https://github.com/ionutmiftode
[ref]: http://danger.systems/js/reference.html
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ yarn lint
The fixers for both tslint and prettier will be applied when you commit, and on a push your code will be verified that
it compiles.

### How does Danger JS work?

Check the [architecture doc](https://github.com/danger/danger-js/blob/master/docs/architecture.md).

### What is the TODO?

Check the issues, I try and keep my short term perspective there. Long term is in the [VISION.md](VISION.md).
Expand Down
10 changes: 7 additions & 3 deletions dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import { DangerDSLType } from "./source/dsl/DangerDSL"
declare var danger: DangerDSLType
// declare var results: any
declare function warn(params: string): void
declare function fail(params: string): void
// declare function fail(params: string): void
// declare function message(params: string): void
// declare function markdown(params: string): void
// declare function schedule(promise: Promise<any | void>): void
// declare function schedule(promise: () => Promise<any | void>): void
// declare function schedule(callback: (resolve: any) => void): void

const checkREADME = async () => {
if (!danger.github) {
return
}

// Request a CHANGELOG entry if not declared #trivial
const hasChangelog = danger.git.modified_files.includes("CHANGELOG.md")
const isTrivial = (danger.github.pr.body + danger.github.pr.title).includes("#trivial")
const isGreenkeeper = danger.github.pr.user.login === "greenkeeper"
const isUser = danger.github!.pr.user.type === "User"

// Politely ask for their name on the entry too
if (!hasChangelog && !isTrivial && !isGreenkeeper) {
if (!hasChangelog && !isTrivial && !isUser) {
const changelogDiff = await danger.git.diffForFile("CHANGELOG.md")
const contributorName = danger.github.pr.user.login
if (changelogDiff && changelogDiff.diff.includes(contributorName)) {
Expand Down
35 changes: 34 additions & 1 deletion docs/guides/faq.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ blurb: Common questions that come up in our GitHub issues.

## Can Danger comment inside a file on an PR?

Not yet, but there is a lot of discussion on [danger-js#77][77].
Not yet, but there is a lot of discussion on [danger-js#77][77] and a [WIP PR here][529].

## Can I use the same Dangerfile across many repos?

Expand All @@ -24,6 +24,38 @@ Let's say you run Danger on the same CI service that deploys your code. If that'

This ensures that Danger only runs when you have the environment variables set up to run. This is how Danger works for a lot of the open source mobile projects in Artsy.

## Danger is not posting to GitHub PRs, but everything looks fine?

Try logging in to the GitHub account that should be writing the messages, it's possible that your account has triggered the bot detection algorithm on GitHub. This means that messages are sent correctly, but do not show up for anyone except the sender. This makes it more or less impossible to detect from Danger's side.

## I'm not sure what Danger is doing

If you run danger with `DEBUG="*"` prefixed, you'll get a lot of information about what's happening under the hood. E.g:

```sh
DEBUG="*" DANGER_GITHUB_API_TOKEN=[123] yarn danger pr https://github.com/facebook/react/pull/11865
```

or on the CI:

```sh
DEBUG="*" yarn danger ci
```

This will print out a _lot_ of information.

## Circle CI doesnt run my build consistently

Yeah... We're struggling with that one. It's something we keep taking stabs at improving, so [keep an eye on the issues][circle_issues]. Ideally this issue will get resolved and we'll get it [fixed for free][circle_pr].

What happens is that Circle triggers a CI build before the PR has been set up, and so Danger cannot get information
about the corresponding repo and PR. Danger on Circle with use the Circle API to try and hook itself up to the right PR, so if you have `yarn danger ci` later on in the process, you'll have a better chance of them hooking up.

This can be worked around by sending PRs from forks.

[circle_issues]: https://github.com/danger/danger-js/search?q=circle&state=open&type=Issues&utf8=✓
[circle_pr]: https://discuss.circleci.com/t/pull-requests-not-triggering-build/1213

## I want to help influence Danger's direction

We'd recommend first becoming acquainted with the [VISION.md][] inside Danger, this is the long-term plan. Then there are two ways to start contributing today:
Expand All @@ -35,6 +67,7 @@ We'd recommend first becoming acquainted with the [VISION.md][] inside Danger, t
We keep comments in the public domain, there is a Slack, but it's very rarely used. If you're interested in joining, you can DM [orta][].

[77]: https://github.com/danger/danger-js/issues/77
[529]: https://github.com/danger/danger-js/issues/529
[vision.md]: https://github.com/danger/danger-js/blob/master/VISION.md
[open]: https://github.com/danger/danger-js/issues?q=is%3Aissue+is%3Aopen+label%3A%22Open+for+Discussion%22
[you-can-do-this]: https://github.com/danger/danger-js/issues?q=is%3Aissue+is%3Aopen+label%3A%22You+Can+Do+This%22
Expand Down
16 changes: 10 additions & 6 deletions docs/guides/the_dangerfile.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,45 @@ blurb: Step two on using Danger in your app, how to work locally and nuances aro

The Danger JS DSL is fully typed via TypeScript. These definitions are shipped with the Danger module. If your text editor supports working with type definitions you will get inline-documentation and auto-completion after you import danger in your Dangerfile. Visual Studios Code will do this by default for you.

If you are using Babel in your project, your Dangerfile will use the same transpilation settings. If you're using TypeScript + Jest it will work out of the box too, however, if you don't, you should head over to the [transpilation guide][transpilation_guide]
If you are using Babel in your project, your Dangerfile will use the same transpilation settings. If you're using TypeScript + Jest it will work out of the box too, however, if you don't, you should head over to the [transpilation guide][transpilation_guide].

## Working on your Dangerfile

There are two ways to locally work on your Dangerfile. These both rely on using the GitHub API locally, so you may hit the GitHub API rate-limit or need to have authenticated request for private repos. In which case you can use an access token to do authenticated requests by exposing a token to Danger.

```sh
export DANGER_GITHUB_API_TOKEN='xxxxxxxxxx'
export DANGER_GITHUB_API_TOKEN='xxxx'

# or for BitBucket
export DANGER_BITBUCKETSERVER_HOST='xxxx' DANGER_BITBUCKETSERVER_USERNAME='yyyy' DANGER_BITBUCKETSERVER_PASSWORD='zzzz'
```

Then the danger CLI will use these authenticated API calls.
Then the danger CLI will use authenticated API calls, which don't get this by API limits.

### Using `danger pr`

The command `danger pr` expects an argument of a PR url, e.g. `yarn danger pr https://github.com/danger/danger-js/pull/100`.

This will use your local Dangerfile against the metadata of the linked PR. Danger will then output the results into your terminal, instead of inside the PR itself.

This _will not_ post comments. It is for locally testing, see `yarn danger pr --help` for more info.

### Using `danger` and Faking being on a CI

If you create an [appropriately scoped temporary api token](http://danger.systems/guides/getting_started.html#setting-up-an-access-token) for your github account, this can be a good way to see if danger is suitable for you before integrating it into your CI system.
If you create an [appropriately scoped temporary api token](http://danger.systems/js/guides/getting_started.html#setting-up-an-access-token) for your GitHub account, this can be a good way to see if danger is suitable for you before integrating it into your CI system.

You can manually trigger danger against a pull request on the command line by setting the following environmental variables:

```bash
export DANGER_FAKE_CI="YEP"
export DANGER_GITHUB_API_TOKEN='xxxxxxxxxx' # a github api token
export DANGER_TEST_REPO='username/reponame'
```

Then you can run against a local branch that is attached to a pull-request, by running the following:

```bash
git checkout branch-for-pr-1234
DANGER_TEST_PR='1234' npm run danger
DANGER_TEST_PR='1234' yarn danger ci
```

Assuming that your local file-system matches up to that branch on GitHub, this will be a good approximation of how danger will work when you integrate it into your CI system. Note: this will leave a comment on the PR.
Expand Down
34 changes: 0 additions & 34 deletions docs/guides/troubleshooting.html.md

This file was deleted.

44 changes: 44 additions & 0 deletions docs/usage/bitbucket_server.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: Danger + BitBucket Server
subtitle: Dangerous bits
layout: guide_js
order: 4
blurb: Some examples of Danger with BitBucket Server
---

To use Danger JS with BitBucket Server: you'll need to create a new account for Danger to use,
then set the following environment variables on your CI:

* `DANGER_BITBUCKETSERVER_HOST` = The root URL for your server, e.g. `https://bitbucket.mycompany.com`.
* `DANGER_BITBUCKETSERVER_USERNAME` = The username for the account used to comment.
* `DANGER_BITBUCKETSERVER_PASSWORD` = The password for the account used to comment.

Then you will have a fully fleshed out `danger.bitbucket_server` object in your Dangerfile to work with,
for example:

```ts
import { danger, warn } from "danger"

if (danger.bitbucket_server.pr.title.includes("WIP")) {
warn("PR is considered WIP")
}
```

The DSL is fully fleshed out, you can see all the details inside the [Danger JS Reference][ref],
but the summary is:

```ts
danger.bitbucket_server.
/** The pull request and repository metadata */
metadata: RepoMetaData
/** The related JIRA issues */
issues: JIRAIssue[]
/** The PR metadata */
pr: BitBucketServerPRDSL
/** The commits associated with the pull request */
commits: BitBucketServerCommit[]
/** The comments on the pull request */
comments: BitBucketServerPRActivity[]
/** The activities such as OPENING, CLOSING, MERGING or UPDATING a pull request */
activities: BitBucketServerPRActivity[]
```
21 changes: 15 additions & 6 deletions source/ci_source/_tests/fixtures/dummy_ci.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
class DummyCI {
get name() { return "Dummy Testing CI" }
get name() {
return "Dummy Testing CI"
}

get isCI() { return false }
get isPR() { return true }
get isCI() {
return false
}
get isPR() {
return true
}

get pullRequestID() { return this.env.pr }
get repoSlug() { return this.env.repo }
get supportedPlatforms() { return ["github"] }
get pullRequestID() {
return this.env.pr
}
get repoSlug() {
return this.env.repo
}
}

module.exports = DummyCI
12 changes: 0 additions & 12 deletions source/ci_source/ci_source.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
/** A json object that represents the outer ENV */
export type Env = any

/** Key details about a repo */
export interface RepoMetaData {
/** A path like "artsy/eigen" */
repoSlug: string
/** The ID for the pull/merge request "11" */
pullRequestID: string
}

/** The shape of an object that represents an individual CI */
export interface CISource {
/** The project name, mainly for showing errors */
Expand All @@ -22,10 +14,6 @@ export interface CISource {

/** What is the reference slug for this environment? */
readonly repoSlug: string

/** What platforms can this CI communicate with? */
readonly supportedPlatforms: string[]

/** What unique id can be found for the code review platform's PR */
readonly pullRequestID: string

Expand Down
18 changes: 16 additions & 2 deletions source/ci_source/ci_source_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Env, RepoMetaData } from "./ci_source"
import { Env } from "./ci_source"
import { GitHubAPI } from "../platforms/github/GitHubAPI"
import { GitHubPRDSL } from "../dsl/GitHubDSL"
import * as find from "lodash.find"
import {
BitBucketServerAPI,
bitbucketServerRepoCredentialsFromEnv,
} from "../platforms/bitbucket_server/BitBucketServerAPI"
import { RepoMetaData } from "../dsl/BitBucketServerDSL"

/**
* Validates that all ENV keys exist and have a length
Expand Down Expand Up @@ -41,13 +46,22 @@ export function ensureEnvKeysAreInt(env: Env, keys: string[]): boolean {
}

/**
* Retrieves the current pull request open for this branch from the GitHub API
* Retrieves the current pull request open for this branch from an API
* @param {Env} env The environment
* @param {string} branch The branch to find pull requests for
* @returns {number} The pull request ID, if any. Otherwise 0 (Github starts from #1).
* If there are multiple pull requests open for a branch, returns the first.
*/
export async function getPullRequestIDForBranch(metadata: RepoMetaData, env: Env, branch: string): Promise<number> {
if (process.env["DANGER_BITBUCKETSERVER_HOST"]) {
const api = new BitBucketServerAPI(metadata, bitbucketServerRepoCredentialsFromEnv(env))
const prs = await api.getPullRequestsFromBranch(branch)
if (prs.length) {
return prs[0].id
}
return 0
}

const token = env["DANGER_GITHUB_API_TOKEN"]
if (!token) {
return 0
Expand Down
4 changes: 0 additions & 4 deletions source/ci_source/providers/Bitrise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ export class Bitrise implements CISource {
return this._parseRepoURL()
}

get supportedPlatforms(): string[] {
return ["github"]
}

get ciRunURL() {
return process.env.BITRISE_PULL_REQUEST
}
Expand Down
4 changes: 0 additions & 4 deletions source/ci_source/providers/BuddyBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,4 @@ export class BuddyBuild implements CISource {
get repoSlug(): string {
return this.env.BUDDYBUILD_REPO_SLUG
}

get supportedPlatforms(): Array<string> {
return ["github"]
}
}

0 comments on commit 9bf089a

Please sign in to comment.