-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Ubuntu get_related_branch_from_repo (#82)
Signed-off-by: EduPonz <eduardoponz@eprosima.com>
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters