Skip to content

Commit e1b6efa

Browse files
authored
i hate this shit
Refactor PR template enforcement workflow to streamline validation and membership checks. Signed-off-by: Lala Sabathil <lala@pycord.dev>
1 parent 9f60e79 commit e1b6efa

File tree

1 file changed

+25
-65
lines changed

1 file changed

+25
-65
lines changed

.github/workflows/enforce-pr-template.yml

Lines changed: 25 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -29,68 +29,45 @@ jobs:
2929
echo "number=${{ github.event.pull_request.number }}" >> "$GITHUB_OUTPUT"
3030
fi
3131
32-
- name: Load PR details
33-
id: load_pr
32+
- name: Enforce template
3433
uses: actions/github-script@v8
3534
with:
3635
github-token: ${{ secrets.GITHUB_TOKEN }}
3736
script: |
3837
const prNumber = Number('${{ steps.pr.outputs.number }}');
38+
const org = 'Pycord-Development';
39+
const INVALID_LABEL = 'invalid';
40+
41+
// 1) Load PR
3942
const { data: pr } = await github.rest.pulls.get({
4043
owner: context.repo.owner,
4144
repo: context.repo.repo,
4245
pull_number: prNumber,
4346
});
4447
45-
core.setOutput('author', pr.user.login);
46-
core.setOutput('state', pr.state);
47-
core.setOutput('number', pr.number.toString());
48-
49-
- name: Check if author is org member
50-
id: org_check
51-
uses: actions/github-script@v8
52-
with:
53-
github-token: ${{ secrets.GITHUB_TOKEN }}
54-
script: |
55-
const org = 'Pycord-Development';
56-
const username = '${{ steps.load_pr.outputs.author }}';
48+
const author = pr.user.login;
5749
50+
// 2) Check org membership
51+
let isMember = false;
5852
try {
59-
await github.rest.orgs.checkMembershipForUser({ org, username });
60-
core.setOutput('is_member', 'true');
53+
await github.rest.orgs.checkMembershipForUser({ org, username: author });
54+
isMember = true;
6155
} catch (error) {
62-
if (error.status === 404) {
63-
core.setOutput('is_member', 'false');
64-
} else {
56+
if (error.status !== 404) {
6557
throw error;
6658
}
6759
}
6860
69-
- name: Skip enforcement for org members
70-
if: steps.org_check.outputs.is_member == 'true'
71-
run: echo "Author is org member; skipping template enforcement."
72-
73-
- name: Validate PR body against template
74-
if: steps.org_check.outputs.is_member == 'false'
75-
id: validate
76-
uses: actions/github-script@v8
77-
with:
78-
github-token: ${{ secrets.GITHUB_TOKEN }}
79-
script: |
80-
const prNumber = Number('${{ steps.load_pr.outputs.number }}');
81-
82-
// Fetch PR fresh here to safely get the body
83-
const { data: pr } = await github.rest.pulls.get({
84-
owner: context.repo.owner,
85-
repo: context.repo.repo,
86-
pull_number: prNumber,
87-
});
61+
if (isMember) {
62+
core.info(`Author ${author} is in org ${org}; skipping enforcement.`);
63+
return;
64+
}
8865
66+
// 3) Validate body
8967
const body = (pr.body || '').trim();
90-
9168
const problems = [];
9269
93-
// Basic length check – adjust as needed
70+
// Basic length check – tune as needed
9471
if (body.length < 150) {
9572
problems.push('PR description is too short (expected more content based on the template).');
9673
}
@@ -113,28 +90,13 @@ jobs:
11390
problems.push('The line "AI Usage has been disclosed." is missing.');
11491
}
11592
116-
if (problems.length > 0) {
117-
core.setOutput('valid', 'false');
118-
core.setOutput('problems', problems.join('\n'));
119-
} else {
120-
core.setOutput('valid', 'true');
93+
if (problems.length === 0) {
94+
core.info('PR body passed template validation.');
95+
return;
12196
}
12297
123-
- name: Handle invalid PR
124-
if: steps.org_check.outputs.is_member == 'false' && steps.validate.outputs.valid == 'false'
125-
uses: actions/github-script@v8
126-
with:
127-
github-token: ${{ secrets.GITHUB_TOKEN }}
128-
script: |
129-
const prNumber = Number('${{ steps.load_pr.outputs.number }}');
130-
const problems = '${{ steps.validate.outputs.problems }}';
131-
const { data: pr } = await github.rest.pulls.get({
132-
owner: context.repo.owner,
133-
repo: context.repo.repo,
134-
pull_number: prNumber,
135-
});
136-
137-
const INVALID_LABEL = 'invalid';
98+
// 4) If invalid: label, comment, close, and fail
99+
core.info('Template validation failed. Applying actions.');
138100
139101
const existingLabels = (pr.labels || []).map(l => l.name);
140102
if (!existingLabels.includes(INVALID_LABEL)) {
@@ -146,6 +108,8 @@ jobs:
146108
});
147109
}
148110
111+
const problemsBlock = problems.join('\n');
112+
149113
await github.rest.issues.createComment({
150114
owner: context.repo.owner,
151115
repo: context.repo.repo,
@@ -158,7 +122,7 @@ jobs:
158122
'Problems detected:',
159123
'',
160124
'```',
161-
problems,
125+
problemsBlock,
162126
'```',
163127
].join('\n'),
164128
});
@@ -173,7 +137,3 @@ jobs:
173137
}
174138
175139
core.setFailed('PR does not follow the required template.');
176-
177-
- name: Validation passed
178-
if: steps.org_check.outputs.is_member == 'false' && steps.validate.outputs.valid == 'true'
179-
run: echo "PR template validation passed for non-org member."

0 commit comments

Comments
 (0)