-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (65 loc) · 1.79 KB
/
index.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
const { Octokit } = require('@octokit/rest');
const git = require('simple-git');
const core = require('@actions/core');
const github = require('@actions/github');
const fetch = require('node-fetch').default;
const {
validateInputs,
isCommitMadeByAction,
checkoutBranch,
commitChanges,
runCommand,
createPullRequest
} = require('./utils');
async function run() {
try {
// Get inputs
const inputs = {
githubToken: core.getInput('github-token'),
branchName: core.getInput('branch-name'),
userName: core.getInput('user-name'),
userEmail: core.getInput('user-email')
};
validateInputs(inputs);
const versionCommand = core.getInput('version-command');
const publishCommand = core.getInput('publish-command');
const commitMsg = core.getInput('commit-message');
const commitTitle = core.getInput('commit-title');
// Get the event that triggered the action
const { context } = github;
// Set up Git
const gitClient = git();
await gitClient.addConfig('user.name', inputs.userName);
await gitClient.addConfig('user.email', inputs.userEmail);
// Check if the commit is made by this action
const isActionCommit = await isCommitMadeByAction(
gitClient,
inputs.userName,
inputs.userEmail
);
// if so run the only publish command and exit
if (isActionCommit) {
await runCommand(publishCommand);
return;
}
// Run the version command on new branch
await checkoutBranch(gitClient, inputs.branchName);
await runCommand(versionCommand);
await commitChanges(gitClient, commitMsg, inputs.branchName);
const octokit = new Octokit({
auth: inputs.githubToken,
request: {
fetch: fetch
}
});
await createPullRequest(
octokit,
context,
commitTitle,
inputs.branchName
);
} catch (error) {
core.setFailed(error.message);
}
}
run();