-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
66 lines (53 loc) · 1.6 KB
/
github.js
File metadata and controls
66 lines (53 loc) · 1.6 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
const { writeFileSync } = require("node:fs");
const { exec } = require("./util");
const path = require("node:path");
function isPrerelease(version) {
return /alpha|beta|rc/i.test(version);
}
function getRepoInfo() {
const repo = process.env.GITHUB_REPOSITORY;
if (!repo) {
throw new Error("GITHUB_REPOSITORY not found");
}
const [owner, name] = repo.split("/");
return { owner, repo: name };
}
function getSha() {
const sha = process.env.GITHUB_SHA;
if (!sha) {
throw new Error("GITHUB_SHA not found");
}
return sha;
}
function tagExists(tag) {
try {
const result = exec(
`gh api repos/${process.env.GITHUB_REPOSITORY}/git/matching-refs/tags/${tag}`
);
return JSON.parse(result).length > 0;
} catch {
return false;
}
}
function createTag(tag, sha) {
console.log(`Creating tag: ${tag}`);
exec(
`gh api repos/${process.env.GITHUB_REPOSITORY}/git/refs -X POST -f ref=refs/tags/${tag} -f sha=${sha}`,
{ stdio: "inherit" }
);
}
async function createRelease(version, tgzPath, body = "") {
const sha = getSha();
console.log(`Creating GitHub Release & Tag: ${version}`);
const prereleaseFlag = isPrerelease(version) ? "--prerelease" : "";
const notes = body || `Release ${version}`;
const assetPath = tgzPath ? path.resolve(tgzPath) : "";
let command = `gh release create ${version} ${assetPath} \
--target ${sha} \
--title "${version}" \
--notes "${notes}" \
${prereleaseFlag}`;
exec(command, { stdio: "inherit" });
console.log(`Release ${version} successfully published.`);
}
module.exports = { getRepoInfo, getSha, tagExists, createTag, createRelease };