From 1af38d02840f47fb9f786fb44d52cd8889c93b2a Mon Sep 17 00:00:00 2001 From: Mikhail Shchatko Date: Wed, 20 Nov 2024 23:05:56 +0200 Subject: [PATCH] SERVER-94187 Switch git ssh to https in copybara (#27212) GitOrigin-RevId: b7709884d7fd79ed7df2e80e1bcbbf0b3b95e8c9 --- buildscripts/sync_repo_with_copybara.py | 44 ++++++++++++++++--- copy.bara.sky | 6 +-- copybara.staging.sky => copy.bara.staging.sky | 15 ++++--- etc/evergreen_yml_components/definitions.yml | 1 + 4 files changed, 51 insertions(+), 15 deletions(-) rename copybara.staging.sky => copy.bara.staging.sky (56%) diff --git a/buildscripts/sync_repo_with_copybara.py b/buildscripts/sync_repo_with_copybara.py index 10e0b2a35c4..9bcde3c9bc9 100644 --- a/buildscripts/sync_repo_with_copybara.py +++ b/buildscripts/sync_repo_with_copybara.py @@ -1,5 +1,6 @@ """Module for syncing a repo with Copybara and setting up configurations.""" import argparse +import fileinput import subprocess import os import sys @@ -112,25 +113,56 @@ def main(): # Read configurations expansions = read_config_file(args.expansion_file) - access_token_copybara_syncer = get_installation_access_token( - expansions["app_id_copybara_syncer"], expansions["private_key_copybara_syncer"], - expansions["installation_id_copybara_syncer"]) + token_mongodb_mongo = get_installation_access_token( + expansions["app_id_copybara_syncer"], + expansions["private_key_copybara_syncer"], + expansions["installation_id_copybara_syncer"], + ) + token_10gen_mongo = get_installation_access_token( + expansions["app_id_copybara_syncer_10gen"], + expansions["private_key_copybara_syncer_10gen"], + expansions["installation_id_copybara_syncer_10gen"], + ) + + tokens_map = { + "https://github.com/mongodb/mongo.git": token_mongodb_mongo, + "https://github.com/10gen/mongo.git": token_10gen_mongo, + } # Create the mongodb-bot.gitconfig file as necessary. create_mongodb_bot_gitconfig() current_dir = os.getcwd() - git_destination_url_with_token = f"https://x-access-token:{access_token_copybara_syncer}@github.com/mongodb/mongo.git" + config_file = f"{current_dir}/copy.bara.sky" + + # Overwrite repo urls in copybara config in-place + with fileinput.FileInput(config_file, inplace=True) as file: + for line in file: + token = None + for repo, value in tokens_map.items(): + if repo in line: + token = value + + if token: + print( + line.replace( + "https://github.com", + f"https://x-access-token:{token}@github.com", + ), + end="", + ) + else: + print(line, end="") # Set up the Docker command and execute it docker_cmd = [ "docker run", "-v ~/.ssh:/root/.ssh", "-v ~/mongodb-bot.gitconfig:/root/.gitconfig", - f'-v "{current_dir}/copy.bara.sky":/usr/src/app/copy.bara.sky', + f'-v "{config_file}":/usr/src/app/copy.bara.sky', "-e COPYBARA_CONFIG='copy.bara.sky'", "-e COPYBARA_SUBCOMMAND='migrate'", - f"-e COPYBARA_OPTIONS='-v --git-destination-url={git_destination_url_with_token}'", + "-e COPYBARA_OPTIONS='-v'", "copybara copybara", ] diff --git a/copy.bara.sky b/copy.bara.sky index 4a904e71d40..549556a347a 100644 --- a/copy.bara.sky +++ b/copy.bara.sky @@ -5,8 +5,8 @@ # sourceUrl = "/path/to/source" # destinationUrl = "/path/to/dest" -sourceUrl = "git@github.com:10gen/mongo.git" -destinationUrl = "git@github.com:mongodb/mongo.git" +sourceUrl = "https://github.com/10gen/mongo.git" +destinationUrl = "https://github.com/mongodb/mongo.git" core.workflow( name = "default", @@ -25,7 +25,7 @@ core.workflow( mode = "ITERATIVE", transformations = [ # (^.*?) - matches the first line (without the newline char) - # \n - matches the first newline (or nothing at all if there is no newline). If there is no match then nopthing happens + # \n - matches the first newline (or nothing at all if there is no newline). If there is no match then nothing happens # ((\n|.)*) - matches everything after # Overall, this copies only the first line of the commit rather than the body metadata.scrubber("(^.*?)\n((\n|.)*)", replacement = "$1"), diff --git a/copybara.staging.sky b/copy.bara.staging.sky similarity index 56% rename from copybara.staging.sky rename to copy.bara.staging.sky index 2214af89bfb..01e0fdf20dd 100644 --- a/copybara.staging.sky +++ b/copy.bara.staging.sky @@ -1,14 +1,13 @@ # This configuration is for migrating code from one Git repository to another using Copybara. # It selectively copies content, excluding specific paths and preserving authorship. -sourceUrl = "git@github.com:10gen/mongo.git" -destinationUrl = "git@github.com:10gen/mongo-copybara.git" +sourceUrl = "https://github.com/10gen/mongo.git" +destinationUrl = "https://github.com/10gen/mongo-copybara.git" core.workflow( name = "default", origin = git.origin( url = sourceUrl, ref = "v7.0", - # VersionSelector ), destination = git.destination( url = destinationUrl, @@ -22,7 +21,11 @@ core.workflow( mode = "ITERATIVE", # Change the path here to the folder you want to publish publicly - # transformations = [ - # core.move("path/to/folder/you/want/exported", ""), - # ], + transformations = [ + # (^.*?) - matches the first line (without the newline char) + # \n - matches the first newline (or nothing at all if there is no newline). If there is no match then nothing happens + # ((\n|.)*) - matches everything after + # Overall, this copies only the first line of the commit rather than the body + metadata.scrubber("(^.*?)\n((\n|.)*)", replacement = "$1"), + ], ) diff --git a/etc/evergreen_yml_components/definitions.yml b/etc/evergreen_yml_components/definitions.yml index 54a924f34f5..8650eb13910 100644 --- a/etc/evergreen_yml_components/definitions.yml +++ b/etc/evergreen_yml_components/definitions.yml @@ -4611,6 +4611,7 @@ tasks: - name: sync_repo_with_copybara tags: [] + patchable: false commands: - command: manifest.load - func: "git get project and add git tag"