Commit 3d120226 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch '7-sync_main_or_master' into 'master'

feat: sync master or main

Closes #7

See merge request to-be-continuous/tools/gitlab-sync!10
parents 3e97701a d875d030
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -108,6 +108,7 @@ function maybe_create_group() {
function sync_project() {
  local src_project_json=$1
  src_project_full_path=$(echo "$src_project_json" | jq -r '.path_with_namespace')
  src_main_branch=$(echo "$src_project_json" | jq -r '.default_branch')
  local dest_parent_id=$2
  src_project_id=${src_project_full_path//\//%2f}
  local dest_group_full_path=$3
@@ -156,7 +157,7 @@ function sync_project() {
          }")
      fi
        # \"visibility\": \"$(echo "$src_project_json" | jq -r .visibility)\",
      dest_latest_commit=$(curl -sSf -H "${DEST_TOKEN+PRIVATE-TOKEN: $DEST_TOKEN}" "$DEST_GITLAB_API/projects/$dest_project_id/repository/commits?ref_name=master&per_page=1" | jq -r '.[0].id')
      dest_latest_commit=$(curl -sSf -H "${DEST_TOKEN+PRIVATE-TOKEN: $DEST_TOKEN}" "$DEST_GITLAB_API/projects/$dest_project_id/repository/commits?ref_name=$src_main_branch&per_page=1" | jq -r '.[0].id')
    else
      # another error: abort
      fail "... unexpected error: $dest_project_status"
@@ -178,14 +179,14 @@ function sync_project() {
  # if project already exists: unprotect master branch first
  if [[ "$dest_project_status" == 200* ]]
  then
    log_info "... unprotect 'master' branch (allow failure)"
    curl -sS -H "${DEST_TOKEN+PRIVATE-TOKEN: $DEST_TOKEN}" -X DELETE "$DEST_GITLAB_API/projects/$dest_project_id/protected_branches/master" > /dev/null
    log_info "... unprotect $src_main_branch branch (allow failure)"
    curl -sS -H "${DEST_TOKEN+PRIVATE-TOKEN: $DEST_TOKEN}" -X DELETE "$DEST_GITLAB_API/projects/$dest_project_id/protected_branches/$src_main_branch" > /dev/null
  fi

  # 2: sync Git repository
  if [[ "$DEST_GITLAB_API" ]]
  then
    src_latest_commit=$(curl -sSf -H "${SRC_TOKEN+PRIVATE-TOKEN: $SRC_TOKEN}" "$SRC_GITLAB_API/projects/$src_project_id/repository/commits?ref_name=master&per_page=1" | jq -r '.[0].id')
    src_latest_commit=$(curl -sSf -H "${SRC_TOKEN+PRIVATE-TOKEN: $SRC_TOKEN}" "$SRC_GITLAB_API/projects/$src_project_id/repository/commits?ref_name=$src_main_branch&per_page=1" | jq -r '.[0].id')
    if [[ "$src_latest_commit" == "$dest_latest_commit" ]]
    then
      log_info "... source and destination repositories are on same latest commit ($src_latest_commit): skip sync"
@@ -210,7 +211,7 @@ function sync_project() {
        # shellcheck disable=SC2001
        dest_repo_url=$(echo "$dest_repo_url" | sed -e "s|://|://token:${DEST_TOKEN}@|")
      fi
      git push --force "$dest_repo_url" --tags master
      git push --force "$dest_repo_url" --tags "$src_main_branch"
      cd ..
    fi
  fi
@@ -218,8 +219,8 @@ function sync_project() {
  # if project didn't exist: unprotect master branch
  if [[ "$dest_project_status" == 404* ]]
  then
    log_info "... unprotect 'master' branch (allow failure)"
    curl -sS -H "${DEST_TOKEN+PRIVATE-TOKEN: $DEST_TOKEN}" -X DELETE "$DEST_GITLAB_API/projects/$dest_project_id/protected_branches/master" > /dev/null
    log_info "... unprotect $src_main_branch branch (allow failure)"
    curl -sS -H "${DEST_TOKEN+PRIVATE-TOKEN: $DEST_TOKEN}" -X DELETE "$DEST_GITLAB_API/projects/$dest_project_id/protected_branches/$src_main_branch" > /dev/null
  fi
}