-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (134 loc) · 5.1 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// noinspection JSUnresolvedFunction,JSIgnoredPromiseFromCall
const core = require('@actions/core')
const github = require('@actions/github')
const shell = require('shelljs')
const fs = require('fs')
const actionToken = core.getInput('actionToken')
const manifestFileName = core.getInput('manifestFileName')
const manifestProtectedTrue = core.getInput('manifestProtectedTrue')
const octokit = github.getOctokit(actionToken)
const owner = github.context.payload.repository.owner.login
const repo = github.context.payload.repository.name
const committer_email = github.context.payload.head_commit.committer.email
const committer_username = github.context.payload.head_commit.committer.username
const zipName = `${github.context.payload.repository.name}.zip`
async function createRelease(versionNumber, commitLog) {
try {
return await octokit.rest.repos.createRelease({
owner: owner,
repo: repo,
tag_name: `${versionNumber}`,
name: `${versionNumber}`,
body: `Release ${versionNumber}\n\n## Release Notes:\n${commitLog}`,
draft: true,
})
} catch (error) {
core.setFailed(error.message)
}
}
async function getCommitLog() {
try {
// Get The Latest Release
console.log(`Get Latest Release for ${owner}/${repo}`)
const latestRelease = await octokit.rest.repos.getLatestRelease({
owner: owner,
repo: repo,
})
// Get Commits Since That Release's Date
console.log(`Get Latest Commits for ${owner}/${repo}`)
const commitList = await octokit.rest.repos.listCommits({
owner: owner,
per_page: 100,
repo: repo,
sha: "main",
since: latestRelease.data.created_at,
})
let commitListMarkdown = ""
commitList.data
.filter(
(commit) =>
!commit.commit.author.name.includes("bot") &&
!commit.commit.message.includes("version.txt")
)
.forEach((commit) => {
commitListMarkdown += `* ${commit.commit.message} (${commit.commit.author.name})\n`;
})
return commitListMarkdown
} catch (error) {
console.log(error)
core.setFailed(error.message)
}
}
async function uploadAssets(releaseResponse) {
try {
// Upload Zip
const zipData = await fs.readFileSync(zipName)
await octokit.rest.repos.uploadReleaseAsset({
owner: owner,
repo: repo,
release_id: releaseResponse.data.id,
name: zipName,
data: zipData
})
// Upload Manifest
const manifestData = await fs.readFileSync(manifestFileName, 'utf-8')
await octokit.rest.repos.uploadReleaseAsset({
owner: owner,
repo: repo,
release_id: releaseResponse.data.id,
name: manifestFileName,
data: manifestData
})
} catch (error) {
core.setFailed(error.message)
}
}
async function run() {
try {
// Validate manifestFileName
if (manifestFileName !== 'system.json' && manifestFileName !== 'module.json')
core.setFailed('manifestFileName must be system.json or module.json')
// Get versionNumber from version.txt
let versionNumber = await fs.readFileSync('version.txt', 'utf-8')
versionNumber = `v${versionNumber.trim()}`
// Set up Download URLs
let downloadURL = `https://github.com/${owner}/${repo}/releases/download/${versionNumber}/${repo}.zip`
let manifestURL = `https://github.com/${owner}/${repo}/releases/download/${versionNumber}/${manifestFileName}`
let manifestProtectedValue = 'false'
if (manifestProtectedTrue === 'true') {
downloadURL = ""
manifestURL = `https://raw.githubusercontent.com/${owner}/dcc-content/main/${repo}/${versionNumber}/${manifestFileName}`
manifestProtectedValue = 'true'
}
// Replace Data in Manifest
fs.readdirSync(".").forEach(file => {
console.log(file);
});
const data = fs.readFileSync(manifestFileName, 'utf8')
const formatted = data
.replace(/"version": .*,/i, `"version": "${versionNumber.replace('v', '')}",`)
.replace(/"download": .*,/i, `"download": "${downloadURL}",`)
.replace(/"manifest": .*,/i, `"manifest": "${manifestURL}",`)
.replace(/"protected": .*,/i, `"protected": ${manifestProtectedValue},`)
fs.writeFileSync(manifestFileName, formatted, 'utf8')
// Git List of Commits Since Last Release
console.log("Get Commit Log")
const commitLog = await getCommitLog()
// Create Release
console.log("Create Release")
const releaseResponse = await createRelease(versionNumber, commitLog)
await shell.exec(`git config user.email "${committer_email}"`)
await shell.exec(`git config user.name "${committer_username}"`)
await shell.exec(`git commit -am "Release ${versionNumber}"`)
await shell.exec(`git archive -o ${zipName} HEAD`)
await uploadAssets(releaseResponse)
// Log Results
console.log(`**** Version ${versionNumber} Release Created!`)
console.log('**** URLs Embedded in Manifest:')
console.log(`** Download URL: ${downloadURL}`)
console.log(`** Manifest URL: ${manifestURL}`)
} catch (error) {
core.setFailed(error.message)
}
}
run()