-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.js
121 lines (104 loc) · 3.89 KB
/
action.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
const core = require('@actions/core');
const github = require('@actions/github');
const process = require('process');
const fs = require('fs/promises');
const config = require('config');
const crypto = require('crypto');
const EOF = crypto.randomBytes(16).toString("hex"); // see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
const GITHUB_ACTION_OUTPUT = process.env.GITHUB_OUTPUT || config.get('GITHUB_OUTPUT'); // see more information here https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-setting-an-output-parameter
const customRepo = (repoPath) => {
const segments = repoPath.split('/', 2);
if (segments.length < 2) {
core.info('Please provide a repository in the format `owner/repo`.')
}
return segments;
}
const repoInput = core.getInput('repo_path');
const [owner, repo] = repoInput
? customRepo(repoInput)
: config.get('GH_REPOSITORY').split('/', 2);
const octokit = new github.GitHub(
core.getInput('github_token', { required: true })
)
async function appendGHOutputfile(content) {
try {
await fs.appendFile(GITHUB_ACTION_OUTPUT, content);
} catch (error) {
core.info('Could not write to the GITHUB_OUTPUT environment file.');
core.setFailed(`Action failed with error ${error}`);
}
}
async function run() {
let latestRelease
core.info(`Fetching the latest release for \`${owner}/${repo}\``)
try {
latestRelease = await octokit.repos.getLatestRelease({
owner,
repo,
})
} catch (error) {
core.info('Could not fetch the latest release. Have you made one yet?');
core.setFailed(`Action failed with error ${error}`);
}
const { data } = latestRelease;
const releaseAttributes = [
'url',
'assets_url',
'upload_url',
'html_url',
'id',
'node_id',
'tag_name',
'target_commitish',
'name',
'body',
'draft',
'prerelease',
'author_id',
'author_node_id',
'author_url',
'author_login',
'author_html_url',
'author_type',
'author_site_admin'
];
let github_output = "";
for (i=0; i < releaseAttributes.length; i++) {
switch(true) {
case (releaseAttributes[i] == "author_id"): // ex. author_id > data.author.id.toString()
tempData = data.author.id.toString();
github_output = releaseAttributes[i] + '=' + tempData + "\n" + github_output;
break;
case (releaseAttributes[i].includes("author")): // ex. author_url > data.author.url
tempAuthorAttribute = releaseAttributes[i];
tempAuthorAttribute = tempAuthorAttribute.replace("author_", "");
github_output = releaseAttributes[i] + '=' + data.author[tempAuthorAttribute] + "\n" + github_output;
break;
case (releaseAttributes[i] == "id"): // ex. id > data.id.toString()
tempData = data[releaseAttributes[i]].toString();
github_output = releaseAttributes[i] + '=' + tempData + "\n" + github_output;
break;
case (releaseAttributes[i] == "body"):
// see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#multiline-strings
github_output = releaseAttributes[i] + '<<' + EOF + '\n' + data[releaseAttributes[i]] + '\n' + EOF + "\n" + github_output;
break;
default: // ex. assets_url > data.assets_url
github_output = releaseAttributes[i] + '=' + data[releaseAttributes[i]] + "\n" + github_output;
}
}
try {
await appendGHOutputfile(github_output);
} catch (error) {
core.setFailed(`Action failed with error ${error}`);
}
}
try {
if(process.env.NODE_ENV != "test") { // don't execute run when jest tests are running as run is exported for jest testing
run();
}
} catch (error) {
core.setFailed(`Action failed with error ${error}`);
}
module.exports.run = run;
module.exports.EOF = EOF;
module.exports.GITHUB_ACTION_OUTPUT = GITHUB_ACTION_OUTPUT;