-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (61 loc) · 1.87 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
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
const VERSION_REGEX = /## \[(.+)] - (.+)/
async function run() {
const octokit = github.getOctokit(core.getInput("github-token", { required: true }));
const changelogFile = core.getInput("path")
const entry = getLatestChangelogEntry(changelogFile)
const releaseTitle = core.getInput("title-template").replaceAll("{version}", entry.version)
const releaseTag = core.getInput("tag-template").replaceAll("{version}", entry.version)
if (!entry) {
console.log(`Could not find the latest release from ${changelogFile}`)
return
}
try {
await octokit.rest.repos.getReleaseByTag({
...github.context.repo,
tag: releaseTag
})
console.log(`A release already exists for ${releaseTag}, skipping`)
} catch (error) {
console.log(`Could not find a GitHub release for ${releaseTag}, creating one`)
octokit.rest.repos.createRelease({
...github.context.repo,
tag_name: releaseTag,
name: releaseTitle,
target_commitish: github.context.sha,
body: entry.changelog,
draft: false,
prerelease: false
})
}
}
function getLatestChangelogEntry(changelogFile) {
if (fs.existsSync(changelogFile)) {
const content = fs.readFileSync(changelogFile);
const entry = {
version: "",
changelog: ""
}
let inLatestEntry = false
for (const line of content.toString().split(/\r?\n/)) {
const result = line.match(VERSION_REGEX)
if (result) {
if (!inLatestEntry) {
// Started reading the latest entry
inLatestEntry = true;
entry.version = result[1];
} else {
break;
}
} else if (inLatestEntry) {
entry.changelog += line + "\n";
}
}
return entry;
}
}
run().catch(error => {
core.setFailed(error)
});