Skip to content

Commit

Permalink
Support getPullRequestIDForBranch
Browse files Browse the repository at this point in the history
  • Loading branch information
azz committed Feb 25, 2018
1 parent c7608d7 commit ca4b9ba
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
13 changes: 11 additions & 2 deletions 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,15 +45,20 @@ 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"]) {
// TODO:
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"]
Expand Down
2 changes: 1 addition & 1 deletion source/dsl/BitBucketServerDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface JIRAIssue {
*/

export interface BitBucketServerPRDSL {
id: string
id: number
version: number
title: string
description: string
Expand Down
32 changes: 27 additions & 5 deletions source/platforms/bitbucket_server/BitBucketServerAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,30 @@ import {
BitBucketServerPRActivity,
} from "../../dsl/BitBucketServerDSL"

import { RepoMetaData } from "../../ci_source/ci_source"
import { RepoMetaData, Env } from "../../ci_source/ci_source"
import { dangerSignaturePostfix, dangerIDToString } from "../../runner/templates/bitbucketServerTemplate"
import { api as fetch } from "../../api/fetch"

// Note that there are parts of this class which don't seem to be
// used by Danger, they are exposed for Peril support.

export interface BitBucketRepoCredentials {
host: string
username?: string
password?: string
}

export function bitbucketServerRepoCredentialsFromEnv(env: Env): BitBucketRepoCredentials {
if (!env["DANGER_BITBUCKETSERVER_HOST"]) {
throw new Error(`DANGER_BITBUCKETSERVER_HOST is not set`)
}
return {
host: env["DANGER_BITBUCKETSERVER_HOST"],
username: env["DANGER_BITBUCKETSERVER_USERNAME"],
password: env["DANGER_BITBUCKETSERVER_PASSWORD"],
}
}

/** This represent the BitBucketServer API */

export class BitBucketServerAPI {
Expand All @@ -25,10 +42,7 @@ export class BitBucketServerAPI {

private pr: BitBucketServerPRDSL

constructor(
public readonly repoMetadata: RepoMetaData,
public readonly repoCredentials: { host: string; username?: string; password?: string }
) {
constructor(public readonly repoMetadata: RepoMetaData, public readonly repoCredentials: BitBucketRepoCredentials) {
// This allows Peril to DI in a new Fetch function
// which can handle unique API edge-cases around integrations
this.fetch = fetch
Expand All @@ -39,6 +53,14 @@ export class BitBucketServerAPI {
return `rest/${service}/1.0/${repoSlug}/pull-requests/${pullRequestID}`
}

getPullRequestsFromBranch = async (branch: string): Promise<BitBucketServerPRDSL[]> => {
const { repoSlug } = this.repoMetadata
const path = `rest/api/1.0/${repoSlug}/pull-requests?at=refs/heads/${branch}&withProperties=false&withAttributes=false`
const res = await this.get(path)
throwIfNotOk(res)
return (await res.json()).values as BitBucketServerPRDSL[]
}

getPullRequestInfo = async (): Promise<BitBucketServerPRDSL> => {
if (this.pr) {
return this.pr
Expand Down
8 changes: 2 additions & 6 deletions source/platforms/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GitJSONDSL } from "../dsl/GitDSL"
import { GitHub } from "./GitHub"
import { GitHubAPI } from "./github/GitHubAPI"
import { BitBucketServer } from "./BitBucketServer"
import { BitBucketServerAPI } from "./bitbucket_server/BitBucketServerAPI"
import { BitBucketServerAPI, bitbucketServerRepoCredentialsFromEnv } from "./bitbucket_server/BitBucketServerAPI"

/** A type that represents the downloaded metadata about a code review session */
export type Metadata = any
Expand Down Expand Up @@ -61,11 +61,7 @@ export function getPlatformForEnv(env: Env, source: CISource, requireAuth = true
// BitBucket Server
const bbsHost = env["DANGER_BITBUCKETSERVER_HOST"]
if (bbsHost) {
const api = new BitBucketServerAPI(source, {
host: bbsHost,
username: env["DANGER_BITBUCKETSERVER_USERNAME"],
password: env["DANGER_BITBUCKETSERVER_PASSWORD"],
})
const api = new BitBucketServerAPI(source, bitbucketServerRepoCredentialsFromEnv(env))
const bbs = new BitBucketServer(api)
return bbs
}
Expand Down
11 changes: 5 additions & 6 deletions source/runner/jsonToDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { sentence, href } from "./DangerUtils"
import { LocalGit } from "../platforms/LocalGit"
import { GitDSL } from "../dsl/GitDSL"
import { bitBucketServerGitDSL } from "../platforms/bitbucket_server/BitBucketServerGit"
import { BitBucketServerAPI } from "../platforms/bitbucket_server/BitBucketServerAPI"
import {
BitBucketServerAPI,
bitbucketServerRepoCredentialsFromEnv,
} from "../platforms/bitbucket_server/BitBucketServerAPI"

export const jsonToDSL = async (dsl: DangerDSLJSONType): Promise<DangerDSLType> => {
const api = apiForDSL(dsl)
Expand Down Expand Up @@ -41,11 +44,7 @@ const apiForDSL = (dsl: DangerDSLJSONType): GitHubNodeAPI | BitBucketServerAPI =
if (process.env["DANGER_BITBUCKETSERVER_HOST"]) {
return new BitBucketServerAPI(
{ repoSlug: "", pullRequestID: "" },
{
host: process.env["DANGER_BITBUCKETSERVER_HOST"]!,
username: process.env["DANGER_BITBUCKETSERVER_USERNAME"],
password: process.env["DANGER_BITBUCKETSERVER_PASSWORD"],
}
bitbucketServerRepoCredentialsFromEnv(process.env)
)
}

Expand Down

0 comments on commit ca4b9ba

Please sign in to comment.