-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (40 loc) · 1.57 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
const Core = require('@actions/core');
const Github = require('@actions/github');
const Path = require("path")
const fs = require('fs')
const {globSync} = require('glob')
try {
// Get octokit
const Octokit = Github.getOctokit(Core.getInput('token'));
// Read & parse release note
let note_content = fs.readFileSync(Path.resolve(Core.getInput('note')), {encoding: 'utf8'});
const [tag_str, tag_name] = note_content.match(/^\`(.*)\`\n/);
note_content = note_content.substring(tag_str.length);
const [name_str, release_name] = note_content.match(/^#\s+(.*)\n/);
note_content = note_content.substring(name_str.length);
// Get assets
const asset_paths = Core.getInput('assets').length ? Core.getInput('assets').split("\n").reduce((prev, cur) => prev.concat(globSync(cur.trim())), []) : [];
// Create release
Octokit.rest.repos.createRelease({
owner: Github.context.repo.owner,
repo: Github.context.repo.repo,
tag_name: tag_name,
target_commitish: Github.context.sha,
name: release_name,
body: note_content,
draft: Core.getInput('draft', {trimWhitespace: true}) == "true"
})
// Upload assets
.then(res => Promise.all(asset_paths.map(asset => Octokit.rest.repos.uploadReleaseAsset({
owner: Github.context.repo.owner,
repo: Github.context.repo.repo,
release_id: res.data.id,
name: Path.basename(asset),
data: fs.readFileSync(asset)
}))))
.then(() => {
console.log("Release created!")
})
} catch (error) {
Core.setFailed(error.message);
}