Add package comment on a pull request #107
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Add package comment on a pull request | |
# Based on work from https://github.com/orgs/community/discussions/51403#discussioncomment-5535167 | |
on: | |
workflow_run: | |
types: | |
- completed | |
workflows: | |
- 'Creating plugin package in the PR' | |
jobs: | |
comment: | |
if: github.event.workflow_run.conclusion == 'success' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Download workflow artifact | |
uses: dawidd6/[email protected] | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
workflow: peek_icons.yml | |
run_id: ${{ github.event.workflow_run.id }} | |
- name: Read the PR number file | |
id: pr_number_reader | |
uses: juliangruber/[email protected] | |
with: | |
path: ./pr_number/pr_number.txt | |
- name: Read the zip file name | |
id: zip_file_name_reader | |
uses: juliangruber/[email protected] | |
with: | |
path: ./zip_file_name/zip_file_name.txt | |
- name: Get artifact and pull request info | |
env: | |
GITHUB_TOKEN: ${{ github.token }} | |
WORKFLOW_RUN_EVENT_OBJ: ${{ toJSON(github.event.workflow_run) }} | |
OWNER: ${{ github.repository_owner }} | |
REPO: ${{ github.event.repository.name }} | |
ZIP_FILENAME: ${{ steps.zip_file_name_reader.outputs.content }} | |
run: | | |
PREVIOUS_JOB_ID=$(jq -r '.id' <<< "$WORKFLOW_RUN_EVENT_OBJ") | |
echo "Previous Job ID: $PREVIOUS_JOB_ID" | |
echo "PREVIOUS_JOB_ID=$PREVIOUS_JOB_ID" >> "$GITHUB_ENV" | |
SUITE_ID=$(jq -r '.check_suite_id' <<< "$WORKFLOW_RUN_EVENT_OBJ") | |
echo "Previous Suite ID: $SUITE_ID" | |
echo "SUITE_ID=$SUITE_ID" >> "$GITHUB_ENV" | |
echo "ARTIFACT_NAME=$ZIP_FILENAME" >> "$GITHUB_ENV" | |
ARTIFACT_IDS=$(gh api "/repos/$OWNER/$REPO/actions/artifacts" \ | |
--jq [".artifacts.[] | | |
select(.workflow_run.id==$PREVIOUS_JOB_ID) | | |
select(.expired==false) | {id, name}"] | |
) | |
echo "ARTIFACT_IDS=$ARTIFACT_IDS" >> "$GITHUB_ENV" | |
echo "Head SHA: ${{ github.event.head_sha }}" | |
- name: Get upload zip artifact id | |
run: | | |
echo "ARTIFACT_ID=$(python -c "import os, json; | |
[print(str(art['id'])) for art in json.loads( | |
os.getenv('ARTIFACT_IDS').replace('\'', '\"')) | |
if art['name'] == os.getenv('ARTIFACT_NAME') ]")" >> "$GITHUB_ENV" | |
echo "The ARTIFACT ID: $ARTIFACT_ID" | |
- name: Find Comment | |
uses: peter-evans/find-comment@v2 | |
id: find-comment | |
with: | |
issue-number: ${{ steps.pr_number_reader.outputs.content }} | |
comment-author: 'github-actions[bot]' | |
- name: Update Comment | |
env: | |
JOB_PATH: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ env.PREVIOUS_JOB_ID }}" | |
ARTIFACT_URL: "${{ github.server_url }}/${{ github.repository }}/suites/${{ env.SUITE_ID }}/artifacts/${{ env.ARTIFACT_ID }}" | |
HEAD_SHA: "${{ github.event.head_sha }}" | |
uses: peter-evans/create-or-update-comment@v3 | |
with: | |
token: ${{ github.token }} | |
issue-number: ${{ steps.pr_number_reader.outputs.content }} | |
comment-id: ${{ steps.find-comment.outputs.comment-id }} | |
edit-mode: replace | |
body: |- | |
![badge] | |
Plugin zip package for the changes in this PR has been successfully built!. | |
Download the plugin zip file here ${{ env.ARTIFACT_URL }} | |
[badge]: https://img.shields.io/badge/package_build-success-green |