-
Notifications
You must be signed in to change notification settings - Fork 19
147 lines (129 loc) · 4.91 KB
/
snapshot.yml
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
# Based on https://github.com/Shopify/polaris/blob/e6157f13d146b3add9c7a227d2d5481278251fac/.github/workflows/snapit.yml
name: Snapshot
on:
issue_comment:
types:
- created
permissions: write-all
concurrency: ${{ github.workflow }}-${{ github.ref }}
jobs:
snapshot:
name: Snapshot Release
if: |
github.event.issue.pull_request &&
(github.event.comment.body == '/release-pr' || github.event.comment.body == '/snapshot-release')
runs-on: ubuntu-latest
steps:
- name: Enforce write permissions
uses: actions/github-script@v7
env:
COMMENT_ID: ${{ github.event.comment.id }}
with:
script: |
const username = context.actor;
const owner = context.repo.owner;
const repo = context.repo.repo;
const response = await github.rest.repos.getCollaboratorPermissionLevel({
owner,
repo,
username,
});
const permission = response.data.permission
if (permission === "write" || permission === "admin") {
await github.rest.reactions.createForIssueComment({
owner,
repo,
comment_id: process.env.COMMENT_ID,
content: "eyes",
})
} else {
core.setFailed(
`🚨 Insufficient Permissions! ${username} does not have write permissions`
)
}
- name: Validate pull request
uses: actions/github-script@v7
id: pr_data
env:
FORK: ${{ github.event.pull_request.head.repo.fork }}
with:
script: |
try {
// Pull request from fork
if (process.env.FORK) {
const errorMessage = 'snapshot releases not supported on pull requests from forked repositories.'
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: errorMessage,
})
core.setFailed(errorMessage)
}
} catch (err) {
core.setFailed(`Request failed with error ${err}`)
}
- name: Checkout pull request
uses: actions/checkout@v4
with:
ref: refs/pull/${{ github.event.issue.number }}/head
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'yarn'
registry-url: 'https://registry.npmjs.org'
- name: Install dependencies
run: yarn install
- name: Exit pre mode if pre.json exists
run: rm -f .changeset/pre.json
- name: Create and publish snapshot release
uses: actions/github-script@v7
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMENT_ID: ${{ github.event.comment.id }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_CONFIG_PROVENANCE: true
with:
script: |
await exec.exec('yarn changeset version --snapshot snapshot');
await exec.exec('yarn build');
let output = ''
const options = {
listeners: {
stdout: (data) => {
output += data.toString();
},
}
};
await exec.exec('yarn changeset publish --no-git-tag --snapshot --tag snapshot', [], options);
const { COMMENT_ID } = process.env
const [_, packagesStdin] = output.split("success packages published successfully:\n")
const newTags = packagesStdin ? packagesStdin.split("\n").flatMap(package => Array.from(package.matchAll(/\s+(.*)/g),m => m[1])) : [];
if (newTags.length) {
const multiple = newTags.length > 1
const body = (
`🫰✨ **Thanks @${context.actor}! ` +
`Your snapshot${multiple ? 's have' : ' has'} been published to npm.**\n\n` +
`Test the snapshot${multiple ? 's' : ''} by updating your \`package.json\` ` +
`with the newly published version${multiple ? 's' : ''}:\n` +
newTags.map(tag => (
'```sh\n' +
`yarn add ${tag}\n` +
'```'
)).join('\n')
)
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
})
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: COMMENT_ID,
content: "rocket",
})
}