Skip to content

Commit

Permalink
feat(compare-branches): add compare branches action
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 30, 2022
1 parent e7f87fa commit 5ae87a7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This is a collection of reusable composite actions for GitHub Actions workflows.
- [setup-git-credentials](#setup-git-credentials)
- [rebase](#rebase)
- [update-tags](#update-tags)
- [compare-branches](#compare-branches)
- [Workflows](#workflows)
- [Semantic Release](#semantic-release-workflow)

Expand Down Expand Up @@ -400,6 +401,27 @@ If run without `minor: true`, or with `minor: false`:

- Will add `v2` to the current commit.

#### compare-branches

[Source](compare-branches/action.yml)

Check if there are new commits in head that are not in base.

##### Inputs

| Required | Name | Description | Example | Default |
|----------|----------|-------------------------------------|-------------------|-----------|
| No | `base` | The base branch to compare against | `main` | `main` |
| No | `head` | The branch to check for new commits | `feat/my-feature` | `develop` |
| No | `remote` | The remote to use | `upstream` | `origin` |

##### Outputs

| Name | Description | Example |
|-----------|----------------------------------------------|--------------------|
| `diff` | Whether the branches are different | `true` |
| `commits` | List of commits in head that are not in base | `• fix: fix a bug` |

## Workflows

### Semantic Release workflow
Expand Down
51 changes: 51 additions & 0 deletions compare-branches/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: 'Compare branches'
description: 'Check if there are new commits in head that are not in base'

inputs:
base:
description: 'The base branch to compare against'
required: false
default: 'main'

head:
description: 'The branch to check for new commits'
required: false
default: 'develop'

remote:
description: 'The remote to use'
required: false
default: 'origin'

outputs:
diff:
description: 'Whether the branches are different'
value: ${{ steps.compare.outputs.diff }}

commits:
description: 'List of commits in head that are not in base'
value: ${{ steps.compare.outputs.commits }}

runs:
using: 'composite'
steps:
- name: 'Compare branches'
id: compare
shell: bash
run: |
RANGE="${{ inputs.remote }}/${{ inputs.base }}..${{ inputs.remote }}/${{ inputs.head }}"
COMMITS="$(git rev-list --count $RANGE)"
if [ $COMMITS -gt 0 ]; then
echo "::set-output name=diff::true"
echo "The following commits are in ${{ inputs.head }}, but not in ${{ inputs.base }}:" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Summary | Commit |" >> $GITHUB_STEP_SUMMARY
echo "| --- | --- |" >> $GITHUB_STEP_SUMMARY
git log --pretty=format:"| %s | [%h]($REPO_URL/commit/%H) |" $RANGE >> $GITHUB_STEP_SUMMARY
echo "::set-output name=commits::$(git log --pretty=format:"• %s" $RANGE | jq -sR)"
else
echo "::set-output name=diff::false"
echo "There are no commits in ${{ inputs.head }} that are not in ${{ inputs.base }}" >> $GITHUB_STEP_SUMMARY
fi

0 comments on commit 5ae87a7

Please sign in to comment.