From 143d9d4b8e7a6393c8c458f958b1fdce8b172ac8 Mon Sep 17 00:00:00 2001 From: Ludovic Ortega Date: Tue, 17 Sep 2024 22:44:33 +0200 Subject: [PATCH] feat(action): allow configuring the github token and set it when it exists (#34) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: set Bearer token in curl request when GITHUB_TOKEN is set Signed-off-by: Ludovic Ortega * fix: use a dedicated input variable to pass github token Signed-off-by: Ludovic Ortega * fix: use github_token inputs Signed-off-by: Ludovic Ortega * Update README.md * Update README.md --------- Signed-off-by: Ludovic Ortega Co-authored-by: Orhun Parmaksız --- README.md | 8 ++++++++ action.yml | 6 +++++- install.sh | 21 ++++++++++++++------- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bf3aa47..48ef412 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ This action generates a changelog based on your Git history using [git-cliff](ht - `version`: `git-cliff` version to use. (e.g. `"latest"`, `"v2.5.0"`) - `config`: Path of the configuration file. (Default: `"cliff.toml"`) - `args`: [Arguments](https://github.com/orhun/git-cliff#usage) to pass to git-cliff. (Default: `"-v"`) +- `github_token`: The GitHub API token used to get `git-cliff` release information via the GitHub API to avoid rate limits. (Default: `${{ github.token }}`) ### Output variables @@ -32,6 +33,13 @@ This action generates a changelog based on your Git history using [git-cliff](ht > > Otherwise, you might end up getting empty changelogs or `git ref` errors depending on arguments passed to `git-cliff`. +### Running the action outside of GitHub + +If you run the action in Gitea or GitHub Enterprise, the `github_token` input is invalid. You have two options: + +- Pass an empty value (`github_token: ""`) (limit of 60 requests per hour per IP address). +- Create a GitHub token and pass it through GitHub secrets to avoid rate limiting. + ### Examples #### Simple diff --git a/action.yml b/action.yml index 1e5310b..978d7b8 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,10 @@ inputs: description: "git-cliff arguments" required: false default: "-v" + github_token: + description: "GitHub API token" + required: false + default: "${{ github.token }}" outputs: changelog: description: "output file" @@ -33,7 +37,7 @@ runs: RUNNER_OS: ${{ runner.os }} RUNNER_ARCH: ${{ runner.arch }} VERSION: ${{ inputs.version }} - GITHUB_TOKEN: ${{ github.token }} + GITHUB_API_TOKEN: ${{ inputs.github_token }} - name: Run git-cliff id: run-git-cliff diff --git a/install.sh b/install.sh index 7401f72..70b1076 100755 --- a/install.sh +++ b/install.sh @@ -19,14 +19,21 @@ if [[ "${VERSION}" != 'latest' ]]; then RELEASE_URL="https://api.github.com/repos/orhun/git-cliff/releases/tags/${VERSION}" fi -# Although releases endpoint is available without authentication, the current github.token is still passed -# in order to increase the limit of 60 requests per hour per IP address to a higher value that's also counted -# per GitHub account. # Caching is disabled in order not to receive stale responses from Varnish cache fronting GitHub API. -RELEASE_INFO="$(curl --silent --show-error --fail \ - --header "authorization: Bearer ${GITHUB_TOKEN}" \ - --header 'Cache-Control: no-cache, must-revalidate' \ - "${RELEASE_URL}")" +if [[ -z "${GITHUB_API_TOKEN}" ]]; then + RELEASE_INFO="$(curl --silent --show-error --fail \ + --header 'Cache-Control: no-cache, must-revalidate' \ + "${RELEASE_URL}")" +else + # Although releases endpoint is available without authentication, the current github.token is still passed + # in order to increase the limit of 60 requests per hour per IP address to a higher value that's also counted + # per GitHub account. + RELEASE_INFO="$(curl --silent --show-error --fail \ + --header "authorization: Bearer ${GITHUB_API_TOKEN}" \ + --header 'Cache-Control: no-cache, must-revalidate' \ + "${RELEASE_URL}")" +fi + TAG_NAME="$(echo "${RELEASE_INFO}" | jq --raw-output ".tag_name")" TARGET="git-cliff-${TAG_NAME:1}-${ARCH}-${OS}.tar.gz" LOCATION="$(echo "${RELEASE_INFO}" \