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

Conversation

JesusPoderoso
Copy link
Contributor

@JesusPoderoso JesusPoderoso commented Mar 14, 2023

INPUTS:

  • remote repo name
  • current branch (HEAD on current repo)
  • base branch (BASE on current repo)
  • variable to record the remote repo branch name

LOGICS:

  • check if current branch (HEAD) exists in remote repo; if not:
  • check if base branch (BASE) exists in remote repo; if not:
  • return 'master', or 'main'

this feature has not been tested deeply yet

Signed-off-by: JesusPoderoso <[email protected]>
@@ -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

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?

run: |
echo "::group::Get the branch name of ${{ inputs.remote_repository }}"
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.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


- 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

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. 🔫

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants