-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
244 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
source/platforms/bitbucket_cloud/_tests_/_bitbucket_cloud_git.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
}) | ||
}) |