-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
git.js
252 lines (203 loc) · 5.71 KB
/
git.js
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
251
252
const { parse } = require('@putout/git-status-porcelain')
const core = require('@actions/core')
const { GitHub, getOctokitOptions } = require('@actions/github/lib/utils')
const { throttling } = require('@octokit/plugin-throttling')
const path = require('path')
const {
GITHUB_TOKEN,
GIT_USERNAME,
GIT_EMAIL,
TMP_DIR,
COMMIT_BODY,
COMMIT_PREFIX,
GITHUB_REPOSITORY,
OVERWRITE_EXISTING_PR,
BRANCH_PREFIX
} = require('./config')
const { dedent, execCmd } = require('./helpers')
class Git {
constructor() {
const Octokit = GitHub.plugin(throttling)
const options = getOctokitOptions(GITHUB_TOKEN, {
throttle: {
onRateLimit: (retryAfter, options) => {
core.warning(`Request quota exhausted for request ${ options.method } ${ options.url }`)
if (options.request.retryCount === 0) {
// only retries once
core.info(`Retrying after ${ retryAfter } seconds!`)
return true
}
},
onAbuseLimit: (retryAfter, options) => {
// does not retry, only logs a warning
core.warning(`Abuse detected for request ${ options.method } ${ options.url }`)
}
}
})
const octokit = new Octokit(options)
// We only need the rest client
this.github = octokit.rest
}
async initRepo(repo) {
// Reset repo specific values
this.existingPr = undefined
this.prBranch = undefined
this.baseBranch = undefined
// Set values to current repo
this.repo = repo
this.workingDir = path.join(TMP_DIR, repo.uniqueName)
this.gitUrl = `https://${ GITHUB_TOKEN }@${ repo.fullName }.git`
await this.clone()
await this.setIdentity()
await this.getBaseBranch()
}
async clone() {
core.debug(`Cloning ${ this.repo.fullName } into ${ this.workingDir }`)
return execCmd(
`git clone --depth 1 ${ this.repo.branch !== 'default' ? '--branch "' + this.repo.branch + '"' : '' } ${ this.gitUrl } ${ this.workingDir }`
)
}
async setIdentity() {
let username = GIT_USERNAME
let email = GIT_EMAIL
if (email === undefined) {
const { data } = await this.github.users.getAuthenticated()
email = data.email
username = data.login
}
core.debug(`Setting git user to email: ${ email }, username: ${ username }`)
return execCmd(
`git config --local user.name "${ username }" && git config --local user.email "${ email }"`,
this.workingDir
)
}
async getBaseBranch() {
this.baseBranch = await execCmd(
`git rev-parse --abbrev-ref HEAD`,
this.workingDir
)
}
async createPrBranch() {
const prefix = BRANCH_PREFIX.replace('SOURCE_REPO_NAME', GITHUB_REPOSITORY.split('/')[1])
let newBranch = path.join(prefix, this.repo.branch)
if (OVERWRITE_EXISTING_PR === false) {
newBranch += `-${ Math.round((new Date()).getTime() / 1000) }`
}
core.debug(`Creating PR Branch ${ newBranch }`)
await execCmd(
`git checkout -b "${ newBranch }"`,
this.workingDir
)
this.prBranch = newBranch
}
async add(file) {
return execCmd(
`git add -f ${ file }`,
this.workingDir
)
}
async hasChanges() {
const statusOutput = await execCmd(
`git status --porcelain`,
this.workingDir
)
return parse(statusOutput).length !== 0
}
async commit(msg) {
let message = msg !== undefined ? msg : `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY }`
if (COMMIT_BODY) {
message += `\n\n${ COMMIT_BODY }`
}
return execCmd(
`git commit -m "${ message }"`,
this.workingDir
)
}
async status() {
return execCmd(
`git status`,
this.workingDir
)
}
async push() {
return execCmd(
`git push ${ this.gitUrl } --force`,
this.workingDir
)
}
async findExistingPr() {
const { data } = await this.github.pulls.list({
owner: this.repo.user,
repo: this.repo.name,
state: 'open',
head: `${ this.repo.user }:${ this.prBranch }`
})
this.existingPr = data[0]
return this.existingPr
}
async setPrWarning() {
await this.github.pulls.update({
owner: this.repo.user,
repo: this.repo.name,
pull_number: this.existingPr.number,
body: dedent(`
⚠️ This PR is being automatically resynced ⚠️
${ this.existingPr.body }
`)
})
}
async removePrWarning() {
await this.github.pulls.update({
owner: this.repo.user,
repo: this.repo.name,
pull_number: this.existingPr.number,
body: this.existingPr.body.replace('⚠️ This PR is being automatically resynced ⚠️', '')
})
}
async createOrUpdatePr(changedFiles) {
const body = dedent(`
Synced local file(s) with [${ GITHUB_REPOSITORY }](https://github.com/${ GITHUB_REPOSITORY }).
${ changedFiles }
---
This PR was created automatically by the [repo-file-sync-action](https://github.com/BetaHuhn/repo-file-sync-action) workflow run [#${ process.env.GITHUB_RUN_ID || 0 }](https://github.com/${ GITHUB_REPOSITORY }/actions/runs/${ process.env.GITHUB_RUN_ID || 0 })
`)
if (this.existingPr) {
core.info(`Overwriting existing PR`)
const { data } = await this.github.pulls.update({
owner: this.repo.user,
repo: this.repo.name,
pull_number: this.existingPr.number,
body: body
})
return data
}
core.info(`Creating new PR`)
const { data } = await this.github.pulls.create({
owner: this.repo.user,
repo: this.repo.name,
title: `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY }`,
body: body,
head: this.prBranch,
base: this.baseBranch
})
this.existingPr = data
return data
}
async addPrLabels(labels) {
await this.github.issues.addLabels({
owner: this.repo.user,
repo: this.repo.name,
issue_number: this.existingPr.number,
labels: labels
})
}
async addPrAssignees(assignees) {
await this.github.issues.addAssignees({
owner: this.repo.user,
repo: this.repo.name,
issue_number: this.existingPr.number,
assignees: assignees
})
}
}
module.exports = Git