Skip to content

Commit e956485

Browse files
author
Ryan
committed
chore: 👷 cleanup action
1 parent c242be4 commit e956485

File tree

2 files changed

+41
-58
lines changed

2 files changed

+41
-58
lines changed

.github/workflows/remove-old-lokalise.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ jobs:
1111
with:
1212
node-version: '12'
1313
- name: remove old lokalise entries
14+
env:
15+
TOKEN: ${{ secrets.GITHUB_TOKEN }}
1416
run: node remove-old-lokalise-prs.js token=${{ secrets.GITHUB_TOKEN }}

remove-old-lokalise-prs.js

Lines changed: 39 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,52 @@
11
const github = require("@actions/github");
2-
const token = process.argv.slice(2).join('').substring(6);
3-
const owner = github.context.repo.owner;
4-
const repo = github.context.repo.repo;
5-
const octo = github.getOctokit(token);
6-
7-
const pullRequests = () => {
8-
var response = octo.rest.pulls.list({
9-
owner: owner,
10-
repo: repo,
11-
})["catch"]((e) => {
12-
console.log(e.message)
13-
});
14-
return response;
15-
}
16-
17-
const parsePullRequestId = githubRef => {
18-
const result = /refs\/pull\/(\d+)\/merge/g.exec(githubRef);
19-
if (!result) throw new Error("Reference not found.");
20-
const [, pullRequestId] = result;
21-
return pullRequestId;
22-
};
232

243
async function main() {
254

26-
// current pull request id
27-
const pullRequestId = parsePullRequestId(process.env.GITHUB_REF);
28-
29-
await pullRequests().then(prs => {
5+
const token = process.env.TOKEN;
6+
const owner = github.context.repo.owner;
7+
const repo = github.context.repo.repo;
8+
const octo = github.getOctokit(token);
9+
const currentPR = github.context.issue.number;
10+
11+
//Fetch PRs
12+
octo.rest.pulls.list({
13+
owner: owner,
14+
repo: repo,
15+
}).then(prs => {
16+
//Filter PRs
3017
let filteredPrs = prs.data.filter(pr => {
3118
let regex = new RegExp('^Lokalise:[ _a-zA-Z0-9]+');
32-
return regex.test(pr.title) && pr.number != pullRequestId;
19+
return regex.test(pr.title) && pr.number != currentPR;
3320
});
34-
35-
//if no extra PRs under Lokalise, exit
36-
if (!filteredPrs.length > 0) return true;
37-
38-
//otherwise, go ahead and set to close and delete the branch.
39-
filteredPrs.forEach(pr => {
40-
//Close Branch
41-
octo.rest.pulls.update({
21+
if (filteredPrs && filteredPrs.length) {
22+
filteredPrs.forEach(pr => {
23+
//Close Branch
24+
octo.rest.pulls.update({
25+
owner: owner,
26+
repo: repo,
27+
pull_number: pr.number,
28+
state: 'closed'
29+
});
30+
//Delete Branch
31+
octo.rest.pulls.get({
4232
owner: owner,
4333
repo: repo,
44-
pull_number: pr.number,
45-
state: 'closed'
34+
pull_number: pr.number
35+
}).then(({ data }) => {
36+
const ref = 'heads/' + data['head']['ref'];
37+
try {
38+
octo.rest.git.deleteRef({
39+
owner: owner,
40+
repo: repo,
41+
ref
42+
});
43+
} catch ({ message }) { console.log(message) }
44+
});
4645
});
47-
octo.rest.pulls.get({
48-
owner: owner,
49-
repo: repo,
50-
pull_number: pr.number
51-
}).then(({ data }) => {
52-
const ref = 'heads/' + data['head']['ref'];
53-
try {
54-
//Delete Branch
55-
octo.rest.git.deleteRef({
56-
owner: owner,
57-
repo: repo,
58-
ref
59-
});
60-
}
61-
catch (e) {
62-
console.log(e.message)
63-
}
64-
});
65-
});
46+
}
47+
}).catch(e => {
48+
console.error(e);
6649
});
6750
};
6851

69-
main().catch(err => {
70-
console.error(err);
71-
});;
52+
main();

0 commit comments

Comments
 (0)