Skip to content

Commit

Permalink
Re-code the commit range action
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Feb 3, 2025
1 parent 7aa5f77 commit 0444a17
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 62 deletions.
29 changes: 0 additions & 29 deletions .github/actions/commit-range/LICENSE.md

This file was deleted.

110 changes: 77 additions & 33 deletions .github/actions/commit-range/action.yml
Original file line number Diff line number Diff line change
@@ -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 ===="

0 comments on commit 0444a17

Please sign in to comment.