Skip to content

Commit 7144f10

Browse files
chore(ci): fixed release notification action; (#6063)
1 parent f6d2cf9 commit 7144f10

File tree

7 files changed

+86
-18
lines changed

7 files changed

+86
-18
lines changed

.github/workflows/notify.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: notify
2+
3+
on:
4+
#workflow_run:
5+
# workflows: ["publish"]
6+
# types:
7+
# - completed
8+
#repository_dispatch:
9+
# types: [ notify ]
10+
release:
11+
types: [ published ]
12+
workflow_dispatch:
13+
inputs:
14+
tag:
15+
required: true
16+
jobs:
17+
notify:
18+
runs-on: ubuntu-latest
19+
#if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}
20+
steps:
21+
#- name: Dump GitHub context
22+
# env:
23+
# GITHUB_CONTEXT: ${{ toJson(github) }}
24+
# run: echo "$GITHUB_CONTEXT"
25+
- uses: actions/checkout@v3
26+
with:
27+
fetch-depth: 0
28+
- name: git config
29+
run: |
30+
git config user.name "${GITHUB_ACTOR}"
31+
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
32+
- name: Setup node
33+
uses: actions/setup-node@v3
34+
with:
35+
node-version: 18
36+
cache: npm
37+
- run: npm ci
38+
- name: Notify published PRs
39+
env:
40+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
run: node ./bin/actions/notify_published.js --tag ${{ github.event.inputs.tag || github.event.release.tag_name }}

.github/workflows/publish.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,3 @@ jobs:
5656
run: npm publish --provenance --access public
5757
env:
5858
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
59-
###### NOTIFY & TAG published PRs ######
60-
- name: Notify and tag published PRs
61-
env:
62-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
63-
run: node ./bin/actions/notify_published.js --tag v${{ steps.package-version.outputs.current-version }}

bin/GithubAPI.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ export default class GithubAPI {
105105
} catch (e) {
106106
}
107107
}
108+
109+
static async getLatestTag() {
110+
try{
111+
const {stdout} = await exec(`git for-each-ref refs/tags --sort=-taggerdate --format='%(refname)' --count=1`);
112+
113+
return stdout.split('/').pop();
114+
} catch (e) {}
115+
}
116+
117+
static normalizeTag(tag){
118+
return tag ? 'v' + tag.replace(/^v/, '') : '';
119+
}
108120
}
109121

110122
const {prototype} = GithubAPI;

bin/RepoBot.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import fs from "fs/promises";
55
import {colorize} from "./helpers/colorize.js";
66
import {getReleaseInfo} from "./contributors.js";
77

8-
const normalizeTag = (tag) => tag.replace(/^v/, '');
8+
const normalizeTag = (tag) => tag ? 'v' + tag.replace(/^v/, '') : '';
99

1010
class RepoBot {
1111
constructor(options) {
@@ -29,7 +29,17 @@ class RepoBot {
2929
}
3030

3131
async notifyPRPublished(id, tag) {
32-
const pr = await this.github.getPR(id);
32+
let pr;
33+
34+
try {
35+
pr = await this.github.getPR(id);
36+
} catch (err) {
37+
if(err.response?.status === 404) {
38+
throw new Error(`PR #${id} not found (404)`);
39+
}
40+
41+
throw err;
42+
}
3343

3444
tag = normalizeTag(tag);
3545

@@ -41,7 +51,7 @@ class RepoBot {
4151
return false
4252
}
4353

44-
await this.github.appendLabels(id, ['v' + tag]);
54+
await this.github.appendLabels(id, [tag]);
4555

4656
if (isBot || labels.find(({name}) => name === 'automated pr') || (await this.github.isCollaborator(login))) {
4757
return false;
@@ -56,14 +66,16 @@ class RepoBot {
5666
author,
5767
release: {
5868
tag,
59-
url: `https://github.com/${this.owner}/${this.repo}/releases/tag/v${tag}`
69+
url: `https://github.com/${this.owner}/${this.repo}/releases/tag/${tag}`
6070
}
6171
});
6272

6373
return await this.addComment(id, message);
6474
}
6575

6676
async notifyPublishedPRs(tag) {
77+
tag = normalizeTag(tag);
78+
6779
const release = await getReleaseInfo(tag);
6880

6981
if (!release) {
@@ -80,9 +92,9 @@ class RepoBot {
8092
try {
8193
console.log(colorize()`${i++}) Notify PR #${pr.id}`)
8294
const result = await this.notifyPRPublished(pr.id, tag);
83-
console.log(result ? 'OK' : 'Skipped');
95+
console.log('✔️', result ? 'Label, comment' : 'Label');
8496
} catch (err) {
85-
console.warn(colorize('green', 'red')` Failed notify PR ${pr.id}: ${err.message}`);
97+
console.warn(colorize('green', 'red')` Failed notify PR ${pr.id}: ${err.message}`);
8698
}
8799
}
88100
}

bin/actions/notify_published.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import minimist from "minimist";
22
import RepoBot from '../RepoBot.js';
3+
import fs from 'fs/promises';
34

45
const argv = minimist(process.argv.slice(2));
56
console.log(argv);
67

7-
const tag = argv.tag;
8+
let {tag} = argv;
89

9-
if (!tag) {
10-
throw new Error('tag must be specified');
11-
}
10+
(async() => {
11+
if (!tag || tag === true) {
12+
const {version} = JSON.parse((await fs.readFile('./package.json')).toString());
1213

13-
const bot = new RepoBot();
14+
tag = 'v' + version;
15+
} else if (typeof tag !== 'string') {
16+
17+
throw new Error('tag must be a string');
18+
}
19+
20+
const bot = new RepoBot();
1421

15-
(async() => {
1622
try {
1723
await bot.notifyPublishedPRs(tag);
1824
} catch (err) {

bin/githubAxios.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import axios from '../index.js';
22

33
const {GITHUB_TOKEN} = process.env;
44

5+
GITHUB_TOKEN ? console.log(`[GITHUB_TOKEN OK]`) : console.warn(`[GITHUB_TOKEN is not defined]`);
6+
57
export default axios.create({
68
headers: {
79
Authorization: GITHUB_TOKEN ? `token ${GITHUB_TOKEN}` : null

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,4 @@
215215
"@commitlint/config-conventional"
216216
]
217217
}
218-
}
218+
}

0 commit comments

Comments
 (0)