@@ -2,7 +2,7 @@ name: Check for NEXT_CHANGELOG.md Changes
22
33on :
44 pull_request_target :
5- branches : [ main ]
5+ branches : [ main ]
66
77permissions :
88 contents : read
@@ -23,93 +23,91 @@ jobs:
2323
2424 - name : Fetch list of changed files
2525 id : changed-files
26+ env :
27+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
2628 run : |
27- files=$(git diff --name-only HEAD^ HEAD || git diff --name-only origin/main HEAD)
28- echo "MODIFIED_FILES<<EOF" >> $GITHUB_ENV
29- echo "$files" >> $GITHUB_ENV
30- echo "EOF" >> $GITHUB_ENV
29+ # Use the GitHub API to fetch changed files
30+ files=$(gh pr view ${{ github.event.pull_request.number }} --json files -q '.files[].path')
31+
32+ # Sanitize to avoid code injection
33+ sanitized_files=$(echo "$files" | sed 's/[^a-zA-Z0-9._/-]/_/g')
34+
35+ # Store the sanitized list of files in a temporary file to avoid env variable issues
36+ echo "$sanitized_files" > modified_files.txt
37+ echo "$sanitized_files"
38+
39+ - name : Fetch PR message
40+ id : pr-message
41+ env :
42+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
43+ run : |
44+ # Use the GitHub API to fetch the PR message
45+ pr_message=$(gh pr view ${{ github.event.pull_request.number }} --json body -q '.body')
46+
47+ # Sanitize the PR message to avoid code injection, keeping the equal sign
48+ sanitized_pr_message=$(echo "$pr_message" | sed 's/[^a-zA-Z0-9._/-=]/_/g')
49+
50+ # Store the sanitized PR message
51+ echo "$sanitized_pr_message" > pr_message.txt
52+ echo "$sanitized_pr_message"
3153
3254 - name : Verify NEXT_CHANGELOG.md was modified or PR message contains NO_CHANGELOG=true
33- id : verify-changelog
3455 run : |
35- PR_BODY="${{ github.event.pull_request.body }}"
36- echo "PR body: $PR_BODY"
56+ # Read the sanitized files and PR message from the temporary files
57+ modified_files=$(cat modified_files.txt)
58+ pr_message=$(cat pr_message.txt)
3759
38- if ! echo "$MODIFIED_FILES" | grep -q "NEXT_CHANGELOG.md"; then
39- if echo "$PR_BODY" | grep -q "NO_CHANGELOG=true"; then
40- echo "NO_CHANGELOG=true found in PR body."
41- echo "CHANGELOG_NEEDED=false" >> $GITHUB_ENV
60+ # Check if NEXT_CHANGELOG.md exists in the list of changed files
61+ echo "Changed files: $modified_files"
62+ if ! echo "$modified_files" | grep -q "NEXT_CHANGELOG.md"; then
63+ echo "NEXT_CHANGELOG.md not modified."
64+
65+ # Check if PR message contains NO_CHANGELOG=true
66+ if echo "$pr_message" | grep -q "NO_CHANGELOG=true"; then
67+ echo "NO_CHANGELOG=true found in PR message. Skipping changelog check."
4268 exit 0
4369 else
44- echo "ERROR: NEXT_CHANGELOG.md not modified and NO_CHANGELOG=true not present."
45- echo "CHANGELOG_NEEDED=true" >> $GITHUB_ENV
70+ echo "WARNING: file NEXT_CHANGELOG.md not changed. If this is expected, add NO_CHANGELOG=true to the PR message."
4671 exit 1
4772 fi
48- else
49- echo "✅ NEXT_CHANGELOG.md was updated."
50- echo "CHANGELOG_NEEDED=false" >> $GITHUB_ENV
5173 fi
5274
5375 - name : Comment on PR with instructions if needed
54- if : failure() && env.CHANGELOG_NEEDED == 'true'
55- uses : actions/github-script@v7
56- with :
57- github-token : ${{ secrets.GITHUB_TOKEN }}
58- script : |
59- const { owner, repo } = context.repo;
60- const issue_number = context.issue.number;
61-
62- // Check if we've already commented
63- const comments = await github.rest.issues.listComments({
64- owner,
65- repo,
66- issue_number
67- });
68-
69- const existingComment = comments.data.find(comment =>
70- comment.body.includes('<!-- NEXT_CHANGELOG_INSTRUCTIONS -->')
71- );
72-
73- if (!existingComment) {
74- await github.rest.issues.createComment({
75- owner,
76- repo,
77- issue_number,
78- body: `<!-- NEXT_CHANGELOG_INSTRUCTIONS -->
79- Please ensure that the \`NEXT_CHANGELOG.md\` file is updated with any relevant changes.
80- If this is not necessary for your PR, include this in the PR body:
81-
82- \`\`\`
83- NO_CHANGELOG=true
84- \`\`\`
85-
86- and rerun the workflow.`
87- });
88- }
76+ if : failure() # This step will only run if the previous step fails (i.e., if NEXT_CHANGELOG.md was not modified and NO_CHANGELOG=true was not in the PR message)
77+ env :
78+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
79+ run : |
80+ # Check if a comment exists with the instructions
81+ previous_comment_ids=$(gh api "repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
82+ --jq '.[] | select(.body | startswith("<!-- NEXT_CHANGELOG_INSTRUCTIONS -->")) | .id')
83+ echo "Previous comment IDs: $previous_comment_ids"
84+
85+ # If no previous comment exists, add one with instructions
86+ if [ -z "$previous_comment_ids" ]; then
87+ echo "Adding instructions comment."
88+ gh pr comment ${{ github.event.pull_request.number }} --body \
89+ "<!-- NEXT_CHANGELOG_INSTRUCTIONS -->
90+ Please ensure that the NEXT_CHANGELOG.md file is updated with any relevant changes.
91+ If this is not necessary for your PR, please include the following in your PR description:
92+ NO_CHANGELOG=true
93+ and rerun the job."
94+ fi
8995
9096 - name : Delete instructions comment on success
91- if : success() && env.CHANGELOG_NEEDED == 'false'
92- uses : actions/github-script@v7
93- with :
94- github-token : ${{ secrets.GITHUB_TOKEN }}
95- script : |
96- const { owner, repo } = context.repo;
97- const issue_number = context.issue.number;
98-
99- const comments = await github.rest.issues.listComments({
100- owner,
101- repo,
102- issue_number
103- });
104-
105- const existingComments = comments.data.filter(comment =>
106- comment.body.includes('<!-- NEXT_CHANGELOG_INSTRUCTIONS -->')
107- );
108-
109- for (const comment of existingComments) {
110- await github.rest.issues.deleteComment({
111- owner,
112- repo,
113- comment_id: comment.id
114- });
115- }
97+ if : success() # This step will only run if the previous check passed (i.e., if NEXT_CHANGELOG.md was modified or NO_CHANGELOG=true is in the PR message)
98+ env :
99+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
100+ run : |
101+ # Check if there is a previous instructions comment
102+ previous_comment_ids=$(gh api "repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
103+ --jq '.[] | select(.body | startswith("<!-- NEXT_CHANGELOG_INSTRUCTIONS -->")) | .id')
104+
105+ # If a comment exists, delete it
106+ if [ -n "$previous_comment_ids" ]; then
107+ echo "Deleting previous instructions comment."
108+ for comment_id in $previous_comment_ids; do
109+ gh api "repos/${{ github.repository }}/issues/comments/$comment_id" --method DELETE
110+ done
111+ else
112+ echo "No instructions comment found to delete."
113+ fi
0 commit comments