-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Copy pathGitHubGit.ts
68 lines (57 loc) · 2.14 KB
/
GitHubGit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { GitDSL, GitJSONDSL } from "../../dsl/GitDSL"
import { GitHubCommit, GitHubDSL } from "../../dsl/GitHubDSL"
import { GitCommit } from "../../dsl/Commit"
import { GitHubAPI } from "../github/GitHubAPI"
import { diffToGitJSONDSL } from "../git/diffToGitJSONDSL"
import { GitJSONToGitDSLConfig, gitJSONToGitDSL } from "../git/gitJSONToGitDSL"
import { debug } from "../../debug"
const d = debug("GitHubGit")
/**
* Returns the response for the new comment
*
* @param {GitHubCommit} ghCommit A GitHub based commit
* @returns {GitCommit} a Git commit representation without GH metadata
*/
function githubCommitToGitCommit(ghCommit: GitHubCommit): GitCommit {
return {
sha: ghCommit.sha,
parents: ghCommit.parents.map(p => p.sha),
author: ghCommit.commit.author,
committer: ghCommit.commit.committer,
message: ghCommit.commit.message,
tree: ghCommit.commit.tree,
url: ghCommit.url,
}
}
export default async function gitDSLForGitHub(api: GitHubAPI): Promise<GitJSONDSL> {
// We'll need all this info to be able to generate a working GitDSL object
const diff = await api.getPullRequestDiff()
const getCommits = await api.getPullRequestCommits()
const commits = getCommits.map(githubCommitToGitCommit)
return diffToGitJSONDSL(diff, commits)
}
export const gitHubGitDSL = (github: GitHubDSL, json: GitJSONDSL, githubAPI?: GitHubAPI, ghToken?: string): GitDSL => {
// TODO: Remove the GitHubAPI
// This is blocked by https://github.com/octokit/node-github/issues/602
const ghAPI =
githubAPI ||
new GitHubAPI({ repoSlug: github.pr.base.repo.full_name, pullRequestID: String(github.pr.number) }, ghToken)
if (!githubAPI) {
d("Got no GH API, had to make it")
}
const config: GitJSONToGitDSLConfig = {
repo: github.pr.head.repo.full_name,
baseSHA: github.pr.base.sha,
headSHA: github.pr.head.sha,
getFileContents: github.utils.fileContents,
getFullDiff: ghAPI.getPullRequestDiff,
}
d("Setting up git DSL with: ", config)
return gitJSONToGitDSL(json, config)
}
export const emptyGitJSON = (): GitJSONDSL => ({
commits: [],
created_files: [],
deleted_files: [],
modified_files: [],
})