|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | +set -x |
| 4 | +# Function to calculate the corresponding branch of Xetabase project |
| 5 | +get_xetabase_branch() { |
| 6 | + # Input parameter (branch name) |
| 7 | + # Input parameter (branch name) |
| 8 | + target_branch="$1" |
| 9 | + current_branch="$2" |
| 10 | + |
| 11 | + # If the branch begins with 'TASK' and exists in the opencga-enterprise repository, I return it |
| 12 | + if [[ $current_branch == TASK* ]]; then |
| 13 | + REPO_URI= |
| 14 | + if [ -z "$ZETTA_REPO_ACCESS_TOKEN" ]; then |
| 15 | + REPO_URI="git@github.com:zetta-genomics/opencga-enterprise.git" |
| 16 | + else |
| 17 | + REPO_URI="https://$ZETTA_REPO_ACCESS_TOKEN@github.com/zetta-genomics/opencga-enterprise.git" |
| 18 | + fi |
| 19 | + if [ "$(git ls-remote "$REPO_URI" "$current_branch" )" ] ; then |
| 20 | + echo "$current_branch"; |
| 21 | + return 0; |
| 22 | + fi |
| 23 | + fi |
| 24 | + |
| 25 | + # Check if the branch name is "develop" in that case return the same branch name |
| 26 | + if [[ "$target_branch" == "develop" ]]; then |
| 27 | + echo "develop" |
| 28 | + return 0 |
| 29 | + fi |
| 30 | + |
| 31 | + # Check if the branch name starts with "release-" and follows the patterns "release-a.x.x" or "release-a.b.x" |
| 32 | + if [[ "$target_branch" =~ ^release-([0-9]+)\.x\.x$ ]] || [[ "$target_branch" =~ ^release-([0-9]+)\.([0-9]+)\.x$ ]]; then |
| 33 | + # Extract the MAJOR part of the branch name |
| 34 | + MAJOR=${BASH_REMATCH[1]} |
| 35 | + # Calculate the XETABASE_MAJOR by subtracting 3 from MAJOR |
| 36 | + XETABASE_MAJOR=$((MAJOR - 3)) |
| 37 | + # Check if the XETABASE_MAJOR is negative |
| 38 | + if (( XETABASE_MAJOR < 0 )); then |
| 39 | + echo "Error: 'MAJOR' digit after subtraction results in a negative number." |
| 40 | + return 1 |
| 41 | + fi |
| 42 | + # Construct and echo the new branch name |
| 43 | + echo "release-$XETABASE_MAJOR.${target_branch#release-$MAJOR.}" |
| 44 | + return 0 |
| 45 | + fi |
| 46 | + |
| 47 | + # If the branch name does not match any of the expected patterns |
| 48 | + echo "Error: The branch name is not correct." |
| 49 | + return 1 |
| 50 | +} |
| 51 | + |
| 52 | +# Check if the script receives exactly one argument |
| 53 | +if [ "$#" -ne 2 ]; then |
| 54 | + echo "Usage: $0 <target-branch> <current-branch>" |
| 55 | + exit 1 |
| 56 | +fi |
| 57 | + |
| 58 | +# Call the function with the input branch name |
| 59 | +get_xetabase_branch "$1" "$2" |
0 commit comments