gitlab.com/gitlab-org/labkit@v1.21.0/downstream-vendor.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  ###################################################
     4  # Downstream Vendor Utility
     5  ###################################################
     6  # This script will attempt to vendor the latest
     7  # version of labkit into downstream projects
     8  #
     9  # This script should only be run on master (for now)
    10  # It assumed that the person executing the script
    11  # has Git+SSH push access to GitLab repositories on
    12  # gitlab.com
    13  #
    14  # A new merge request will be created for each
    15  # downstream project
    16  ###################################################
    17  
    18  set -euo pipefail
    19  IFS=$'\n\t'
    20  
    21  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    22  cd "${SCRIPT_DIR}"
    23  
    24  declare -A DOWNSTREAM_PROJECTS=(
    25    ["gitlab-org/gitlab"]="workhorse"
    26    ["gitlab-org/gitaly"]=""
    27    ["gitlab-org/gitlab-pages"]=""
    28    ["gitlab-org/gitlab-shell"]=""
    29    ["gitlab-org/container-registry"]=""
    30    ["gitlab-org/cluster-integration/gitlab-agent"]=""
    31  )
    32  
    33  MERGE_REQUESTS=()
    34  
    35  LABKIT_VERSION_TAG=$(git describe --tags --abbrev=0)
    36  
    37  WORKING_DIR=$(mktemp -d)
    38  trap '{ rm -rf "${WORKING_DIR}"; }' EXIT
    39  pushd "${WORKING_DIR}"
    40  
    41  # Pushes to a remote and returns the GitLab Merge Request IID
    42  function git_push_and_return_merge_request_iid() {
    43    # This could be done more elegantly with gnu-grep, but we can't assume that
    44    # everyone user (macos users mainly) will have it
    45    git push "$@" 2>&1 >/dev/null | grep /merge_requests/ | sed 's#.*/merge_requests/##;s# ##g'
    46  }
    47  
    48  # Fetch the description from the Releases API
    49  function get_labkit_release_summary() {
    50    curl --silent --fail \
    51      "https://gitlab.com/api/v4/projects/gitlab-org%2flabkit/releases/${LABKIT_VERSION_TAG}" |
    52      jq -r '.description' |
    53      grep -v '^#' |
    54      grep -v '^$'
    55  }
    56  
    57  function vendor_downstream_project() {
    58    local project=$1
    59    local subdir=$2
    60    local path=${project##*/}
    61    local branch_name="vendor-$LABKIT_VERSION_TAG"
    62    local vendor_branch_exists
    63    local merge_request_iid
    64    local requires_amend=no
    65    local summary
    66  
    67    echo "# cloning ${project}"
    68  
    69    git clone "git@gitlab.com:${project}.git" "${path}"
    70    pushd "${path}/${subdir}"
    71  
    72    if git ls-remote --exit-code --heads origin "${branch_name}"; then
    73      vendor_branch_exists=yes
    74      git checkout -b "${branch_name}" "origin/${branch_name}"
    75    else
    76      vendor_branch_exists=no
    77      git checkout -b "${branch_name}"
    78    fi
    79  
    80    # Update to the appropriate commit SHA
    81    go get -u=patch "gitlab.com/gitlab-org/labkit@$LABKIT_VERSION_TAG"
    82    go mod tidy
    83    git add go.mod go.sum
    84  
    85    if [[ "$vendor_branch_exists" == "yes" ]]; then
    86      git commit --amend --no-edit
    87      merge_request_iid=$(git_push_and_return_merge_request_iid --force-with-lease)
    88      echo "# updated merge request https://gitlab.com/${project}/-/merge_requests/${merge_request_iid}"
    89    else
    90      summary=$(get_labkit_release_summary)
    91  
    92      git commit -m "Update LabKit library to $LABKIT_VERSION_TAG
    93  
    94  ${summary}
    95  
    96  See https://gitlab.com/gitlab-org/labkit/-/releases/$LABKIT_VERSION_TAG"
    97  
    98      merge_request_iid=$(
    99        git_push_and_return_merge_request_iid \
   100          -o merge_request.create \
   101          -o merge_request.target=master \
   102          -o merge_request.remove_source_branch \
   103          --set-upstream origin "$(git rev-parse --abbrev-ref HEAD)"
   104      )
   105  
   106      echo "# created merge request https://gitlab.com/${project}/-/merge_requests/${merge_request_iid}"
   107    fi
   108  
   109    MERGE_REQUESTS+=("https://gitlab.com/${project}/-/merge_requests/${merge_request_iid}")
   110  
   111    # Add a changelog entry?
   112    if [[ -x _support/changelog ]]; then
   113      if _support/changelog \
   114        --merge-request "${merge_request_iid}" \
   115        --type other \
   116        "Update LabKit to $LABKIT_VERSION_TAG"; then
   117        requires_amend=yes
   118        git add "changelogs/unreleased/*"
   119      fi
   120    fi
   121  
   122    ## Temporary hack until we come up with a better solution
   123    if [[ "$project" == "gitlab-org/cluster-integration/gitlab-agent" ]]; then
   124      if make update-repos; then
   125        requires_amend=yes
   126        git status
   127        git add "build/*"
   128      fi
   129    fi
   130  
   131    if [[ "$requires_amend" == "yes" ]]; then
   132      git commit --amend --no-edit
   133      git push --force-with-lease
   134    fi
   135  
   136    popd
   137  }
   138  
   139  for project in "${!DOWNSTREAM_PROJECTS[@]}"; do
   140    vendor_downstream_project "${project}" "${DOWNSTREAM_PROJECTS[$project]}"
   141  done
   142  
   143  echo "####################################"
   144  echo "# completed successfully"
   145  for mr_url in "${MERGE_REQUESTS[@]}"; do
   146    echo "# ${mr_url}"
   147  done