Skip to content

Commit

Permalink
[WIP] Implement BitBucketCloud GIT
Browse files Browse the repository at this point in the history
  • Loading branch information
HelloCore committed Jun 23, 2019
1 parent 6d8a026 commit 0fb3e55
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 16 deletions.
105 changes: 89 additions & 16 deletions source/dsl/BitBucketCloudDSL.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import { BitBucketServerPRDSL, RepoMetaData as BitBucketServerRepoMetaData } from "./BitBucketServerDSL"
import { RepoMetaData as BitBucketServerRepoMetaData } from "./BitBucketServerDSL"

export interface BitBucketCloudJSONDSL {
/** The pull request and repository metadata */
metadata: RepoMetaData
/** The PR metadata */
pr: BitBucketCloudPRDSL
/** The commits associated with the pull request */
commits: BitBucketCloudCommit[]
/** The comments on the pull request */
comments: BitBucketCloudPRComment[]
/** The activities such as OPENING, CLOSING, MERGING or UPDATING a pull request */
activities: BitBucketCloudPRActivity[]
}

export interface BitBucketCloudDSL extends BitBucketCloudJSONDSL {}

export interface BitBucketCloudPagedResponse<T> {
pagelen: number
Expand All @@ -8,8 +23,68 @@ export interface BitBucketCloudPagedResponse<T> {
previous: string | undefined
values: T[]
}
export interface BitBucketCloudPRDSL {
/** The PR's ID */
id: number
/** Title of the pull request. */
title: string
/** The text describing the PR */
description: string
/** The pull request's current status. */
state: "OPEN" | "MERGED" | "DECLINED" | "SUPERSEDED"
/** Date PR created as number of milliseconds since the unix epoch */
created_on: number
/** Date PR updated as number of milliseconds since the unix epoch */
updated_on: number
/** The PR's source, The repo Danger is running on */
source: BitBucketCloudMergeRef
/** The PR's destination */
destination: BitBucketCloudMergeRef
/** The creator of the PR */
author: BitBucketCloudUser
/** People requested as reviewers */
reviewers: BitBucketCloudUser[]
/** People who have participated in the PR */
participants: BitBucketCloudPRParticipant[]
/** Misc links for hypermedia conformance */
links: BitBucketCloudLinks<
"decline" | "commits" | "self" | "comments" | "merge" | "html" | "activity" | "diff" | "approve" | "statuses"
>
}

export interface BitBucketCloudMergeRef {
commit: {
hash: string
}
branch: {
name: string
}
repository: BitBucketCloudRepo
}

export type BitBucketCloudLinks<Names extends string> = {
[key in Names]: {
href: string
}
}

export interface BitBucketCloudPRParticipant {
/*The user for */
user: BitBucketCloudUser

/** How did they contribute */
role: "REVIEWER" | "PARTICIPANT"

/** Did they approve of the PR? */
approved: boolean
}

export interface BitBucketCloudRepo {
name: string
full_name: string
uuid: string
}

export type BitBucketCloudPRDSL = BitBucketServerPRDSL
export type RepoMetaData = BitBucketServerRepoMetaData

export interface BitBucketCloudUser {
Expand All @@ -32,7 +107,11 @@ export interface BitBucketCloudCommit {
hash: string

/** The author of the commit, assumed to be the person who wrote the code. */
author: BitBucketCloudUser
author: {
/** Format: `Foo Bar <foo@bar.com>` */
raw: string
user: BitBucketCloudUser
}

/** When the commit was commited to the project, in ISO 8601 format */
date: string
Expand All @@ -43,19 +122,9 @@ export interface BitBucketCloudCommit {
/** The full SHA */
hash: string
}[]
}

export interface BitBucketCloudPRLink {
id: number
links: {
self: {
href: string
}
html: {
href: string
}
}
title: string
/** The commit's links */
links: BitBucketCloudLinks<"html">
}

export interface BitBucketCloudContent {
Expand All @@ -67,7 +136,11 @@ export interface BitBucketCloudContent {

export interface BitBucketCloudPRComment {
deleted: boolean
pullrequest: BitBucketCloudPRLink
pullrequest: {
id: number
links: BitBucketCloudLinks<"self" | "html">
title: string
}
content: BitBucketCloudContent

/** When the comment was created, in ISO 8601 format */
Expand Down
16 changes: 16 additions & 0 deletions source/platforms/bitbucket_cloud/BitBucketCloudAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ export class BitBucketCloudAPI {
return values
}

getFileContents = async (filePath: string, repoSlug?: string, ref?: string) => {
if (!repoSlug || !ref) {
const prJSON = await this.getPullRequestInfo()
repoSlug = prJSON.source.repository.full_name
ref = prJSON.source.commit.hash
}

const url = `${this.baseURL}/repositories/${repoSlug}/src/${ref}/${filePath}`
const res = await this.get(url, undefined, true)
if (res.status === 404) {
return ""
}
throwIfNotOk(res)
return await res.text()
}

getDangerComments = async (dangerID: string): Promise<BitBucketCloudPRComment[]> => {
const comments = await this.getPullRequestComments()
const dangerIDMessage = dangerIDToString(dangerID)
Expand Down
67 changes: 67 additions & 0 deletions source/platforms/bitbucket_cloud/BitBucketCloudGit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { GitJSONDSL, GitDSL } from "../../dsl/GitDSL"
import { BitBucketCloudAPI } from "./BitBucketCloudAPI"
import { diffToGitJSONDSL } from "../git/diffToGitJSONDSL"
import { BitBucketCloudCommit, BitBucketCloudDSL } from "../../dsl/BitBucketCloudDSL"
import { GitCommit, GitCommitAuthor } from "../../dsl/Commit"
import { GitJSONToGitDSLConfig, gitJSONToGitDSL } from "../git/gitJSONToGitDSL"

import { debug } from "../../debug"
const d = debug("BitBucketCloudGit")

export function bitBucketCloudRawAndDateToGitCommitAuthor(raw: string, date: string): GitCommitAuthor {
const startIndexOfEmail = raw.lastIndexOf("<")
const endIndexOfEmail = raw.lastIndexOf(">")
if (startIndexOfEmail === -1 || endIndexOfEmail === -1) {
return {
name: raw,
email: "",
date,
}
}
const name = raw.substring(0, startIndexOfEmail).trim()
const email = raw.substring(startIndexOfEmail + 1, endIndexOfEmail).trim()
return {
name,
email,
date,
}
}

function bitBucketCloudCommitToGitCommit(commit: BitBucketCloudCommit): GitCommit {
const user = bitBucketCloudRawAndDateToGitCommitAuthor(commit.author.raw, commit.date)

return {
sha: commit.hash,
author: user,
committer: user,
message: commit.message,
tree: null,
parents: commit.parents != null ? commit.parents.map(parent => parent.hash) : undefined,
url: commit.links.html.href,
}
}

export default async function gitDSLForBitBucketCloud(api: BitBucketCloudAPI): Promise<GitJSONDSL> {
// We'll need all this info to be able to generate a working GitDSL object
const diffs = await api.getPullRequestDiff()
const gitCommits = await api.getPullRequestCommits()
const commits = gitCommits.map(bitBucketCloudCommitToGitCommit)
return diffToGitJSONDSL(diffs, commits)
}

export const bitBucketCloudGitDSL = (
bitBucketCloud: BitBucketCloudDSL,
json: GitJSONDSL,
bitBucketCloudAPI: BitBucketCloudAPI
): GitDSL => {
const config: GitJSONToGitDSLConfig = {
repo: bitBucketCloud.pr.source.repository.full_name,
baseSHA: bitBucketCloud.pr.destination.commit.hash,
headSHA: bitBucketCloud.pr.source.commit.hash,
getFileContents: bitBucketCloudAPI.getFileContents,
getFullDiff: bitBucketCloudAPI.getPullRequestDiff,
}

d("Setting up git DSL with: ", config)
return gitJSONToGitDSL(json, config)
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ describe("API testing - BitBucket Cloud", () => {
})
})

it("getFileContents", async () => {
api.fetch = fetchText
let text = await api.getFileContents("Dangerfile.ts", api.repoMetadata.repoSlug, "a0cd")

expect(JSON.parse(text)).toMatchObject({
method: "GET",
api: "https://api.bitbucket.org/2.0/repositories/foo/bar/src/a0cd/Dangerfile.ts",
headers: expectedJSONHeaders,
})
})

it("getPullRequestComments", async () => {
jsonResult = () => ({ next: undefined, values: [{ comment: {} }, {}] })
const result = await api.getPullRequestComments()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { bitBucketCloudRawAndDateToGitCommitAuthor } from "../BitBucketCloudGit"

describe("bitBucketCloudRawAndDateToGitCommitAuthor", () => {
const date = "2019-05-13T11:41:13+00:00"
it("should convert name doesn't contain space correctly", () => {
const raw = "Foo <foo@bar.com>"
expect(bitBucketCloudRawAndDateToGitCommitAuthor(raw, date)).toEqual({
name: "Foo",
email: "foo@bar.com",
date,
})
})
it("should convert name contains one space correctly", () => {
const raw = "Foo Bar <foo@bar.com>"
expect(bitBucketCloudRawAndDateToGitCommitAuthor(raw, date)).toEqual({
name: "Foo Bar",
email: "foo@bar.com",
date,
})
})
it("should convert name contains multiple space correctly", () => {
const raw = "Foo Bar Foo Bar Foo <foo@bar.com>"
expect(bitBucketCloudRawAndDateToGitCommitAuthor(raw, date)).toEqual({
name: "Foo Bar Foo Bar Foo",
email: "foo@bar.com",
date,
})
})
it("should convert name contains special characters correctly", () => {
const raw = "Foo Bar < Foo @Bar >Foo <foo@bar.com>"
expect(bitBucketCloudRawAndDateToGitCommitAuthor(raw, date)).toEqual({
name: "Foo Bar < Foo @Bar >Foo",
email: "foo@bar.com",
date,
})
})
it("should convert email contains multiple dot correctly", () => {
const raw = "Foo Bar <foo@bar.hello.com>"
expect(bitBucketCloudRawAndDateToGitCommitAuthor(raw, date)).toEqual({
name: "Foo Bar",
email: "foo@bar.hello.com",
date,
})
})
it("should put raw into name if it couldn't convert", () => {
const raw = "Foo Bar"
expect(bitBucketCloudRawAndDateToGitCommitAuthor(raw, date)).toEqual({
name: raw,
email: "",
date,
})
})
it("should put only name if it couldn't find an email", () => {
const raw = "Foo Bar <>"
expect(bitBucketCloudRawAndDateToGitCommitAuthor(raw, date)).toEqual({
name: "Foo Bar",
email: "",
date,
})
})
})

0 comments on commit 0fb3e55

Please sign in to comment.