From 4c1a56ac564aca3990e513f26127c5524fd03a1c Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:08:26 +0300 Subject: [PATCH] Re-code the commit range action --- .github/actions/commit-range/LICENSE.md | 29 -------- .github/actions/commit-range/action.yml | 95 ++++++++++++++++--------- 2 files changed, 62 insertions(+), 62 deletions(-) delete mode 100644 .github/actions/commit-range/LICENSE.md diff --git a/.github/actions/commit-range/LICENSE.md b/.github/actions/commit-range/LICENSE.md deleted file mode 100644 index 58584a014..000000000 --- a/.github/actions/commit-range/LICENSE.md +++ /dev/null @@ -1,29 +0,0 @@ -BSD 3-Clause License - -Copyright (c) 2022, Eindhoven University of Technology - CST Robotics Group -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/.github/actions/commit-range/action.yml b/.github/actions/commit-range/action.yml index 9c43415fb..16a86a08a 100644 --- a/.github/actions/commit-range/action.yml +++ b/.github/actions/commit-range/action.yml @@ -1,43 +1,72 @@ ---- -# Copyright (c) 2022, Eindhoven University of Technology - CST Robotics Group -# BDS 3-Clause License (see ./LICENSE.md) - -name: 'Commit range' -description: 'Determine commit range' +name: 'Get Commit Range' +description: 'Outputs the commit range for a push or pull request' outputs: - commit-range: - description: 'Range of commits in this run' - value: ${{ steps.commit-range.outputs.commit-range }} + commit_range: + description: 'The commit range' + value: ${{ steps.get-range.outputs.range }} runs: using: "composite" steps: - - name: Commit range - id: commit-range + - name: Get commit range + id: get-range + shell: bash env: - # Doing this inside run doesn't work because of piping: - commit_list: ${{ toJson(github.event.commits) }} + GH_TOKEN: ${{ github.token }} run: | - if [ "${GITHUB_EVENT_NAME}" == "push" ] - then - NUMBER_COMMITS=$(echo "${commit_list}" | jq 'length') - if [ ${NUMBER_COMMITS} -le 1 ] - then - COMMIT_RANGE="" + echo "==== Get Commit Range Action ====" + echo "Event type: ${{ github.event_name }}" + + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + echo "Processing a pull request event" + echo "Pull Request number: ${{ github.event.pull_request.number }}" + base_sha=${{ github.event.pull_request.base.sha }} + head_sha=${{ github.event.pull_request.head.sha }} + echo "Base SHA: ${base_sha}" + echo "Head SHA: ${head_sha}" + + echo "Fetching merge base from GitHub API..." + merge_base=$(gh api repos/${{ github.repository }}/compare/$base_sha...$head_sha --jq '.merge_base_commit.sha') + echo "Merge base: ${merge_base}" + + range="${merge_base}...${head_sha}" + echo "Calculated range: ${range}" + echo "range=${range}" >> $GITHUB_OUTPUT + + echo "Explanation: This range represents all commits in the pull request branch that are not in the base branch, starting from the common ancestor (merge base)." + + elif [[ "${{ github.event_name }}" == "push" ]]; then + echo "Processing a push event" + echo "Ref: ${{ github.ref }}" + echo "Before SHA: ${{ github.event.before }}" + echo "After SHA: ${{ github.sha }}" + + if [[ "${{ github.event.before }}" == "0000000000000000000000000000000000000000" ]]; then + echo "This is a push to a new branch" + echo "Fetching first commit of the branch from GitHub API..." + first_commit=$(gh api repos/${{ github.repository }}/commits/${{ github.ref_name }} --jq '.parents[0].sha // .sha') + echo "First commit (or its parent): ${first_commit}" + range="${first_commit}...${{ github.sha }}" else - OLDEST=$(echo "${commit_list}" | jq 'first.id' | tr -d '"') - NEWEST=$(echo "${commit_list}" | jq 'last.id' | tr -d '"') - OLDEST_PARENTS=($(git show --no-patch --format="%P" ${OLDEST})) - NUMBER_OLDEST_PARENTS=${#OLDEST_PARENTS[@]} - # Take second parent if possible, - # see https://stackoverflow.com/a/2222920: - COMMIT_RANGE="${OLDEST}^${NUMBER_OLDEST_PARENTS}...${NEWEST}" + echo "This is a push to an existing branch" + range="${{ github.event.before }}...${{ github.sha }}" fi - elif [ "${GITHUB_EVENT_NAME}" == "pull_request" ] - then - OLDEST=${{ github.event.pull_request.base.sha }} - NEWEST=${{ github.event.pull_request.head.sha }} - COMMIT_RANGE="${OLDEST}...${NEWEST}" + + echo "Calculated range: ${range}" + echo "range=${range}" >> $GITHUB_OUTPUT + + echo "Explanation: This range represents all new commits pushed to the branch." + + else + echo "Error: Unsupported event type: ${{ github.event_name }}" + exit 1 fi - echo -e "\e[35m\e[1m COMMIT_RANGE = ${COMMIT_RANGE} \e[0m" - echo "commit-range=${COMMIT_RANGE}" >> $GITHUB_OUTPUT + + echo "==== Action Complete ====" + + - name: Debug output shell: bash + run: | + echo "==== Commit Range Result ====" + echo "The calculated commit range is: ${{ steps.get-range.outputs.range }}" + echo "This range can be used for further processing or analysis of the changes." + echo "================================"