Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update actions-branch-descendant.sh #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Branch Descendant

This action checks that a branch's nearest MST ancestor is the Master branch. If the nearest MST ancestor is master, then the action will succeed, otherwise it will fail. In either case the action returns the outputs listed below. The commit hashes followed to reach the closest MST parent branch are available in the action's logs, but are not returned as a variable for other actions to use. To see sample output from this action, check the pull requests and actions tabs of this repo. Read more about MST here: https://github.com/colpal/MST-branching.
This action checks that a branch's nearest MST ancestor is the Main branch. If the nearest MST ancestor is main, then the action will succeed, otherwise it will fail. In either case the action returns the outputs listed below. The commit hashes followed to reach the closest MST parent branch are available in the action's logs, but are not returned as a variable for other actions to use. To see sample output from this action, check the pull requests and actions tabs of this repo. Read more about MST here: https://github.com/colpal/MST-branching.

## Inputs

Expand All @@ -11,24 +11,24 @@ This action checks that a branch's nearest MST ancestor is the Master branch. If
### `valid`

#### Valid MST
A boolean indicating whether or not the closest MST ancestor branch is the Master branch. Either `true` or `false`.
A boolean indicating whether or not the closest MST ancestor branch is the Main branch. Either `true` or `false`.

### `PR-string`

#### Pretty String for PRs

A pretty string indicating whether or not the closest MST ancestor branch is the Master branch. Either `"Master branch is closest MST parent. PR good to go 👍"` or `"Master branch should be the closest MST parent. PR not good to go 👎"`.
A pretty string indicating whether or not the closest MST ancestor branch is the Main branch. Either `"Main branch is closest MST parent. PR good to go 👍"` or `"Main branch should be the closest MST parent. PR not good to go 👎"`.

### `closest-MST-parent`

#### Closest MST Ancestor

A string indicating which MST branch is the closest ancestor to the branch being considered. Either `"Master"`, `"Develop"`, or `"Test"`.
A string indicating which MST branch is the closest ancestor to the branch being considered. Either `"Main"`, `"Develop"`, or `"Test"`.

## Example usage

The following example runs the action on every pull request to the develop, test, or master branches. It then prints out the three outputs in the workflow logs, and leaves a comment containing the three outputs on the PR.
* Note that `continue-on-error` is set to `true` for this action. If this is not set, the action will fail when the nearest MST ancestor branch is not master, which will cause the action to fail. This may or may not be desirable depending on your use case.
The following example runs the action on every pull request to the develop, test, or main branches. It then prints out the three outputs in the workflow logs, and leaves a comment containing the three outputs on the PR.
* Note that `continue-on-error` is set to `true` for this action. If this is not set, the action will fail when the nearest MST ancestor branch is not main, which will cause the action to fail. This may or may not be desirable depending on your use case.
* Also note that `ref` is set to `${{ github.head_ref }}` in `actions/checkout@v2`. This is so that the action is checking the 'compare' branch and not the 'base' branch of the PR. Without this the action is unlikely to perform as expected.

```
Expand All @@ -38,7 +38,7 @@ on:
branches:
- develop
- test
- master
- main
jobs:
main:
runs-on: ubuntu-latest
Expand All @@ -51,7 +51,7 @@ jobs:
- name: Run colpal/actions-branch-descendant
id: actions-branch-descendant
continue-on-error: true
uses: colpal/actions-branch-descendant@master
uses: colpal/actions-branch-descendant@main

- name: Update Pull Request
uses: actions/[email protected]
Expand All @@ -63,9 +63,9 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `### ${ process.env.PR_STRING }
#### Closest MST parent branch needs to be: \`Master\`.
#### Closest MST parent branch needs to be: \`Main\`.
#### Closest MST parent branch is: \`${ process.env.PARENT }\`.
#### Closest MST parent is Master? \`${ process.env.VALID }\`.
#### Closest MST parent is Main? \`${ process.env.VALID }\`.
#### Read more about MST here: https://github.com/colpal/MST-branching.
### ${ process.env.PR_STRING }`;

Expand Down
6 changes: 3 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
name: Branch Descendant
description: Ensure closest MST parent branch is master.
description: Ensure closest MST parent branch is main.
outputs:
valid:
description: "If the branch's closest MST parent branch is Master."
description: "If the branch's closest MST parent branch is Main."
value: ${{ steps.actions-branch-descendant.outputs.valid }}
PR-string:
description: "String indicating whether or not PR is good to go."
value: ${{ steps.actions-branch-descendant.outputs.PR-string }}
closest-MST-parent:
description: "The closest MST parent. Either Master, Develop, or Test."
description: "The closest MST parent. Either Main, Develop, or Test."
value: ${{ steps.actions-branch-descendant.outputs.closest-MST-parent }}
runs:
using: "composite"
Expand Down
46 changes: 23 additions & 23 deletions actions-branch-descendant.sh
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
head_sha=`git rev-parse HEAD`
master_sha=`diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "origin/master") <(git rev-list --first-parent "HEAD") | head -1`
main_sha=`diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "origin/main") <(git rev-list --first-parent "HEAD") | head -1`
develop_sha=`diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "origin/develop") <(git rev-list --first-parent "HEAD") | head -1`
test_sha=`diff --old-line-format='' --new-line-format='' <(git rev-list --first-parent "origin/test") <(git rev-list --first-parent "HEAD") | head -1`

echo "HEAD SHA: $head_sha"
echo "Master Fork SHA: $master_sha"
echo "Main Fork SHA: $main_sha"
echo "Develop Fork SHA: $develop_sha"
echo "Test Fork SHA: $test_sha"
echo ""

if [ ! -z "$master_sha" ] && [ -z "$develop_sha" ] && [ -z "$test_sha" ]
if [ ! -z "$main_sha" ] && [ -z "$develop_sha" ] && [ -z "$test_sha" ]
then
echo "Master branch is closest MST parent. PR good to go 👍"
echo "Main branch is closest MST parent. PR good to go 👍"
echo "::set-output name=valid::true"
echo "::set-output name=closest-MST-parent::Master"
echo "::set-output name=PR-string::Master branch is closest MST parent. PR good to go 👍"
echo "::set-output name=closest-MST-parent::main"
echo "::set-output name=PR-string::Main branch is closest MST parent. PR good to go 👍"
exit 0
fi

if [ ! -z "$develop_sha" ] && [ -z "$master_sha" ] && [ -z "$test_sha" ]
if [ ! -z "$develop_sha" ] && [ -z "$main_sha" ] && [ -z "$test_sha" ]
then
echo "Develop branch is closest MST parent."
echo "::set-output name=closest-MST-parent::Develop"
echo "Master branch should be the closest MST parent. PR not good to go 👎"
echo "Main branch should be the closest MST parent. PR not good to go 👎"
echo "::set-output name=valid::false"
echo "::set-output name=PR-string::Master branch should be the closest MST parent. PR not good to go 👎"
echo "::set-output name=PR-string::Main branch should be the closest MST parent. PR not good to go 👎"
exit 1
fi

if [ ! -z "$test_sha" ] && [ -z "$master_sha" ] && [ -z "$develop_sha" ]
if [ ! -z "$test_sha" ] && [ -z "$main_sha" ] && [ -z "$develop_sha" ]
then
echo "Test branch is closest MST parent."
echo "::set-output name=closest-MST-parent::Test"
echo "Master branch should be the closest MST parent. PR not good to go 👎"
echo "Main branch should be the closest MST parent. PR not good to go 👎"
echo "::set-output name=valid::false"
echo "::set-output name=PR-string::Master branch should be the closest MST parent. PR not good to go 👎"
echo "::set-output name=PR-string::Main branch should be the closest MST parent. PR not good to go 👎"
exit 1
fi

: ${master_sha:="0"}
: ${main_sha:="0"}
: ${develop_sha:="0"}
: ${test_sha:="0"}

queue=($head_sha)
current_sha=${queue[0]}

echo "Ancestor SHA list going from HEAD -> master:"
echo "Ancestor SHA list going from HEAD -> main:"
echo "$current_sha"

while [ $current_sha != $master_sha ] && [ $current_sha != $test_sha ] && [ $current_sha != $develop_sha ]
while [ $current_sha != $main_sha ] && [ $current_sha != $test_sha ] && [ $current_sha != $develop_sha ]
do

queue+=(`git log --pretty=%P -n 1 "$current_sha"`)
queue=("${queue[@]:1}")
current_sha=${queue[0]}
echo "$current_sha"
done

echo ""
if [ $current_sha == $master_sha ]
if [ $current_sha == $main_sha ]
then
echo "Master branch is closest MST parent. PR good to go 👍"
echo "Main branch is closest MST parent. PR good to go 👍"
echo "::set-output name=valid::true"
echo "::set-output name=closest-MST-parent::Master"
echo "::set-output name=PR-string::Master branch is closest MST parent. PR good to go 👍"
echo "::set-output name=closest-MST-parent::main"
echo "::set-output name=PR-string::Main branch is closest MST parent. PR good to go 👍"
exit 0
fi
if [ $current_sha == $develop_sha ]
Expand All @@ -75,7 +75,7 @@ then
echo "Test branch is closest MST parent."
echo "::set-output name=closest-MST-parent::Test"
fi
echo "Master branch should be the closest MST parent. PR not good to go 👎"
echo "Main branch should be the closest MST parent. PR not good to go 👎"
echo "::set-output name=valid::false"
echo "::set-output name=PR-string::Master branch should be the closest MST parent. PR not good to go 👎"
exit 1
echo "::set-output name=PR-string::Main branch should be the closest MST parent. PR not good to go 👎"
exit 1