Skip to content

Commit

Permalink
Merge remote-tracking branch 'original/azz-bitbucket-server' into fil…
Browse files Browse the repository at this point in the history
…e_line_api

# Conflicts:
#	source/platforms/GitHub.ts
#	source/runner/Executor.ts
  • Loading branch information
sunshinejr committed Mar 4, 2018
2 parents 8e433b6 + 491fd89 commit 57a5db5
Show file tree
Hide file tree
Showing 36 changed files with 4,076 additions and 85 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
## Master

* Update the PR DSL to include bots. [@orta][]
* Add BitBucket server support. [@azz][]

## 3.1.7

Expand Down Expand Up @@ -906,3 +907,4 @@ 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
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
15 changes: 14 additions & 1 deletion source/ci_source/ci_source_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Env, RepoMetaData } 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"

/**
* Validates that all ENV keys exist and have a length
Expand Down Expand Up @@ -41,13 +45,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
8 changes: 0 additions & 8 deletions source/ci_source/providers/Nevercode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,4 @@ export class Nevercode implements CISource {
get ciRunURL() {
return process.env.NEVERCODE_BUILD_URL
}

private get branchName(): string {
if (this.isPR) {
return this.env.NEVERCODE_PULL_REQUEST_SOURCE
} else {
return this.env.NEVERCODE_BRANCH
}
}
}
19 changes: 6 additions & 13 deletions source/commands/danger-pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import * as debug from "debug"
import * as jsome from "jsome"

import { FakeCI } from "../ci_source/providers/Fake"
import { GitHub } from "../platforms/GitHub"
import { GitHubAPI } from "../platforms/github/GitHubAPI"
import { pullRequestParser } from "../platforms/github/pullRequestParser"
import { pullRequestParser } from "../platforms/pullRequestParser"
import { dangerfilePath } from "./utils/file-utils"
import validateDangerfileExists from "./utils/validateDangerfileExists"
import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs"
import { jsonDSLGenerator } from "../runner/dslGenerator"
import { prepareDangerDSL } from "./utils/runDangerSubprocess"
import { runRunner } from "./ci/runner"
import { Platform, getPlatformForEnv } from "../platforms/platform"

// yarn build; cat source/_tests/fixtures/danger-js-pr-384.json | node --inspect --inspect-brk distribution/commands/danger-runner.js --text-only

Expand All @@ -35,7 +34,7 @@ program
.on("--help", () => {
log("\n")
log(" Docs:")
if (!process.env["DANGER_GITHUB_API_TOKEN"]) {
if (!process.env["DANGER_GITHUB_API_TOKEN"] && !process.env["DANGER_BITBUCKETSERVER_HOST"]) {
log("")
log(" You don't have a DANGER_GITHUB_API_TOKEN set up, this is optional, but TBH, you want to do this.")
log(" Check out: http://danger.systems/js/guides/the_dangerfile.html#working-on-your-dangerfile")
Expand Down Expand Up @@ -71,19 +70,13 @@ if (program.args.length === 0) {
// TODO: Use custom `fetch` in GitHub that stores and uses local cache if PR is closed, these PRs
// shouldn't change often and there is a limit on API calls per hour.

const token = process.env["DANGER_GITHUB_API_TOKEN"]
if (!token) {
console.log("You don't have a DANGER_GITHUB_API_TOKEN set up, this is optional, but TBH, you want to do this")
console.log("Check out: http://danger.systems/js/guides/the_dangerfile.html#working-on-your-dangerfile")
}

console.log(`Starting Danger PR on ${pr.repo}#${pr.pullRequestNumber}`)

if (validateDangerfileExists(dangerFile)) {
d(`executing dangerfile at ${dangerFile}`)
const source = new FakeCI({ DANGER_TEST_REPO: pr.repo, DANGER_TEST_PR: pr.pullRequestNumber })
const api = new GitHubAPI(source, token)
const platform = new GitHub(api)
const platform = getPlatformForEnv(process.env, source, /* requireAuth */ false)

if (app.json || app.js) {
d("getting just the JSON/JS DSL")
runHalfProcessJSON(platform)
Expand All @@ -102,7 +95,7 @@ if (program.args.length === 0) {
}

// Run the first part of a Danger Process and output the JSON to CLI
async function runHalfProcessJSON(platform: GitHub) {
async function runHalfProcessJSON(platform: Platform) {
const dangerDSL = await jsonDSLGenerator(platform)
const processInput = prepareDangerDSL(dangerDSL)
const output = JSON.parse(processInput)
Expand Down
2 changes: 1 addition & 1 deletion source/commands/danger-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ getRuntimeCISource(app).then(source => {
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, if you want other platforms, consider the Ruby version or help out.`
`Currently Danger JS only supports GitHub and BitBucket Server, if you want other platforms, consider the Ruby version or help out.`
)
process.exitCode = 1
}
Expand Down

0 comments on commit 57a5db5

Please sign in to comment.