-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1002 from rohit-gohri/gitlab-diff
Add structured diff support to Gitlab
- Loading branch information
Showing
18 changed files
with
1,363 additions
and
144 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,26 +1,45 @@ | ||
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 { gitJSONToGitDSL, GitJSONToGitDSLConfig } 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) | ||
} | ||
|
||
export const gitlabChangesToDiff = (changes: GitLabMRChange[]): string => { | ||
d("Converting GitLab Changes to Diff") | ||
// Gitlab doesn't return full raw git diff, relevant issue: https://gitlab.com/gitlab-org/gitlab/issues/24913 | ||
return changes | ||
.map(change => { | ||
const { diff } = change | ||
if (diff.startsWith("diff --git a/") || diff.startsWith("--- a/") || diff.startsWith("--- /dev/null")) { | ||
return diff | ||
} | ||
|
||
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} | ||
${diff}` | ||
}) | ||
.join("\n") | ||
} |
Oops, something went wrong.