forked from code-hike/codehike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelease.mjs
More file actions
97 lines (86 loc) · 2.77 KB
/
Copy pathrelease.mjs
File metadata and controls
97 lines (86 loc) · 2.77 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
import fs from "fs"
import { getExecOutput } from "@actions/exec"
import { pushTags } from "./git-utils.mjs"
import { PACKAGE_DIR, PUBLISH_COMMAND } from "./params.mjs"
import github from "@actions/github"
import { getChangelogEntry } from "./md-utils.mjs"
import { Octokit } from "@octokit/action"
const cwd = process.cwd()
const octokit = new Octokit({})
console.log("Writing .npmrc")
await fs.promises.writeFile(
`${process.env.HOME}/.npmrc`,
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`,
)
console.log("Running publish command")
const [publishCommand, ...publishArgs] = PUBLISH_COMMAND.split(/\s+/)
const output = await getExecOutput(publishCommand, publishArgs, { cwd })
if (output.exitCode !== 0) {
console.error(output.stderr)
process.exit(1)
}
const published = !output.stdout.includes("No unpublished projects to publish")
console.log(`Published: ${published}`)
console.log("Pushing tags")
await pushTags()
console.log("Creating GitHub release")
const pkg = JSON.parse(
await fs.promises.readFile(`${PACKAGE_DIR}/package.json`, "utf8"),
)
const changelog = await fs.promises.readFile(
`${PACKAGE_DIR}/CHANGELOG.md`,
"utf8",
)
const entry = getChangelogEntry(changelog, pkg.version)
const tag = `${pkg.name}@${pkg.version}`
await octokit.rest.repos.createRelease({
...github.context.repo,
name: tag,
tag_name: tag,
body: entry.content,
})
console.log("Getting all released issues")
const query = `query ($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
pullRequests(first: 100, labels: ["changeset"], states: MERGED) {
edges {
node {
number
closingIssuesReferences(first: 10) {
nodes {
number
}
}
}
}
}
}
}`
const { repository } = await octokit.graphql(query, github.context.repo)
const prNumbers = repository.pullRequests.edges.map(({ node }) => node.number)
const issueNumbers = repository.pullRequests.edges.flatMap(({ node }) =>
node.closingIssuesReferences.nodes.map(({ number }) => number),
)
console.log("Commenting issues", issueNumbers)
await Promise.all(
issueNumbers.map(async (number) => {
const commentBody = `🚀 Released in ${"`" + tag + "`"} 🚀
If this was helpful, consider **[sponsoring Code Hike](https://github.com/sponsors/code-hike?metadata_source=Issue)**. Your contribution helps sustain open-source work.
Thanks for using Code Hike!`
await octokit.issues.createComment({
...github.context.repo,
issue_number: number,
body: commentBody,
})
}),
)
console.log("Updating PRs", prNumbers)
await Promise.all(
prNumbers.map(async (number) => {
await octokit.issues.removeLabel({
...github.context.repo,
issue_number: number,
name: "changeset",
})
}),
)