-
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getFullDiff implementation for GitLab
- Loading branch information
1 parent
4b2ec11
commit 3323612
Showing
4 changed files
with
48 additions
and
10 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 |
---|---|---|
@@ -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") | ||
} |
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