Skip to content

GH Actions: fix PR comment conundrum #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions .github/workflows/pr-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Pull Request Comment

on:
workflow_run:
workflows: ['Publish wiki']
types:
- completed

# Cancels all previous workflow runs for pull requests that have not completed.
concurrency:
# The concurrency group contains the workflow name and the branch name for pull requests
# or the commit hash for any other events.
group: ${{ github.workflow }}-${{ github.sha }}

jobs:
artifact-review-comment:
if: >
github.repository == 'PHPCSStandards/PHP_CodeSniffer-documentation' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'

runs-on: ubuntu-latest

permissions:
# Needed for the PR comment.
pull-requests: write

name: Comment on a pull request

steps:
- name: Download PR info artifact
uses: actions/github-script@v7
with:
script: |
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});

const matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr_info"
})[0];

if ( ! matchArtifact ) {
core.setFailed( 'No artifact found!' );
return;
}

const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});

const fs = require( 'fs' );
fs.writeFileSync( '${{ github.workspace }}/pr_info.zip', Buffer.from( download.data ) )

- name: Unzip PR info artifact
run: unzip pr_info.zip

- name: Read PR number
id: pr_number
shell: bash
run: |
value=$(<"pr_number")
echo "PR_NR=$value" >> "$GITHUB_OUTPUT"

- name: Read Wiki artifact URL
id: artifact_url
shell: bash
run: |
value=$(<"artifact_url")
echo "URL=$value" >> "$GITHUB_OUTPUT"

- name: "Post comment to review artifact"
uses: mshick/add-pr-comment@v2
with:
issue: ${{ steps.pr_number.outputs.PR_NR }}
repo-token: ${{ secrets.GITHUB_TOKEN }}
message: |
**_=== This is an auto-generated comment ===_**

Thank you for your PR.
A dry-run has been executed on your PR, executing all markdown pre-processing for the wiki files.

Please review the resulting final markdown files via the [created artifact](${{ steps.artifact_url.outputs.URL }}).
This is especially important when adding new pages or updating auto-generated output blocks.

_N.B.: the above link will automatically be updated when this PR is updated._
49 changes: 31 additions & 18 deletions .github/workflows/publish-wiki.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ on:
- 'main'
paths:
- .github/workflows/publish-wiki.yml
- .github/workflows/pr-comment.yml
- build/wiki-code-samples/**
- build/wiki-command-replacer.sh
- wiki/**
# Do a dry-run (check, no deploy) for PRs.
pull_request:
paths:
- .github/workflows/publish-wiki.yml
- .github/workflows/pr-comment.yml
- build/wiki-code-samples/**
- build/wiki-command-replacer.sh
- wiki/**
Expand All @@ -38,8 +40,6 @@ jobs:
permissions:
# Needed for the commit to the wiki.
contents: write
# Needed for the PR comment.
pull-requests: write

steps:
- name: Checkout code
Expand Down Expand Up @@ -126,22 +126,6 @@ jobs:
if-no-files-found: error
retention-days: 10

- name: "[PR only] Post comment to review artifact"
if: ${{ github.event_name == 'pull_request' }}
uses: mshick/add-pr-comment@v2
with:
repo-token: ${{ secrets.COMMENT_ON_PRS_TOKEN }}
message: |
**_=== This is an auto-generated comment ===_**

Thank you for your PR.
A dry-run has been executed on your PR, executing all markdown pre-processing for the wiki files.

Please review the resulting final markdown files via the [created artifact](${{ steps.artifact.outputs.artifact-url }}).
This is especially important when adding new pages or updating auto-generated output blocks.

_N.B.: the above link will automatically be updated when this PR is updated._


# ################################################################################
# Deploy to the wiki in the PHPCS repo.
Expand All @@ -153,6 +137,7 @@ jobs:
git_threshold: partial_outage

- name: Deploy to wiki
if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository }}
uses: Andrew-Chen-Wang/[email protected]
env:
COMMIT_MSG: ${{ github.event.head_commit.message }}
Expand All @@ -166,3 +151,31 @@ jobs:
dry-run: ${{ github.event_name == 'pull_request' }}
disable-empty-commits: true
preprocess: false


# ################################################################################
# Dry-run/PRs: save PR info for use in Pull Request Comment workflow.
# ################################################################################

- name: Create temporary directory
if: ${{ github.event_name == 'pull_request' }}
run: mkdir -p ./pr

- name: Save PR number
if: ${{ github.event_name == 'pull_request' }}
env:
PR_NUMBER: ${{ github.event.number }}
run: echo "${PR_NUMBER}" > ./pr/pr_number

- name: Save artifact URL
if: ${{ github.event_name == 'pull_request' }}
env:
ARTIFACT_URL: ${{ steps.artifact.outputs.artifact-url }}
run: echo "${ARTIFACT_URL}" > ./pr/artifact_url

- name: Upload PR info as artifact
if: ${{ github.event_name == 'pull_request' }}
uses: actions/upload-artifact@v4
with:
name: pr_info
path: pr/
Loading