Skip to content

Commit 3e90b97

Browse files
authored
Split the GitHub Action for license header checks into two workflows. One for checking the PR and the other for posting PR comments. (#6632)
The current approach is resulting in permissions failures when trying to post comments. This approach should give the necessary permissions. Signed-off-by: David Venable <dlv@amazon.com>
1 parent 3734d6a commit 3e90b97

3 files changed

Lines changed: 117 additions & 95 deletions

File tree

.github/scripts/check-license-headers.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,9 @@ def main():
126126

127127
violation_text = '\n'.join(violations)
128128

129-
# Set output for GitHub Actions
130-
github_output = os.environ.get('GITHUB_OUTPUT')
131-
if github_output:
132-
with open(github_output, 'a') as f:
133-
f.write(f"violations<<EOF\n{violation_text}\nEOF\n")
129+
# Write violations file for the comment workflow artifact
130+
with open('violations.txt', 'w') as f:
131+
f.write(violation_text + '\n')
134132

135133
print("\nViolations:")
136134
for violation in violations:
@@ -139,11 +137,6 @@ def main():
139137
sys.exit(1)
140138
else:
141139
print("\n✅ All files have proper license headers!")
142-
# Set empty output for GitHub Actions
143-
github_output = os.environ.get('GITHUB_OUTPUT')
144-
if github_output:
145-
with open(github_output, 'a') as f:
146-
f.write("violations=\n")
147140

148141
if __name__ == "__main__":
149142
main()

.github/workflows/license-header-check.yml

Lines changed: 15 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -8,114 +8,44 @@
88
#
99

1010
# Performs a license header check on new files.
11-
# It will comment on PRs if it finds violations.
11+
# Results are uploaded as an artifact for the comment workflow.
1212

1313
name: License Header Check
1414

1515
on:
1616
pull_request:
1717
types: [opened, synchronize, reopened]
1818

19-
permissions:
20-
issues: write
21-
pull-requests: write
22-
2319
jobs:
2420
license-header-check:
2521
runs-on: ubuntu-latest
2622
name: Check License Headers on New Files
27-
23+
2824
steps:
2925
- name: Checkout code
3026
uses: actions/checkout@v6
3127
with:
3228
fetch-depth: 0
33-
29+
3430
- name: Setup Python
3531
uses: actions/setup-python@v6
3632
with:
3733
python-version: '3.14'
38-
34+
3935
- name: Run License Header Check
4036
id: license-check
4137
run: |
4238
python .github/scripts/get-new-files.py | python .github/scripts/check-license-headers.py
43-
44-
- name: Comment on PR
45-
if: failure() && steps.license-check.outputs.violations != ''
46-
uses: actions/github-script@v8
47-
with:
48-
script: |
49-
const violations = process.env.VIOLATIONS;
50-
51-
const body = [
52-
'## ⚠️ License Header Violations Found',
53-
'',
54-
'The following newly added files are missing required license headers:',
55-
'',
56-
violations,
57-
'',
58-
'Please add the appropriate license header to each file and push your changes.',
59-
'',
60-
'**See the license header requirements:** https://github.com/opensearch-project/data-prepper/blob/main/CONTRIBUTING.md#license-headers'
61-
].join('\n');
6239
63-
const { data: comments } = await github.rest.issues.listComments({
64-
owner: context.repo.owner,
65-
repo: context.repo.repo,
66-
issue_number: context.issue.number,
67-
});
68-
69-
const botComment = comments.find(comment =>
70-
comment.user.type === 'Bot' &&
71-
comment.body.includes('License Header Violations Found')
72-
);
73-
74-
if (botComment) {
75-
await github.rest.issues.updateComment({
76-
owner: context.repo.owner,
77-
repo: context.repo.repo,
78-
comment_id: botComment.id,
79-
body: body
80-
});
81-
} else {
82-
await github.rest.issues.createComment({
83-
owner: context.repo.owner,
84-
repo: context.repo.repo,
85-
issue_number: context.issue.number,
86-
body: body
87-
});
88-
}
89-
env:
90-
VIOLATIONS: ${{ steps.license-check.outputs.violations }}
91-
92-
- name: Update PR comment (all violations resolved)
93-
if: success()
94-
uses: actions/github-script@v8
40+
- name: Save PR number
41+
if: always()
42+
run: echo "${{ github.event.pull_request.number }}" > pr_number.txt
43+
44+
- name: Upload results
45+
if: always()
46+
uses: actions/upload-artifact@v4
9547
with:
96-
script: |
97-
const { data: comments } = await github.rest.issues.listComments({
98-
owner: context.repo.owner,
99-
repo: context.repo.repo,
100-
issue_number: context.issue.number,
101-
});
102-
103-
const botComment = comments.find(comment =>
104-
comment.user.type === 'Bot' &&
105-
(comment.body.includes('License Header Violations Found') || comment.body.includes('License Header Check Passed'))
106-
);
107-
108-
if (botComment) {
109-
const successBody = [
110-
'## ✅ License Header Check Passed',
111-
'',
112-
'All newly added files have proper license headers. Great work! 🎉'
113-
].join('\n');
114-
115-
await github.rest.issues.updateComment({
116-
owner: context.repo.owner,
117-
repo: context.repo.repo,
118-
comment_id: botComment.id,
119-
body: successBody
120-
});
121-
}
48+
name: license-check-results
49+
path: |
50+
pr_number.txt
51+
violations.txt
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#
2+
# Copyright OpenSearch Contributors
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# The OpenSearch Contributors require contributions made to
6+
# this file be licensed under the Apache-2.0 license or a
7+
# compatible open source license.
8+
#
9+
10+
# Comments on PRs with license header check results.
11+
# Triggered by the License Header Check workflow.
12+
13+
name: License Header Comment
14+
15+
on:
16+
workflow_run:
17+
workflows: ['License Header Check']
18+
types: [completed]
19+
20+
permissions:
21+
issues: write
22+
pull-requests: write
23+
24+
jobs:
25+
comment:
26+
runs-on: ubuntu-latest
27+
if: github.event.workflow_run.event == 'pull_request'
28+
29+
steps:
30+
- name: Download results
31+
uses: actions/download-artifact@v4
32+
with:
33+
name: license-check-results
34+
run-id: ${{ github.event.workflow_run.id }}
35+
github-token: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Read PR number
38+
id: pr
39+
run: echo "number=$(cat pr_number.txt)" >> "$GITHUB_OUTPUT"
40+
41+
- name: Comment on PR
42+
uses: actions/github-script@v8
43+
with:
44+
script: |
45+
const fs = require('fs');
46+
const prNumber = parseInt('${{ steps.pr.outputs.number }}');
47+
const failed = '${{ github.event.workflow_run.conclusion }}' === 'failure';
48+
const hasViolations = fs.existsSync('violations.txt');
49+
50+
const { data: comments } = await github.rest.issues.listComments({
51+
owner: context.repo.owner,
52+
repo: context.repo.repo,
53+
issue_number: prNumber,
54+
});
55+
56+
const botComment = comments.find(comment =>
57+
comment.user.type === 'Bot' &&
58+
(comment.body.includes('License Header Violations Found') || comment.body.includes('License Header Check Passed'))
59+
);
60+
61+
let body;
62+
if (failed && hasViolations) {
63+
const violations = fs.readFileSync('violations.txt', 'utf8').trim();
64+
body = [
65+
'## ⚠️ License Header Violations Found',
66+
'',
67+
'The following newly added files are missing required license headers:',
68+
'',
69+
violations,
70+
'',
71+
'Please add the appropriate license header to each file and push your changes.',
72+
'',
73+
'**See the license header requirements:** https://github.com/opensearch-project/data-prepper/blob/main/CONTRIBUTING.md#license-headers'
74+
].join('\n');
75+
} else if (!failed && botComment) {
76+
body = [
77+
'## ✅ License Header Check Passed',
78+
'',
79+
'All newly added files have proper license headers. Great work! 🎉'
80+
].join('\n');
81+
} else {
82+
return;
83+
}
84+
85+
if (botComment) {
86+
await github.rest.issues.updateComment({
87+
owner: context.repo.owner,
88+
repo: context.repo.repo,
89+
comment_id: botComment.id,
90+
body: body
91+
});
92+
} else {
93+
await github.rest.issues.createComment({
94+
owner: context.repo.owner,
95+
repo: context.repo.repo,
96+
issue_number: prNumber,
97+
body: body
98+
});
99+
}

0 commit comments

Comments
 (0)