From 68f712f8765cb3e00a210d04ee5e1f9eadff2df8 Mon Sep 17 00:00:00 2001 From: Joseph H Kennedy Date: Tue, 2 Nov 2021 22:27:12 -0800 Subject: [PATCH] Post jobs to slack when PR is merged (#2) * Switch to on push; use parent hash for diff * Update README for on push instead of opened PR * Need to checkout depth 2 so parents are available --- .github/workflows/post-jobs-slack.yaml | 11 +++++++---- README.md | 18 +++++++++--------- action.yml | 6 +----- entrypoint.sh | 3 ++- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/.github/workflows/post-jobs-slack.yaml b/.github/workflows/post-jobs-slack.yaml index 04156ee..8f12f10 100644 --- a/.github/workflows/post-jobs-slack.yaml +++ b/.github/workflows/post-jobs-slack.yaml @@ -1,5 +1,5 @@ on: - pull_request: + push: paths: - 'example/jobs.yaml' branches: @@ -11,15 +11,18 @@ jobs: name: Run Jobs Slack Poster steps: - uses: actions/checkout@v2 + with: + fetch-depth: 2 + - id: updater name: Job Updater uses: ./ env: SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} - with: + with: filename: "example/jobs.yaml" key: "url" - + - run: echo ${{ steps.updater.outputs.fields }} name: Show New Jobs - shell: bash + shell: bash diff --git a/README.md b/README.md index fa11a5f..d7a5f4e 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ and a yaml file with a list of jobs (or other links): url: https://my-job.org/12345 ``` -The action will inspect the file to determine lines that are newly added (compared to the main branch) +The action will inspect the file to determine lines that are newly added (compared to the parent commit) for a field of interest (e.g., the "url" attribute in a list of jobs), extract this field, and then post to a Slack channel. ![img/example.png](img/example.png) @@ -44,16 +44,14 @@ and put it in a safe place. We will want to keep this URL as a secret in our eve ## 2. Usage -Add an GitHub workflow file in .github/workflows to specify the following. Note that -the workflow below will do the check and update on the opening of a pull request. +Add an GitHub workflow file in `.github/workflows` to specify the following. Note that +the workflow below will do the check and update on any push to main (e.g., a merged pull request). ```yaml on: - pull_request: + push: paths: - '_data/jobs.yml' - types: - - opened branches: - main @@ -63,17 +61,19 @@ jobs: name: Run Jobs Slack Poster steps: - uses: actions/checkout@v2 + with: + fetch-depth: 2 + - id: updater name: Job Updater uses: rseng/jobs-updater@main env: SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} - with: + with: filename: "_data/jobs.yml" key: "url" - + - run: echo ${{ steps.updater.outputs.fields }} name: Show New Jobs shell: bash ``` - diff --git a/action.yml b/action.yml index f5094ee..814eadb 100644 --- a/action.yml +++ b/action.yml @@ -1,10 +1,6 @@ name: 'Jobs Updater' description: "The jobs updater will respond on some trigger, and them parse a jobs file for changes, posting a field of interest to slack." inputs: - main: - description: main branch to compare to (defaults to main) - required: false - default: main filename: description: the filename for the jobs required: true @@ -28,8 +24,8 @@ runs: id: jobs-updater env: INPUT_FILENAME: ${{ inputs.filename }} - INPUT_MAIN: ${{ inputs.main }} INPUT_KEY: ${{ inputs.key }} + CURRENT_SHA: ${{ github.sha }} ACTION_DIR: ${{ github.action_path }} INPUT_REPO: ${{ github.repository }} run: ${{ github.action_path }}/entrypoint.sh diff --git a/entrypoint.sh b/entrypoint.sh index d2f38f8..e2cf795 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -12,7 +12,8 @@ if [[ ! -f "${INPUT_FILENAME}" ]]; then fi # Wget the comparison file -JOBFILE="https://raw.githubusercontent.com/${INPUT_REPO}/${INPUT_MAIN}/${INPUT_FILENAME}" +PARENT_SHA=$(git log --pretty=%P -n 1 ${CURRENT_SHA} | cut -d ' ' -f 1) +JOBFILE="https://raw.githubusercontent.com/${INPUT_REPO}/${PARENT_SHA}/${INPUT_FILENAME}" TMP=$(mktemp -d) BASENAME=$(basename ${INPUT_FILENAME})