-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathmain.ts
303 lines (266 loc) · 9.38 KB
/
main.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import * as core from '@actions/core'
import path from 'path'
import simpleGit, { Response } from 'simple-git'
import { checkInputs, getInput, logOutputs, setOutput } from './io'
import { log, matchGitArgs, parseInputArray } from './util'
const baseDir = path.join(process.cwd(), getInput('cwd') || '')
const git = simpleGit({ baseDir })
const exitErrors: Error[] = []
core.info(`Running in ${baseDir}`)
;(async () => {
await checkInputs()
core.startGroup('Internal logs')
core.info('> Staging files...')
const ignoreErrors =
getInput('pathspec_error_handling') == 'ignore' ? 'pathspec' : 'none'
if (getInput('add')) {
core.info('> Adding files...')
await add(ignoreErrors)
} else core.info('> No files to add.')
if (getInput('remove')) {
core.info('> Removing files...')
await remove(ignoreErrors)
} else core.info('> No files to remove.')
core.info('> Checking for uncommitted changes in the git working tree...')
const changedFiles = (await git.diffSummary(['--cached'])).files.length
// continue if there are any changes or if the allow-empty commit argument is included
if (
changedFiles > 0 ||
matchGitArgs(getInput('commit') || '').includes('--allow-empty')
) {
core.info(`> Found ${changedFiles} changed files.`)
core.debug(
`--allow-empty argument detected: ${matchGitArgs(
getInput('commit') || ''
).includes('--allow-empty')}`
)
await git
.addConfig('user.email', getInput('author_email'), undefined, log)
.addConfig('user.name', getInput('author_name'), undefined, log)
.addConfig('author.email', getInput('author_email'), undefined, log)
.addConfig('author.name', getInput('author_name'), undefined, log)
.addConfig('committer.email', getInput('committer_email'), undefined, log)
.addConfig('committer.name', getInput('committer_name'), undefined, log)
core.debug(
'> Current git config\n' +
JSON.stringify((await git.listConfig()).all, null, 2)
)
let fetchOption: string | boolean
try {
fetchOption = getInput('fetch', true)
} catch {
fetchOption = getInput('fetch')
}
if (fetchOption) {
core.info('> Fetching repo...')
await git.fetch(
matchGitArgs(fetchOption === true ? '' : fetchOption),
log
)
} else core.info('> Not fetching repo.')
const targetBranch = getInput('new_branch')
if (targetBranch) {
core.info('> Checking-out branch...')
if (!fetchOption)
core.warning(
'Creating a new branch without fetching the repo first could result in an error when pushing to GitHub. Refer to the action README for more info about this topic.'
)
await git
.checkout(targetBranch)
.then(() => {
log(undefined, `'${targetBranch}' branch already existed.`)
})
.catch(() => {
log(undefined, `Creating '${targetBranch}' branch.`)
return git.checkoutLocalBranch(targetBranch, log)
})
}
const pullOption = getInput('pull')
if (pullOption) {
core.info('> Pulling from remote...')
core.debug(`Current git pull arguments: ${pullOption}`)
await git
.fetch(undefined, log)
.pull(undefined, undefined, matchGitArgs(pullOption), log)
core.info('> Checking for conflicts...')
const status = await git.status(undefined, log)
if (!status.conflicted.length) {
core.info('> No conflicts found.')
core.info('> Re-staging files...')
if (getInput('add')) await add(ignoreErrors)
if (getInput('remove')) await remove(ignoreErrors)
} else
throw new Error(
`There are ${
status.conflicted.length
} conflicting files: ${status.conflicted.join(', ')}`
)
} else core.info('> Not pulling from repo.')
core.info('> Creating commit...')
await git
.commit(getInput('message'), matchGitArgs(getInput('commit') || ''))
.then(async (data) => {
log(undefined, data)
setOutput('committed', 'true')
setOutput('commit_sha', data.commit)
await git
.revparse(data.commit)
.then((long_sha) => setOutput('commit_long_sha', long_sha))
.catch((err) => core.warning(`Couldn't parse long SHA:\n${err}`))
})
.catch((err) => core.setFailed(err))
if (getInput('tag')) {
core.info('> Tagging commit...')
if (!fetchOption)
core.warning(
'Creating a tag without fetching the repo first could result in an error when pushing to GitHub. Refer to the action README for more info about this topic.'
)
await git
.tag(matchGitArgs(getInput('tag') || ''), (err, data?) => {
if (data) setOutput('tagged', 'true')
return log(err, data)
})
.then((data) => {
setOutput('tagged', 'true')
return log(null, data)
})
.catch((err) => core.setFailed(err))
} else core.info('> No tag info provided.')
let pushOption: string | boolean
try {
pushOption = getInput('push', true)
} catch {
pushOption = getInput('push')
}
if (pushOption) {
// If the options is `true | string`...
core.info('> Pushing commit to repo...')
if (pushOption === true) {
core.debug(
`Running: git push origin ${
getInput('new_branch') || ''
} --set-upstream`
)
await git.push(
'origin',
getInput('new_branch'),
{ '--set-upstream': null },
(err, data?) => {
if (data) setOutput('pushed', 'true')
return log(err, data)
}
)
} else {
core.debug(`Running: git push ${pushOption}`)
await git.push(
undefined,
undefined,
matchGitArgs(pushOption),
(err, data?) => {
if (data) setOutput('pushed', 'true')
return log(err, data)
}
)
}
if (getInput('tag')) {
core.info('> Pushing tags to repo...')
await git
.pushTags('origin', matchGitArgs(getInput('tag_push') || ''))
.then((data) => {
setOutput('tag_pushed', 'true')
return log(null, data)
})
.catch((err) => core.setFailed(err))
} else core.info('> No tags to push.')
} else core.info('> Not pushing anything.')
core.endGroup()
core.info('> Task completed.')
} else {
core.endGroup()
core.info('> Working tree clean. Nothing to commit.')
}
})()
.then(() => {
// Check for exit errors
if (exitErrors.length == 1) throw exitErrors[0]
else if (exitErrors.length > 1) {
exitErrors.forEach((e) => core.error(e))
throw 'There have been multiple runtime errors.'
}
})
.then(logOutputs)
.catch((e) => {
core.endGroup()
logOutputs()
core.setFailed(e)
})
async function add(ignoreErrors: 'all' | 'pathspec' | 'none' = 'none') {
const input = getInput('add')
if (!input) return []
const parsed = parseInputArray(input)
const res: (string | void)[] = []
for (const args of parsed) {
res.push(
// Push the result of every git command (which are executed in order) to the array
// If any of them fails, the whole function will return a Promise rejection
await git
.add(matchGitArgs(args), (err: any, data?: any) =>
log(ignoreErrors == 'all' ? null : err, data)
)
.catch((e: Error) => {
// if I should ignore every error, return
if (ignoreErrors == 'all') return
// if it's a pathspec error...
if (
e.message.includes('fatal: pathspec') &&
e.message.includes('did not match any files')
) {
if (ignoreErrors == 'pathspec') return
const peh = getInput('pathspec_error_handling'),
err = new Error(
`Add command did not match any file: git add ${args}`
)
if (peh == 'exitImmediately') throw err
if (peh == 'exitAtEnd') exitErrors.push(err)
} else throw e
})
)
}
return res
}
async function remove(
ignoreErrors: 'all' | 'pathspec' | 'none' = 'none'
): Promise<(void | Response<void>)[]> {
const input = getInput('remove')
if (!input) return []
const parsed = parseInputArray(input)
const res: (void | Response<void>)[] = []
for (const args of parsed) {
res.push(
// Push the result of every git command (which are executed in order) to the array
// If any of them fails, the whole function will return a Promise rejection
await git
.rm(matchGitArgs(args), (e: any, d?: any) =>
log(ignoreErrors == 'all' ? null : e, d)
)
.catch((e: Error) => {
// if I should ignore every error, return
if (ignoreErrors == 'all') return
// if it's a pathspec error...
if (
e.message.includes('fatal: pathspec') &&
e.message.includes('did not match any files')
) {
if (ignoreErrors == 'pathspec') return
const peh = getInput('pathspec_error_handling'),
err = new Error(
`Remove command did not match any file:\n git rm ${args}`
)
if (peh == 'exitImmediately') throw err
if (peh == 'exitAtEnd') exitErrors.push(err)
} else throw e
})
)
}
return res
}