generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 7
/
push.js
84 lines (68 loc) · 3.07 KB
/
push.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
const core = require('@actions/core');
const exec = require('@actions/exec');
const github = require('@actions/github');
const utils = require('../utils');
async function commentOnPullRequest(octokit, pullRequest) {
// if a PR comment was supplied, add it to the pull request
const comment = core.getInput('pr_comment');
if (comment) {
core.info(`Adding comment ${comment}`);
core.warning('"pr_comment" is deprecated. Please use the "pr_url" and "pr_number" step outputs to script actions on an available pull request.');
await octokit.rest.issues.createComment({
...github.context.repo,
issue_number: pullRequest.number,
body: comment,
});
}
}
async function run() {
let licensesUpdated = false;
const branch = utils.getBranch(github.context);
core.setOutput('licenses_branch', branch);
core.setOutput('user_branch', branch);
const { command, options } = await utils.getLicensedInput();
// pre-check, if status succeeds no need to re-cache
let statusResult = await core.group('Pre-Checking license status', async () => {
return utils.checkStatus(command, options);
});
if (statusResult.success) {
return;
}
const [localBranch] = await utils.ensureBranch(branch, branch, false);
// find an open pull request for the changes if one exists
const octokit = utils.newOctokit();
const pullRequest = await utils.findPullRequest(octokit, { head: branch });
// cache any metadata updates
await core.group('Refreshing cache', async () => {
await exec.exec(command, ['cache', ...options.cacheOptions]);
// stage any changes, checking only configured cache paths if possible
const cachePaths = await utils.getCachePaths(command, options);
await exec.exec('git', ['add', '--', ...(await utils.filterCachePaths(cachePaths))]);
// check for any changes, checking only configured cache paths if possible
const exitCode = await exec.exec('git', ['diff-index', '--quiet', 'HEAD', '--', ...cachePaths], { ignoreReturnCode: true });
if (exitCode > 0) {
// if files were changed, push them back up to origin using the passed in github token
await exec.exec('git', [...utils.userConfig(), 'commit', '-m', utils.getCommitMessage(github.context)]);
const extraHeadersConfig = await utils.extraHeaderConfigWithoutAuthorization();
await exec.exec('git', [...extraHeadersConfig, 'push', utils.getOrigin(), `${localBranch}:${branch}`]);
licensesUpdated = true;
// if a PR comment was supplied and PR exists, add comment
if (pullRequest) {
await commentOnPullRequest(octokit, pullRequest);
}
}
core.setOutput('licenses_updated', licensesUpdated.toString());
});
if (pullRequest) {
core.setOutput('pr_url', pullRequest.html_url);
core.setOutput('pr_number', pullRequest.number);
}
// after re-caching, check status
await core.group('Check license status', async () => {
statusResult = await utils.checkStatus(command, options);
if (!statusResult.success) {
throw new Error('Cached metadata checks failed');
}
});
}
module.exports = { run };