Skip to content

Commit

Permalink
Add Ubuntu get_related_branch_from_repo (#82)
Browse files Browse the repository at this point in the history
Signed-off-by: EduPonz <eduardoponz@eprosima.com>
  • Loading branch information
EduPonz authored Apr 2, 2024
1 parent e97873e commit 9cd58f0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ For more information about versioning handle of this project, check following [f

- [setup_cmake](ubuntu/setup_cmake/action.yml)
- Set a specific CMake version

- [get_related_branch_from_repo](ubuntu/get_related_branch_from_repo/action.yml)
- Get the related branch from a remote repository.
---

### macOS
Expand Down
112 changes: 112 additions & 0 deletions ubuntu/get_related_branch_from_repo/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
name: get_related_branch_from_repo
description: >
Get the related branch from a remote repository.
In case the event triggering the workflow is a "pull_request", the branch deduction is as follows:
1. The action will look for the PR's source branch in the remote repository.
2. If the PR's source branch is not found, the action will look for the PR's base branch in the remote repository.
3. If the PR's base branch is not found, the action will look for the fallback branch in the remote repository.
4. If the fallback branch is not found, the action will fail.
In case the event triggering the workflow is not a "pull_request", the action will look for the fallback branch in the remote repository, and fail if it is not found.
inputs:

remote_repository:
description: >
The remote repository from which to get the related branch.
Format: owner/repo (e.g.: eProsima/Fast-DDS).
required: true

fallback_branch:
description: >
The branch to use if no candidate branch can be found in the remote repository.
This is the only branch that is checked for existence in the remote repository in events other that "pull_request".
required: true

outputs:
deduced_branch:
description: "Deduced branch"
value: ${{ steps.deduce_branch.outputs.deduced_branch }}

runs:
using: composite
steps:

- name: Get related branch from remote repository
id: deduce_branch
run: |
# Function to check if a branch exists in a remote repository
# Usage: branch_exists <repository_url> <branch_name>
#
# Arguments:
# - repository_url: The URL of the remote repository
# - branch_name: The name of the branch to check
#
# Returns: "true" if the branch exists, "false" otherwise
function branch_exists ()
{
ret="false";
if [[ $(git ls-remote --heads ${1} ${2} | wc -l) != 0 ]]
then
ret="true"
fi
echo "$ret";
}
# Start by assuming we will fail
exit_code=1
remote_repo=https://github.com/${{ inputs.remote_repository }}.git
result_branch=${{ inputs.fallback_branch }}
# This is a PR event -> deduce which branch to use
if [[ ${{ github.event_name }} == "pull_request" ]]
then
# Attempt to use PR's source branch
if [[ $(branch_exists ${remote_repo} ${{ github.head_ref }}) == "true" ]]
then
result_branch=${{ github.head_ref }}
exit_code=0
fi
# Attempt to use PR's base branch
if [ ${exit_code} == "1" ] && [ $(branch_exists ${remote_repo} ${{ github.base_ref }}) == "true" ]
then
result_branch=${{ github.base_ref }}
exit_code=0
fi
# Attempt to use fallback branch, which will most likely be the base anyways.
# This is just in case the PR was to an intermediate branch
if [ ${exit_code} == "1" ] && [ $(branch_exists ${remote_repo} ${{ inputs.fallback_branch }}) == "true" ]
then
result_branch=${{ inputs.fallback_branch }}
exit_code=0
fi
# This is not a PR event -> check if fallback branch exists in the remote repository
else
if [[ $(branch_exists ${remote_repo} ${{ inputs.fallback_branch }}) == "true" ]]
then
result_branch=${{ inputs.fallback_branch }}
exit_code=0
fi
fi
# Set output if some deduced branch was found
if [[ ${exit_code} == "0" ]]
then
echo "Using deduced branch '${result_branch}' for ${remote_repo} repository"
echo "deduced_branch=${result_branch}" >> $GITHUB_OUTPUT
else
echo "No deduced branch found for ${remote_repo} repository"
fi
exit ${exit_code}
shell: bash
2 changes: 2 additions & 0 deletions versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ The [Forthcoming](#forthcoming) section includes those features added in `main`

The upcoming release will include the following **features**:

- New Ubuntu `get_related_branch_from_repo` action to get the related branch from a remote repository.

## v0.14.0

- New `add_labels` action to add labels to a pull request.
Expand Down

0 comments on commit 9cd58f0

Please sign in to comment.