Skip to content

Commit

Permalink
feat: add getFullDiff implementation for GitLab
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-gohri committed Feb 25, 2020
1 parent 4b2ec11 commit 3323612
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
17 changes: 17 additions & 0 deletions source/dsl/GitLabDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,20 @@ export interface GitLabRepositoryFile {
commit_id: string
last_commit_id: string
}

export interface GitLabCommit {
id: string
short_id: string
title: string
author_name: string
author_email: string
created_at: string
}

export interface GitLabRepositoryCompare {
commit: GitLabCommit
commits: GitLabCommit[]
diffs: GitLabMRChange[]
compare_timeout: boolean
compare_same_ref: boolean
}
8 changes: 8 additions & 0 deletions source/platforms/gitlab/GitLabAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
GitLabNote,
GitLabUserProfile,
GitLabRepositoryFile,
GitLabRepositoryCompare,
} from "../../dsl/GitLabDSL"

import { Gitlab } from "gitlab"
Expand Down Expand Up @@ -218,6 +219,13 @@ class GitLabAPI {
throw e
}
}

getCompareChanges = async (base: string, head: string): Promise<GitLabMRChange[]> => {
const api = this.api.Repositories
const projectId = this.repoMetadata.repoSlug
const compare = (await api.compare(projectId, base, head)) as GitLabRepositoryCompare
return compare.diffs
}
}

export default GitLabAPI
31 changes: 22 additions & 9 deletions source/platforms/gitlab/GitLabGit.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
import { debug } from "../../debug"
import { GitLabDSL } from "../../dsl/GitLabDSL"
import { GitLabDSL, GitLabMRChange } from "../../dsl/GitLabDSL"
import { GitDSL, GitJSONDSL } from "../../dsl/GitDSL"
import { gitJSONToGitDSL, GitJSONToGitDSLConfig, GitStructuredDiff } from "../git/gitJSONToGitDSL"
import GitLabAPI from "./GitLabAPI"

const d = debug("GitLabGit")

export const gitLabGitDSL = (gitlab: GitLabDSL, json: GitJSONDSL): GitDSL => {
export const gitLabGitDSL = (gitlab: GitLabDSL, json: GitJSONDSL, gitlabAPI: GitLabAPI): GitDSL => {
const config: GitJSONToGitDSLConfig = {
repo: `${gitlab.mr.project_id}`, // we don't get the repo slug, but `project_id` is equivalent in API calls
baseSHA: gitlab.mr.diff_refs.base_sha,
headSHA: gitlab.mr.diff_refs.head_sha,
getFileContents: gitlab.utils.fileContents,
// TODO: implement me when the API methods are in
getFullDiff: async (): Promise<string> => {
throw new Error("getFullDiff is not yet implemented")
},
// TODO: implement me when the API methods are in
getStructuredDiffForFile: async (): Promise<GitStructuredDiff> => {
throw new Error("getStructuredDiffForFile is not yet implemented")
getFullDiff: async (base: string, head: string) => {
const changes = await gitlabAPI.getCompareChanges(base, head)
return gitlabChangesToDiff(changes)
},
}

d("Setting up git DSL with: ", config)
return gitJSONToGitDSL(json, config)
}

const gitlabChangesToDiff = (changes: GitLabMRChange[]): string => {
// Gitlab doesn't return full raw git diff, relevant issue: https://gitlab.com/gitlab-org/gitlab/issues/24913
return changes
.map(change => {
return `\
diff --git a/${change.old_path} b/${change.new_path}
${change.new_file ? `new file mode ${change.b_mode}` : ""}\
${change.deleted_file ? `deleted file mode ${change.a_mode}` : ""}\
${change.renamed_file ? `rename from ${change.old_path}\nrename to ${change.new_path}` : ""}
--- ${change.new_file ? "/dev/null" : "a/" + change.old_path}
+++ ${change.deleted_file ? "/dev/null" : "b/" + change.new_path}
${change.diff}`
})
.join("\n")
}
2 changes: 1 addition & 1 deletion source/runner/jsonToDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const jsonToDSL = async (dsl: DangerDSLJSONType, source: CISource): Promi
} else if (process.env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] || process.env["DANGER_BITBUCKETCLOUD_USERNAME"]) {
git = bitBucketCloudGitDSL(bitbucket_cloud!, dsl.git, api as BitBucketCloudAPI)
} else if (process.env["DANGER_GITLAB_API_TOKEN"]) {
git = gitLabGitDSL(gitlab!, dsl.git)
git = gitLabGitDSL(gitlab!, dsl.git, api as GitLabAPI)
} else {
git = source && source.useEventDSL ? ({} as any) : githubJSONToGitDSL(github!, dsl.git)
}
Expand Down

0 comments on commit 3323612

Please sign in to comment.