Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GitJSONDSL and diffForFile for BitBucket Server #764

Merged
merged 1 commit into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

## Master

- Fix `GitJSONDSL` and `diffForFile` for BitBucket Server [@langovoi][]

# 6.1.3

- Add support for personal tokens of BitBucket Server - [@langovoi][]
Expand Down
24 changes: 24 additions & 0 deletions source/danger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,30 @@ interface BitBucketServerPRComment {
}
}

interface BitBucketServerChanges {
nextPageStart: number | null
values: BitBucketServerChangesValue[]
}

interface BitBucketServerChangesValueAddModifyDelete {
type: "ADD" | "MODIFY" | "DELETE"
path: {
toString: string
}
}

interface BitBucketServerChangesValueMove {
type: "MOVE"
path: {
toString: string
}
srcPath: {
toString: string
}
}

type BitBucketServerChangesValue = BitBucketServerChangesValueAddModifyDelete | BitBucketServerChangesValueMove

/** A platform agnostic reference to a Git commit */
interface GitCommit {
/** The SHA for the commit */
Expand Down
24 changes: 24 additions & 0 deletions source/dsl/BitBucketServerDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,27 @@ export interface BitBucketServerPRComment {
id: number
}
}

export interface BitBucketServerChanges {
nextPageStart: number | null
values: BitBucketServerChangesValue[]
}

export interface BitBucketServerChangesValueAddModifyDelete {
type: "ADD" | "MODIFY" | "DELETE"
path: {
toString: string
}
}

export interface BitBucketServerChangesValueMove {
type: "MOVE"
path: {
toString: string
}
srcPath: {
toString: string
}
}

export type BitBucketServerChangesValue = BitBucketServerChangesValueAddModifyDelete | BitBucketServerChangesValueMove
4 changes: 2 additions & 2 deletions source/platforms/_tests/_bitbucket_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class mockBitBucketServerAPI /*tslint:disable-line*/ {
const fixtures = await requestWithFixturedJSON("bitbucket_server_comments.json")
return await fixtures()
}
async getPullRequestDiff() {
const fixtures = await requestWithFixturedJSON("bitbucket_server_diff.json")
async getPullRequestChanges() {
const fixtures = await requestWithFixturedJSON("bitbucket_server_changes.json")
return await fixtures()
}
async getPullRequestActivities() {
Expand Down
29 changes: 29 additions & 0 deletions source/platforms/_tests/fixtures/bitbucket_server_changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"type": "MODIFY",
"path": {
"toString": ".gitignore"
}
},
{
"type": "ADD",
"path": {
"toString": "banana"
}
},
{
"type": "MOVE",
"path": {
"toString": ".babelrc"
},
"srcPath": {
"toString": ".babelrc.example"
}
},
{
"type": "DELETE",
"path": {
"toString": "jest.eslint.config.js"
}
}
]
27 changes: 20 additions & 7 deletions source/platforms/bitbucket_server/BitBucketServerAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
BitBucketServerPRActivity,
BitBucketServerDiff,
RepoMetaData,
BitBucketServerChanges,
BitBucketServerChangesValue,
} from "../../dsl/BitBucketServerDSL"
import { Comment } from "../platform"

Expand Down Expand Up @@ -87,19 +89,30 @@ export class BitBucketServerAPI {
return (await res.json()).values
}

getStructuredDiff = async (base: string, head: string): Promise<BitBucketServerDiff[]> => {
getStructuredDiffForFile = async (base: string, head: string, filename: string): Promise<BitBucketServerDiff[]> => {
const { repoSlug } = this.repoMetadata
const path = `rest/api/1.0/${repoSlug}/compare/diff?withComments=false&from=${base}&to=${head}`
const path = `rest/api/1.0/${repoSlug}/compare/diff/${filename}?withComments=false&from=${base}&to=${head}`
const res = await this.get(path)
throwIfNotOk(res)
return (await res.json()).diffs
}

getPullRequestDiff = async (): Promise<BitBucketServerDiff[]> => {
const path = `${this.getPRBasePath()}/diff?withComments=false`
const res = await this.get(path)
throwIfNotOk(res)
return (await res.json()).diffs
getPullRequestChanges = async (): Promise<BitBucketServerChangesValue[]> => {
let nextPageStart: null | number = 0
let values: BitBucketServerChangesValue[] = []

do {
const path = `${this.getPRBasePath()}/changes?start=${nextPageStart}`
const res = await this.get(path)
throwIfNotOk(res)

const data = (await res.json()) as BitBucketServerChanges

values = values.concat(data.values)
nextPageStart = data.nextPageStart
} while (nextPageStart !== null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collect all pages with changes


return values
}

getPullRequestComments = async (): Promise<BitBucketServerPRActivity[]> => {
Expand Down
59 changes: 44 additions & 15 deletions source/platforms/bitbucket_server/BitBucketServerGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BitBucketServerDSL,
BitBucketServerDiff,
RepoMetaData,
BitBucketServerChangesValue,
} from "../../dsl/BitBucketServerDSL"
import { GitCommit } from "../../dsl/Commit"

Expand Down Expand Up @@ -53,12 +54,12 @@ function bitBucketServerCommitToGitCommit(

export default async function gitDSLForBitBucketServer(api: BitBucketServerAPI): Promise<GitJSONDSL> {
// We'll need all this info to be able to generate a working GitDSL object
const diff = await api.getPullRequestDiff()
const changes = await api.getPullRequestChanges()
const gitCommits = await api.getPullRequestCommits()
const commits = gitCommits.map(commit =>
bitBucketServerCommitToGitCommit(commit, api.repoMetadata, api.repoCredentials.host)
)
return bitBucketServerDiffToGitJSONDSL(diff, commits)
return bitBucketServerChangesToGitJSONDSL(changes, commits)
}

export const bitBucketServerGitDSL = (
Expand All @@ -73,8 +74,8 @@ export const bitBucketServerGitDSL = (
baseSHA: bitBucketServer.pr.fromRef.latestCommit,
headSHA: bitBucketServer.pr.toRef.latestCommit,
getFileContents: bitBucketServerAPI.getFileContents,
getFullStructuredDiff: async (base: string, head: string) => {
const diff = await bitBucketServerAPI.getStructuredDiff(base, head)
getStructuredDiffForFile: async (base: string, head: string, filename: string) => {
const diff = await bitBucketServerAPI.getStructuredDiffForFile(base, head, filename)
return bitBucketServerDiffToGitStructuredDiff(diff)
},
}
Expand All @@ -83,17 +84,45 @@ export const bitBucketServerGitDSL = (
return gitJSONToGitDSL(json, config)
}

const bitBucketServerDiffToGitJSONDSL = (diffs: BitBucketServerDiff[], commits: GitCommit[]): GitJSONDSL => {
const deleted_files = diffs.filter(diff => diff.source && !diff.destination).map(diff => diff.source!.toString)
const created_files = diffs.filter(diff => !diff.source && diff.destination).map(diff => diff.destination!.toString)
const modified_files = diffs.filter(diff => diff.source && diff.destination).map(diff => diff.destination!.toString)

return {
modified_files,
created_files,
deleted_files,
commits,
}
const bitBucketServerChangesToGitJSONDSL = (
changes: BitBucketServerChangesValue[],
commits: GitCommit[]
): GitJSONDSL => {
return changes.reduce<GitJSONDSL>(
(git, value) => {
switch (value.type) {
case "ADD":
return {
...git,
created_files: [...git.created_files, value.path.toString],
}
case "MODIFY":
return {
...git,
modified_files: [...git.modified_files, value.path.toString],
}
case "MOVE":
return {
...git,
created_files: [...git.created_files, value.path.toString],
deleted_files: [...git.deleted_files, value.srcPath.toString],
}
case "DELETE":
return {
...git,
deleted_files: [...git.deleted_files, value.path.toString],
}
default:
throw new Error("Unhandled change type")
}
},
{
modified_files: [],
created_files: [],
deleted_files: [],
commits,
}
)
}

const bitBucketServerDiffToGitStructuredDiff = (diffs: BitBucketServerDiff[]): GitStructuredDiff => {
Expand Down
Loading