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

Get best-fitting branch name of a given repo based on current branch name (on current repo) action #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions ubuntu/get_branch/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: 'get_branch'
description: 'Get the branch name of the given repository that fits with the given repo@branch'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, explain a bit more the logic of the algorithm


inputs:

result_env_var:
description: 'Environment Variable name for result'
required: true

head_ref:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume head and base are used here as arguments because of where this action will be used from.
I would change these names for attempt1 attempt2 or something different that has no meaning itself (head and base has meaning in git already, and it is not the same as the meaning used here).

Let's be pythonic, the function does not depend on where it is called. Who calls it dependens on the function. 🔫

description: 'Reference to Git Head to compare with'
required: true

base_ref:
description: 'Reference to Git current'
required: true

remote_repository:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT;

Suggested change
remote_repository:
source_repository:

It does not need to be remote, is it?

description: 'Remote Github repository name'
required: true

runs:
using: composite
steps:

- name: get_branch
run: |
echo "::group::Get the branch name of ${{ inputs.remote_repository }}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "::group::Get the branch name of ${{ inputs.remote_repository }}"
echo "::group::Get the branch name of ${{ inputs.remote_repository }}"

Please, keep same format as other actions when possible

echo "using head '${{ inputs.head_ref }}' branch"
DOC_REPO=https://github.com/eProsima/${{ remote_repository }}.git
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use user/repo_name as repository name, and not only the name.
This is how is done in other internal and external actions (e.g. https://github.com/actions/checkout#usage)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also helps you to use this action with variable $GITHUB_REPOSITORY that retrieves the info of "this" repo in user/name format

RESPONSE_CODE=$(git ls-remote --heads $DOC_REPO ${{ inputs.head_ref }} | wc -l)
if [[ ${RESPONSE_CODE} == "0" ]]
then
echo "head '${{ inputs.head_ref }}' branch DOES NOT exist, using base '${{ github.base_ref }}'"
RESPONSE_CODE=$(git ls-remote --heads $DOC_REPO ${{ inputs.base_ref }} | wc -l)
if [[ ${RESPONSE_CODE} == "0" ]]
then
echo "base '${{ inputs.base_ref }}' branch DOES NOT exist, using 'MASTER'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if what you are looking for is the default branch (main, master or whatever) use this:
curl -s "https://api.github.com/repos/${{ inputs.remote_repository }}" | jq -r '.default_branch'

Check:
https://dev.to/bowmanjd/get-github-default-branch-from-the-command-line-powershell-or-bash-zsh-37m9

RESPONSE_CODE=$(git ls-remote --heads $DOC_REPO master | wc -l)
if [[ ${RESPONSE_CODE} == "0" ]]
then
echo "'MASTER' branch DOES NOT exist, using 'MAIN'"
RESPONSE_CODE=$(git ls-remote --heads $DOC_REPO main | wc -l)
if [[ ${RESPONSE_CODE} == "0" ]]
then
echo "'main' branch DOES NOT exist, using ''"
echo "${{ inputs.result_env_var }}=" >> $GITHUB_ENV
else
echo "${{ inputs.result_env_var }}=main" >> $GITHUB_ENV
fi
else
echo "${{ inputs.result_env_var }}=master" >> $GITHUB_ENV
fi
else
echo "${{ inputs.result_env_var }}=${{ inputs.base_ref }}" >> $GITHUB_ENV
fi
else
echo "${{ inputs.result_env_var }}=${{ inputs.head_ref }}" >> $GITHUB_ENV
fi

echo "::endgroup::"

shell: bash