From 413e41a31e1d1f525eb8ebfd4148635ff15900bd Mon Sep 17 00:00:00 2001
From: Update Bot <update-bot@private.storage>
Date: Fri, 15 Jul 2022 14:14:38 -0400
Subject: [PATCH] take more values as inputs instead of guessing

---
 .gitlab-ci.yml          |  8 ++++++-
 ci-tools/update-nixpkgs | 50 +++++++++++++++++++++++++++--------------
 2 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 321a74d6..5be10e26 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -163,4 +163,10 @@ update-nixpkgs:
   <<: *RUN_ON_SCHEDULE
   script:
     - |
-      ./ci-tools/with-ssh-agent ./ci-tools/update-nixpkgs whetstone.private.storage PrivateStorage/PrivateStorageio "$CI_PROJECT_ID"
+      ./ci-tools/with-ssh-agent \
+          ./ci-tools/update-nixpkgs \
+              "$CI_SERVER_URL" \
+              "$CI_SERVER_HOST" \
+              "$CI_PROJECT_PATH" \
+              "$CI_PROJECT_ID" \
+              "$CI_DEFAULT_BRANCH"
diff --git a/ci-tools/update-nixpkgs b/ci-tools/update-nixpkgs
index e9696325..22e46c74 100755
--- a/ci-tools/update-nixpkgs
+++ b/ci-tools/update-nixpkgs
@@ -20,13 +20,18 @@ main() {
     local TOKEN=$1
     shift
 
-    # This is the hostname of the GitLab instance where the project lives.
-    local HOST=$1
+    # This is the URL of the root of the GitLab API.
+    local SERVER_URL=$1
+    shift
+
+    # This is the hostname of the GitLab server (suitable for use in a Git
+    # remote).
+    local SERVER_HOST=$1
     shift
 
     # This is the "group/project"-style identifier for the project we're working
     # with.
-    local SLUG=$1
+    local PROJECT_PATH=$1
     shift
 
     # The GitLab id of the project (eg, from CI_PROJECT_ID in the CI
@@ -34,6 +39,11 @@ main() {
     local PROJECT_ID=$1
     shift
 
+    # The name of the branch on which to base changes and which to target with
+    # the resulting merge request.
+    local DEFAULT_BRANCH=$1
+    shift
+
     # Only proceed if we have an ssh-agent.
     check_agent
 
@@ -41,7 +51,7 @@ main() {
     local SOURCE_BRANCH="nixpkgs-upgrade-$(date +%Y-%m-%d)"
 
     setup_git
-    checkout_source_branch "$SSHKEY" "$HOST" "$SLUG" "$SOURCE_BRANCH"
+    checkout_source_branch "$SSHKEY" "$SERVER_HOST" "$PROJECT_PATH" "$DEFAULT_BRANCH" "$SOURCE_BRANCH"
     build "result-before"
 
     # If nothing changed, update_nixpkgs will just exit for us.
@@ -50,7 +60,7 @@ main() {
     build "result-after"
     local DIFF=$(compute_diff "./result-before" "./result-after")
     commit_and_push "$SSHKEY" "$SOURCE_BRANCH" "$DIFF"
-    create_merge_request "$HOST" "$TOKEN" "$PROJECT_ID" "$SOURCE_BRANCH" "$DIFF"
+    create_merge_request "$SERVER_URL" "$TOKEN" "$PROJECT_ID" "$DEFAULT_BRANCH" "$SOURCE_BRANCH" "$DIFF"
 }
 
 # Add the ssh key required to push and (maybe) pull to the ssh-agent.  This
@@ -91,10 +101,14 @@ check_agent() {
 checkout_source_branch() {
     local SSHKEY=$1
     shift
-    local HOST=$1
+    local SERVER_HOST=$1
     shift
-    local SLUG=$1
+    local PROJECT_PATH=$1
     shift
+    # The branch we'll start from.
+    local DEFAULT_BRANCH=$1
+    shift
+    # The name of our branch.
     local BRANCH=$1
     shift
 
@@ -102,9 +116,9 @@ checkout_source_branch() {
     # tends to like to share across builds) clone it to a new temporary path.
     git clone . working-copy
     cd working-copy
-    git remote add upstream gitlab@"$HOST":"$SLUG".git
+    git remote add upstream gitlab@"$SERVER_HOST":"$PROJECT_PATH".git
     refresh_ssh_key "$SSHKEY"
-    git fetch upstream develop
+    git fetch upstream "$DEFAULT_BRANCH"
     # Typically this tool runs infrequently enough that the branch doesn't
     # already exist.  However, as a convenience for developing on this tool
     # itself, if it does already exist, wipe it and start fresh for greater
@@ -112,7 +126,7 @@ checkout_source_branch() {
     git branch -D "${BRANCH}" || true
 
     # Then create a new branch starting from the mainline development branch.
-    git checkout -B "${BRANCH}" upstream/develop
+    git checkout -B "${BRANCH}" upstream/"$DEFAULT_BRANCH"
 }
 
 # Do the "before nixpkgs change" build to use as the base of the diff we
@@ -177,11 +191,13 @@ $DIFF
 # Create a GitLab MR for the branch we just pushed, including a description of
 # the package changes it implies.
 create_merge_request() {
-    local HOST=$1
+    local SERVER_URL=$1
     shift
     local TOKEN=$1
     shift
-    local CI_PROJECT_ID=$1
+    local PROJECT_ID=$1
+    shift
+    local DEFAULT_BRANCH=$1
     shift
     local BRANCH=$1
     shift
@@ -194,15 +210,15 @@ def rewrite_escapes(s):
     return re.sub(r"\x1b\[[^m]*m", "", s)
 print(json.dumps({
     "id": sys.argv[1],
-    "source_branch": sys.argv[2],
-    "target_branch": "develop",
+    "target_branch": sys.argv[2],
+    "source_branch": sys.argv[3],
     "remove_source_branch": True,
     "title": "bump nixpkgs version",
-    "description": f"```\n{rewrite_escapes(sys.argv[3])}\n```",
+    "description": f"```\n{rewrite_escapes(sys.argv[4])}\n```",
 }))
-' "$CI_PROJECT_ID" "$BRANCH" "$DIFF")
+' "$PROJECT_ID" "$DEFAULT_BRANCH" "$BRANCH" "$DIFF")
 
-    curl --verbose -X POST --data "${BODY}" --header "Content-Type: application/json" --header "PRIVATE-TOKEN: ${TOKEN}" "https://${HOST}/api/v4/projects/${CI_PROJECT_ID}/merge_requests"
+    curl --verbose -X POST --data "${BODY}" --header "Content-Type: application/json" --header "PRIVATE-TOKEN: ${TOKEN}" "${SERVER_URL}/api/v4/projects/${PROJECT_ID}/merge_requests"
 }
 
 # Pull the private ssh key and GitLab token from the environment here so we
-- 
GitLab