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 Jul 30, 2024
1 parent 2eb092e commit 4c1a56a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
29 changes: 0 additions & 29 deletions .github/actions/commit-range/LICENSE.md

This file was deleted.

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

0 comments on commit 4c1a56a

Please sign in to comment.