Skip to content

Commit

Permalink
feat: Make token and repository params optional (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrekkr authored Jul 10, 2024
1 parent 08c36ee commit 6e2eaa1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,45 @@ jobs:
exit 1
fi
single-job-test-default-repo-token:
name: Test single job with default repo and token
runs-on: ubuntu-latest
steps:
- name: Checkout action
uses: actions/checkout@v4
with:
path: .github/actions/gha-jobid-action
- name: Run action with options
id: test1
uses: ./.github/actions/gha-jobid-action
with:
job_name: "build-matrix (1)"
run_id: "6225949607"
- name: Verify
run: |
check_url() {
if [[ "$1" == "$2" ]]; then
echo "html_url OK"
else
echo "html_url ERROR" >&2
ERROR_FLAG=1
fi
}
check_job_id() {
if [[ "$1" == "$2" ]]; then
echo "job_id OK"
else
echo "job_id ERROR" >&2
ERROR_FLAG=1
fi
}
ERROR_FLAG=0
check_url "${{ steps.test1.outputs.html_url }}" "https://github.com/Tiryoh/gha-jobid-action/actions/runs/6225949607/job/16897423007"
check_job_id "${{ steps.test1.outputs.job_id }}" "16897423007"
if [[ "$ERROR_FLAG" == 1 ]]; then
exit 1
fi
multi-jobs-test:
name: Test multi jobs
runs-on: ubuntu-latest
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ https://docs.github.com/ja/rest/actions/workflow-jobs?apiVersion=2022-11-28

### `github_token`

**Required** GITHUB_TOKEN to use GitHub API
**Optional** auth token to use with GitHub API

Simply, just specify `${{ secrets.GITHUB_TOKEN }}`.
By default `github.token` context value (same as `secrets.GITHUB_TOKEN`) is used. You can use your own `PAT` if prefered.
Token requires `actions: read` scope.

### `job_name`

Expand All @@ -35,9 +36,9 @@ jobs:

### `repository`

target GitHub repository
**Optional**: target GitHub repository

default: `${GITHUB_REPOSITORY}`
default: `github.repository` context value

### `run_id`

Expand Down Expand Up @@ -92,7 +93,6 @@ jobs:
uses: Tiryoh/gha-jobid-action@v1
id: jobs
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
job_name: "build" # input job.<job-id>
#job_name: "${{ github.job }}" # if job.<job-id>.name is not specified, this works too
- name: Output Current Job Log URL
Expand Down Expand Up @@ -121,7 +121,6 @@ jobs:
uses: Tiryoh/gha-jobid-action@v1
id: jobs
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
job_name: "Build and Test" # input job.<job-id>.name here. job.<job-id> won't works.
- name: Output Current Job Log URL
run: echo ${{ steps.jobs.outputs.html_url }}
Expand Down Expand Up @@ -151,7 +150,6 @@ jobs:
uses: Tiryoh/gha-jobid-action@v1
id: jobs
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
job_name: "build (${{ matrix.distro }})" # input job.<job-id>.name and matrix here.
per_page: 50 # input matrix size here if it is larger than 30
- name: Output Current Job Log URL
Expand Down
11 changes: 7 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ description: "GitHub Action to get the current workflow run's job_id"
inputs:
github_token:
description: "GITHUB_TOKEN to use GitHub API v3"
required: true
required: false
default: ${{ github.token }}
repository:
description: "target GitHub repository"
required: false
default: ${{ github.repository }}
run_id:
description: "run_id of target workflow run"
required: false
default: ${{ github.run_id }}
job_name:
description: "job_name of tartget workflow jobs"
required: true
per_page:
description: "Results per page (max 100) of target workflow run"
required: false
default: 30
outputs:
job_id:
description: "job_id of target workflow jobs"
Expand All @@ -39,11 +43,10 @@ runs:
INPUT_RUN_ID: ${{ inputs.run_id }}
INPUT_JOB_NAME: ${{ inputs.job_name }}
INPUT_PER_PAGE: ${{ inputs.per_page }}
GITHUB_BASEURL: ${{ github.api_url }}
shell: bash
run: |
GITHUB_API="/repos/${INPUT_REPOSITORY:-${GITHUB_REPOSITORY}}/actions/runs/${INPUT_RUN_ID:-${GITHUB_RUN_ID}}/jobs"
JOBINFO="$(curl --get -Ss -H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "${GITHUB_BASEURL}${GITHUB_API}?per_page=${INPUT_PER_PAGE:-30}")"
GITHUB_API="/repos/${INPUT_REPOSITORY}/actions/runs/${INPUT_RUN_ID}/jobs"
JOBINFO="$(curl --get -Ss -H "Authorization: Bearer ${INPUT_GITHUB_TOKEN}" -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "${GITHUB_API_URL}${GITHUB_API}?per_page=${INPUT_PER_PAGE}")"
echo "${JOBINFO}" | grep "Resource not accessible by integration" && exit 1
TOTAL_COUNT="$(echo "${JOBINFO}" | jq -r .total_count)"
eval "$(echo "${JOBINFO}" | jq -r --arg job_name "${INPUT_JOB_NAME}" '.jobs | map(select(.name == $job_name)) | .[0] | @sh "JOB_ID=\(.id) HTML_URL=\(.html_url)"')"
Expand Down

0 comments on commit 6e2eaa1

Please sign in to comment.