-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepository.ts
More file actions
250 lines (209 loc) · 7.53 KB
/
repository.ts
File metadata and controls
250 lines (209 loc) · 7.53 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import type { Config } from "../types/config";
import type { GitHub } from "@actions/github/lib/utils";
import type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods/dist-types/generated/parameters-and-response-types";
import { exec } from "./processes";
import { randomString } from "./strings";
import {
defaultAuthor,
defaultCommit,
defaultPullRequest,
} from "../libs/defaults";
import { info } from "@actions/core";
export class Repository {
private _config: Config;
private _currentBranch: string = "";
private _newBranch: boolean = false;
private _octokit: InstanceType<typeof GitHub>;
constructor(config: Config, octokit: InstanceType<typeof GitHub>) {
this._config = config;
this._octokit = octokit;
}
async authenticate() {
const authorName =
this._config.repository?.commit?.author?.name || defaultAuthor.name;
const authorEmail =
this._config.repository?.commit?.author?.email ||
defaultAuthor.email;
try {
await exec(`git config user.name "${authorName}"`);
await exec(`git config user.email "${authorEmail}"`);
} catch (error) {
// @ts-expect-error
error.message = `Error authenticating user "${authorName}" with e-mail "${authorEmail}": ${error.message}`;
throw error;
}
}
async branchExists() {
try {
const hasLocalBranch = async () => {
const result = await exec(
`git branch --list "${this.branchName()}"`,
);
return result.includes(this.branchName());
};
const hasRemoteBranch = async () => {
const result = await exec(
`git ls-remote --heads origin "${this.branchName()}"`,
);
return result.includes(this.branchName());
};
return (await hasLocalBranch()) || (await hasRemoteBranch());
} catch (error) {
// @ts-expect-error
error.message = `Error searching for branch "${this.branchName()}": ${error.message}`;
throw error;
}
}
async checkoutBranch(isNew: boolean) {
try {
this._newBranch = isNew;
await exec(
`git switch ${isNew ? "-c" : ""} "${this.branchName()}"`,
);
} catch (error) {
// @ts-expect-error
error.message = `Error checking out ${isNew ? "new" : "existing"} branch "${this.branchName()}": ${error.message}`;
throw error;
}
}
async stage() {
try {
await exec(`git add ${this._config.readme}`);
} catch (error) {
// @ts-expect-error
error.message = `Error staging file "${this._config.readme}": ${error.message}`;
throw error;
}
}
async commit() {
try {
let message =
this._config.repository?.commit?.title || defaultCommit.title;
const body =
this._config.repository?.commit?.body ||
defaultCommit.body ||
"";
if (body !== "") {
message += `\n${body}`;
}
await exec(`git commit -m "${message}"`);
} catch (error) {
// @ts-expect-error
error.message = `Error committing file "${this._config.readme}": ${error.message}`;
throw error;
}
}
async push() {
try {
let cmd = "git push";
if (this._newBranch) {
cmd += ` --set-upstream origin ${this.branchName()}`;
}
await exec(cmd);
} catch (error) {
// @ts-expect-error
error.message = `Error pushing changes to "${this.branchName()} branch": ${error.message}`;
throw error;
}
}
async createPullRequest() {
try {
const defaultBranch: string = await this.defaultBranchName();
return await this._octokit.rest.pulls.create(<
RestEndpointMethodTypes["pulls"]["create"]["parameters"]
>{
owner: this._config.repository?.owner,
repo: this._config.repository?.repo,
title:
this._config.repository?.pullRequest?.title ||
defaultPullRequest.title,
body:
this._config.repository?.pullRequest?.body ||
defaultPullRequest.body ||
"",
head: this.branchName(),
base: defaultBranch,
});
} catch (error) {
// @ts-expect-error
error.message = `Error when creating a pull request from ${this.branchName()}: ${error.message}`;
throw error;
}
}
async assignee(issueNumber: number, assignees: string[]) {
try {
if (assignees.length === 0) {
return;
}
return await this._octokit.rest.issues.addAssignees(<
RestEndpointMethodTypes["issues"]["addAssignees"]["parameters"]
>{
owner: this._config.repository?.owner,
repo: this._config.repository?.repo,
issue_number: issueNumber,
assignees: assignees,
});
} catch (error) {
// @ts-expect-error
error.message = `Error when adding assignees to issue ${issueNumber}: ${error.message}`;
throw error;
}
}
async addLabels(issueNumber: number, labels: string[]) {
try {
if (labels.length === 0) {
return;
}
return await this._octokit.rest.issues.addLabels(<
RestEndpointMethodTypes["issues"]["addLabels"]["parameters"]
>{
owner: this._config.repository?.owner,
repo: this._config.repository?.repo,
issue_number: issueNumber,
labels,
});
} catch (error) {
// @ts-expect-error
error.message = `Error when adding labels to issue ${issueNumber}: ${error.message}`;
throw error;
}
}
async getRawFile(repo: string, filename: string): Promise<string> {
try {
const response = await this._octokit.rest.repos.getContent(<
RestEndpointMethodTypes["repos"]["getContent"]["parameters"]
>{
owner: this._config.repository?.owner,
repo: repo,
path: filename,
headers: {
Accept: "application/vnd.github.v3.raw",
},
});
if (response.status !== 200) {
return "";
}
// @ts-ignore
return response.data;
} catch (error) {
// @ts-expect-error
info(error.message);
return "";
}
}
branchName(): string {
if (this._currentBranch === "") {
const branch: string =
this._config.repository?.commit?.branch ||
defaultCommit.branch ||
"preview/{random}";
this._currentBranch = branch.replace("{random}", randomString());
}
return this._currentBranch;
}
async defaultBranchName(): Promise<string> {
return await exec(
`git remote show origin | grep 'HEAD branch' | cut -d ' ' -f5`,
);
}
}