Skip to content

Commit b38fdaf

Browse files
authored
Merge pull request #8 from OPCODE-Open-Spring-Fest/dummy
ci: added label checker to prevent pre merges
2 parents 07be9ca + 3ddf703 commit b38fdaf

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

.github/PULL_REQUEST_TEMPLATE/pull_request_template.md renamed to .github/pull_request_template.md

File renamed without changes.

.github/workflows/checklabels.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Label Checker
2+
3+
on:
4+
pull_request:
5+
types: [opened, edited, synchronize, reopened, labeled, unlabeled]
6+
7+
jobs:
8+
check-labels:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- name: Set up Node.js
13+
uses: actions/setup-node@v3
14+
with:
15+
node-version: 16
16+
- name: Install dependencies
17+
run: npm install @actions/github @actions/core
18+
- name: Run Label Checker
19+
run: node .github/workflows/label-checker.js
20+
env:
21+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/label-checker.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getOctokit, context } from '@actions/github';
2+
import { setFailed } from '@actions/core';
3+
4+
async function run() {
5+
try {
6+
const token = process.env.GITHUB_TOKEN;
7+
const octokit = new getOctokit(token);
8+
9+
const pullRequest = context.payload.pull_request;
10+
const owner = pullRequest.base.repo.owner.login;
11+
const repo = pullRequest.base.repo.name;
12+
const pullNumber = pullRequest.number;
13+
14+
const { data: labels } = await octokit.rest.issues.listLabelsOnIssue({
15+
owner,
16+
repo,
17+
issue_number: pullNumber,
18+
});
19+
20+
const labelNames = labels.map((label) => label.name);
21+
22+
const requiredLabels = [
23+
['Type:Easy', 'Type:Medium', 'Type:Hard'],
24+
['Semver:major', 'Semver:minor', 'Semver:patch'],
25+
['PR:Accept'],
26+
];
27+
28+
const hasRequiredLabels = requiredLabels.every((labelGroup) =>
29+
labelGroup.some((label) => labelNames.includes(label))
30+
);
31+
32+
if (!hasRequiredLabels) {
33+
setFailed(
34+
'This pull request must have at least one label from each of the following groups: Type (Easy, Medium, Hard), Semver (Major, Minor, Patch), and PR:Accept.'
35+
);
36+
}
37+
} catch (error) {
38+
setFailed(error.message);
39+
}
40+
}
41+
42+
run();

0 commit comments

Comments
 (0)