forked from lingodotdev/lingo.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
109 lines (93 loc) · 3.38 KB
/
github.ts
File metadata and controls
109 lines (93 loc) · 3.38 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Octokit } from "octokit";
import { PlatformKit } from "./_base.js";
import Z from "zod";
import { configureGitCredentials } from "./git-utils.js";
export class GitHubPlatformKit extends PlatformKit {
private _octokit?: Octokit;
get octokit() {
if (!this._octokit) {
this._octokit = new Octokit({ auth: this.platformConfig.ghToken });
}
return this._octokit;
}
async branchExists({ branch }: { branch: string }) {
return await this.octokit.rest.repos
.getBranch({
branch,
owner: this.platformConfig.repositoryOwner,
repo: this.platformConfig.repositoryName,
})
.then((r) => r.data)
.then((v) => !!v)
.catch((r) => (r.status === 404 ? false : Promise.reject(r)));
}
async getOpenPullRequestNumber({ branch }: { branch: string }) {
return await this.octokit.rest.pulls
.list({
head: `${this.platformConfig.repositoryOwner}:${branch}`,
owner: this.platformConfig.repositoryOwner,
repo: this.platformConfig.repositoryName,
base: this.platformConfig.baseBranchName,
state: "open",
})
.then(({ data }) => data[0])
.then((pr) => pr?.number);
}
async closePullRequest({ pullRequestNumber }: { pullRequestNumber: number }) {
await this.octokit.rest.pulls.update({
pull_number: pullRequestNumber,
owner: this.platformConfig.repositoryOwner,
repo: this.platformConfig.repositoryName,
state: "closed",
});
}
async createPullRequest({ head, title, body }: { head: string; title: string; body?: string }) {
return await this.octokit.rest.pulls
.create({
head,
title,
body,
owner: this.platformConfig.repositoryOwner,
repo: this.platformConfig.repositoryName,
base: this.platformConfig.baseBranchName,
})
.then(({ data }) => data.number);
}
async commentOnPullRequest({ pullRequestNumber, body }: { pullRequestNumber: number; body: string }) {
await this.octokit.rest.issues.createComment({
issue_number: pullRequestNumber,
body,
owner: this.platformConfig.repositoryOwner,
repo: this.platformConfig.repositoryName,
});
}
async gitConfig() {
const { ghToken, repositoryOwner, repositoryName } = this.platformConfig;
const { processOwnCommits } = this.config;
if (ghToken && processOwnCommits) {
console.log("Using provided GH_TOKEN. This will trigger your CI/CD pipeline to run again.");
const URL = `https://${ghToken}@github.com/${repositoryOwner}/${repositoryName}.git`;
configureGitCredentials(ghToken, URL);
}
}
get platformConfig() {
const env = Z.object({
GITHUB_REPOSITORY: Z.string(),
GITHUB_REPOSITORY_OWNER: Z.string(),
GITHUB_REF_NAME: Z.string(),
GITHUB_HEAD_REF: Z.string(),
GH_TOKEN: Z.string().optional(),
}).parse(process.env);
const baseBranchName = !env.GITHUB_REF_NAME.endsWith("/merge") ? env.GITHUB_REF_NAME : env.GITHUB_HEAD_REF;
return {
ghToken: env.GH_TOKEN,
baseBranchName,
repositoryOwner: env.GITHUB_REPOSITORY_OWNER,
repositoryName: env.GITHUB_REPOSITORY.split("/")[1],
};
}
buildPullRequestUrl(pullRequestNumber: number) {
const { repositoryOwner, repositoryName } = this.platformConfig;
return `https://github.com/${repositoryOwner}/${repositoryName}/pull/${pullRequestNumber}`;
}
}