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..6105bbb0e 100644 --- a/.github/actions/commit-range/action.yml +++ b/.github/actions/commit-range/action.yml @@ -1,43 +1,87 @@ --- -# 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" + echo "branch that are not in the base branch, starting from the common" + echo "ancestor (merge base)." + + elif [[ "${{ github.event_name }}" == "push" ]]; then + echo "Processing a push event" + echo "Ref: ${{ github.ref }}" + echo "After SHA: ${{ github.sha }}" + + echo "Fetching the pull request information..." + pr_info=$(\ + gh pr list \ + --head ${{ github.ref_name }} \ + --json number,baseRefName \ + --limit 1) + if [[ \ + -n "$pr_info" && \ + "$(echo "$pr_info" | jq '.[0].baseRefName')" != "null" \ + ]]; then + base_branch=$(echo "$pr_info" | jq -r '.[0].baseRefName') + echo "Associated PR found. Base branch: ${base_branch}" 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 "No associated PR found or base branch is null." + echo "Using default branch as base." + base_branch=$(gh api repos/${{ github.repository }} --jq '.default_branch') + echo "Default branch: ${base_branch}" 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 "Fetching the branch point..." + if ! branch_point=$(git merge-base origin/${base_branch} ${{ github.sha }}) + then + echo "Failed to find merge-base." + echo "Using the first commit of the current branch." + branch_point=$(git rev-list --max-parents=0 HEAD) + fi + echo "Branch point: ${branch_point}" + + range="${branch_point}...${{ github.sha }}" + echo "Calculated range: ${range}" + echo "range=${range}" >> $GITHUB_OUTPUT + + echo "This range represents all commits in ${{ github.ref }} since it" + echo "diverged from ${base_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 - shell: bash + + echo "==== Get Commit Range action complete ===="