-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (96 loc) · 3.98 KB
/
Copy pathindex.js
File metadata and controls
111 lines (96 loc) · 3.98 KB
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
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs')
// most @actions toolkit packages have async methods
async function run() {
core.info("Starting...")
try {
// Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
// Get owner and repo from context of payload that triggered the action
const { owner: currentOwner, repo: currentRepo } = github.context.repo;
// get workflow inputs
const cargo_path = core.getInput('cargo_path', { required: false });
const body = core.getInput('body', { required: false });
const body_path = core.getInput('body_path', { required: false });
const owner = core.getInput('owner', { required: false }) || currentOwner;
const repo = core.getInput('repo', { required: false }) || currentRepo;
const dry_run = core.getInput('dry_run', { required: false });
// initialize outputs
id = null;
tag_name = null;
html_url = null;
upload_url = null;
is_new_release = null;
core.info("Getting content of the body...");
let bodyFileContent = null;
if (body_path !== '' && !!body_path) {
try {
bodyFileContent = fs.readFileSync(body_path, { encoding: 'utf8' });
} catch (error) {
core.setFailed(error.message);
}
}
core.info("Getting cargo file contents...");
const cargo_content = fs.readFileSync(cargo_path, 'utf8').toString();
core.info("Getting crate version...");
let cargo_version_re = /version *= *"(?<version>[a-zA-Z0-9._-]+)"/u;
let cargo_version_result = cargo_version_re.exec(cargo_content);
let cargo_version = cargo_version_result.groups.version;
core.info(`Got crate version ${cargo_version}`);
// check current releases for existing version
const release_name = `v${cargo_version}`
const releases = await octokit.rest.repos.listReleases({
owner: owner,
repo: repo,
});
release = null
const existing = releases.data.some(i => i.name === release_name);
if (existing) {
release = releases.data.filter(i => i.name === release_name)[0];
core.info(`Skipping: Release with tag ${cargo_version} already exists`);
core.notice(`Release with tag ${cargo_version} already exists`);
is_new_release = 'false';
} else {
core.info(`Creating release with tag ${cargo_version}...`)
if(dry_run === 'false') {
is_new_release = 'true';
release = await await octokit.rest.repos.createRelease({
owner: owner,
repo: repo,
tag_name: release_name,
name: release_name,
body: bodyFileContent || body || `Release ${release_name}`,
});
} else {
is_new_release = 'false';
core.info(`Would create release with tag ${cargo_version}, but this is a dry run.`);
}
core.notice(`Created release with tag ${cargo_version}`);
}
// Set output variables
core.setOutput('cargo_version', cargo_version);
if (release != null) {
// Set the output variables for use by other actions: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs
id = release.id
tag_name = release.tag_name
html_url = release.html_url
upload_url = release.upload_url
}
core.setOutput('id', id);
core.setOutput('tag_name', tag_name);
core.setOutput('html_url', html_url);
core.setOutput('upload_url', upload_url);
core.setOutput('is_new_release', is_new_release);
// Log the output variables
core.info(`Cargo Version: ${cargo_version}`);
core.info(`Release ID: ${id}`);
core.info(`Release Tag Name: ${tag_name}`);
core.info(`Release HTML URL: ${html_url}`);
core.info(`Release Upload URL: ${upload_url}`);
core.info(`Is new release: ${is_new_release}`);
} catch (error) {
core.setFailed(error.message);
}
}
run();