/
fixturedGitHubDSL.ts
73 lines (58 loc) · 2.8 KB
/
fixturedGitHubDSL.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
69
70
71
72
73
import { GitHub, githubJSONToGitHubDSL, GitHubType } from "../../GitHub"
import { GitHubAPI } from "../GitHubAPI"
import { FakeCI } from "../../../ci_source/providers/Fake"
import { readFileSync } from "fs"
import { resolve, join as pathJoin } from "path"
import { gitHubGitDSL as gitJSONToGitDSL } from "../GitHubGit"
import { Octokit as NodeGitHub } from "@octokit/rest"
import { GitHubDSL } from "../../../dsl/GitHubDSL"
import { GitDSL, GitJSONDSL } from "../../../dsl/GitDSL"
import { DangerDSLType } from "../../../dsl/DangerDSL"
const fixtures = resolve(__dirname, "..", "..", "_tests", "fixtures")
/** Returns a fixture. */
const loadFixture = (path: string): string =>
readFileSync(pathJoin(fixtures, path), {})
.toString()
.replace(/\r/g, "")
/** Returns JSON from the fixtured dir */
export const requestWithFixturedJSON = async (path: string): Promise<() => Promise<any>> => () =>
Promise.resolve(JSON.parse(loadFixture(path)))
/** Returns arbitrary text value from a request */
export const requestWithFixturedContent = async (path: string): Promise<() => Promise<string>> => () =>
Promise.resolve(loadFixture(path))
const pullRequestInfoFilename = "github_pr.json"
// const masterSHA = JSON.parse(readFileSync(pathJoin(fixtures, pullRequestInfoFilename), {}).toString()).base.sha
export const fixturedGitHubDSL = async (): Promise<DangerDSLType> => {
let github: GitHubType = {} as any
let nodeGitHubAPI: NodeGitHub = {} as any
let gitJSONDSL: GitJSONDSL = {} as any
let githubDSL: GitHubDSL = {} as any
let gitDSL: GitDSL = {} as any
const api = new GitHubAPI(new FakeCI({}))
github = GitHub(api)
// Unused, but needed for DSL generation
api.getIssue = () => Promise.resolve({})
api.getReviews = () => Promise.resolve({})
api.getReviewerRequests = () => Promise.resolve({})
// Actually used
api.getPullRequestInfo = await requestWithFixturedJSON(pullRequestInfoFilename)
api.getPullRequestDiff = await requestWithFixturedContent("github_diff.diff")
api.getPullRequestCommits = await requestWithFixturedJSON("github_commits.json")
api.getFileContents = async (_path, _repoSlug, ref) => (await requestWithFixturedJSON(`static_file.${ref}.json`))()
nodeGitHubAPI = new NodeGitHub()
const mockContents = async ({ ref }: any) => (await requestWithFixturedJSON(`static_file.${ref}.json`))()
nodeGitHubAPI.repos.getContent = mockContents as any
gitJSONDSL = await github.getPlatformGitRepresentation()
const githubJSONDSL = await github.getPlatformReviewDSLRepresentation()
githubDSL = githubJSONToGitHubDSL(githubJSONDSL, nodeGitHubAPI)
gitDSL = gitJSONToGitDSL(githubDSL, gitJSONDSL, github.api)
return {
// bitbucket_server: undefined,
git: gitDSL,
github: githubDSL,
utils: {
href: jest.fn() as any,
sentence: jest.fn() as any,
},
} as any
}