Skip to content

Commit 13c6181

Browse files
GeorgeSapkinAnsuel
authored andcommitted
formal: warn when maintainers can't edit
Warn when PR edits by maintainers are not allowed. Signed-off-by: George Sapkin <george@sapk.in>
1 parent 235bf3f commit 13c6181

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

.github/scripts/process_formalities.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ const MINIMIZE_COMMENT_MUTATION = `
2828
}
2929
`;
3030

31-
const COMMENT_LOOKUP = "Some formality checks failed.";
31+
const COMMENT_LOOKUP = "<!-- FORMALITY_LOOKUP -->";
3232

3333
const SUMMARY_HEADER=`
3434
> [!WARNING]
3535
>
36-
> ${COMMENT_LOOKUP}
36+
> Some formality checks failed.
3737
>
3838
> Consider (re)reading [submissions guidelines](https://openwrt.org/submitting-patches#submission_guidelines).
3939
@@ -47,6 +47,14 @@ const SUMMARY_FOOTER=`
4747
</details>
4848
`;
4949

50+
const NO_MODIFY=`
51+
> [!TIP]
52+
>
53+
> PR has _Allow edits and access to secrets by maintainers_ disabled. Consider allowing edits to simplify review.
54+
>
55+
> [More info](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)
56+
`;
57+
5058
async function hideOldSummaries({ github, owner, repo, issueNumber }) {
5159
const result = await github.graphql(GET_COMMENTS_QUERY, { owner, repo, issueNumber });
5260

@@ -68,27 +76,42 @@ function getJobUrl({ context, jobId }) {
6876
function getSummaryMessage({ context, jobId, summary }) {
6977
return `
7078
${SUMMARY_HEADER}
71-
7279
${summary}
73-
7480
${SUMMARY_FOOTER}
7581
For more details, see the [full job log](${getJobUrl({ context, jobId })}).
7682
`;
7783
}
7884

79-
async function processFormalities({ github, context, jobId, summary }) {
80-
const { owner, repo } = context.repo;
81-
const issueNumber = context.issue.number;
85+
function getCommentMessage({ context, jobId, noModify, summary }) {
86+
return `
87+
${summary.length > 0 ? getSummaryMessage({ context, jobId, summary }) : ''}
88+
${noModify ? NO_MODIFY : ''}
89+
${COMMENT_LOOKUP}
90+
`;
91+
}
92+
93+
async function processFormalities({
94+
context,
95+
github,
96+
jobId,
97+
summary,
98+
warnOnNoModify,
99+
}) {
100+
const { owner, repo, number: issueNumber } = context.issue;
82101

83102
await hideOldSummaries({ github, owner, repo, issueNumber });
84103

104+
// Explicitly check maintainer_can_modify as it might not be set at all
105+
const { pull_request: pr } = context.payload.pull_request;
106+
const noModify = warnOnNoModify && pr?.maintainer_can_modify === false;
85107
summary = summary.trim();
86-
if (summary.length === 0) {
108+
if (summary.length === 0 && !noModify) {
109+
console.log('Summary is empty and modify checks passed, skipping posting a comment');
87110
return;
88111
}
89112

90113
console.log("Posting new summary comment");
91-
const body = getSummaryMessage({ context, jobId, summary });
114+
const body = getCommentMessage({ context, jobId, noModify, summary });
92115
return github.rest.issues.createComment({
93116
issue_number: issueNumber,
94117
owner,

.github/workflows/formal.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ on:
1111
description: 'Post summaries to the pull request'
1212
required: false
1313
type: boolean
14+
warn_on_no_modify:
15+
description: 'Warn when PR edits by maintainers are not allowed. Requires post_comment to be true.'
16+
required: false
17+
type: boolean
1418

1519
permissions:
1620
pull-requests: write
@@ -51,12 +55,14 @@ jobs:
5155
env:
5256
JOB_ID: ${{ job.check_run_id }}
5357
SUMMARY: ${{ steps.formalities.outputs.content }}
58+
WARN_ON_NO_MODIFY: ${{ inputs.warn_on_no_modify }}
5459
with:
5560
script: |
5661
const processFormalities = require('./workflow_context/.github/scripts/process_formalities.js')
5762
await processFormalities({
5863
github,
5964
context,
60-
jobId: process.env.JOB_ID,
65+
jobId: process.env.JOB_ID,
6166
summary: process.env.SUMMARY,
67+
warnOnNoModify: process.env.WARN_ON_NO_MODIFY,
6268
});

0 commit comments

Comments
 (0)