From 89fd1ffa71fb6c491a3c82e15b76b73bf4955f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Wed, 18 Dec 2024 10:50:36 +0100 Subject: [PATCH] feat: compare against a previous report Only the downloading logic was added, the comparison logic was added in the previous commit. --- action.yml | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/action.yml b/action.yml index 8aec574..86efef0 100644 --- a/action.yml +++ b/action.yml @@ -18,6 +18,8 @@ description: | comment-on-pr: 'true' generate-artifact: 'false' artifact-name: 'size-report.md' + base-branch: ${{ github.base_ref }} # set to e.g. main if not triggered by a pull_request + job-name: '' # set to the name of this job if you want to enable comparisons ``` inputs: comment-on-pr: @@ -29,6 +31,21 @@ inputs: artifact-name: description: The name of the generated artifact. default: report.json + base-branch: + description: | + The name of the base branch, defaults to github.base_ref which is only + available in pull_request triggered workflows, for other workflows + specify it manually. + + It will try to download the generated artifact from this branch so make + sure `generate-artifacte: true` when the workflow is running on that + branch. + default: ${{ github.base_ref }} + job-name: + description: | + The name of the job running this action. If not set, no comparisons can + be made. + default: '' outputs: runs: using: 'composite' @@ -44,5 +61,33 @@ runs: name: ${{ inputs.artifact-name }} - name: Comment Report if: inputs.comment-on-pr == 'true' + env: + ARTIFACT_NAME: ${{ inputs.artifact-name }} + HEAD_BRANCH: ${{ inputs.base-branch }} + JOB_NAME: ${{ inputs.job-name }} run: | + # USAGE: base_report_url + base_report_url() { + curl -X 'GET' \ + "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/tasks" \ + -H "Authorization: token $GITHUB_TOKEN" \ + -H 'accept: application/json' | + jq --raw-output '[.workflow_runs[] | select(.name == '"$JOB_NAME"' and .head_branch == '"$HEAD_BRANCH"')] | first | .url' + } + # USAGE: has_report + has_report() { + http_code=$(curl -X 'GET' -o /dev/null --silent -Iw '%{http_code}' "$1/artifacts/$ARTIFACT_NAME") + test "$http_code" = '200' + } + + # If we have a previous report compare against it + if [ "$JOB_NAME" ] && [ "$HEAD_BRANCH" ]; then + url=$(base_report_url) + if has_report "$url"; then + curl -X 'GET' -o old-report.json "$url/artifacts/$ARTIFACT_NAME" + "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json old-report.json + exit 0 + fi + fi + "$GITHUB_ACTION_PATH/comment_on_pr.sh" report.json -- 2.47.0