From a7dd125e17d4fddf0e8e6683c1630282d7d6d3ff Mon Sep 17 00:00:00 2001 From: Nicolas Schweitzer Date: Tue, 17 Dec 2024 09:17:42 +0100 Subject: [PATCH] feat(release): Set gitconfig before git write operations --- .github/workflows/create_rc_pr.yml | 1 + tasks/collector.py | 10 +++++----- tasks/libs/common/git.py | 20 +++++++++++--------- tasks/libs/common/utils.py | 11 ++++++++++- tasks/release.py | 15 ++++++++------- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/.github/workflows/create_rc_pr.yml b/.github/workflows/create_rc_pr.yml index 2a685c3cc85df..4f827591fc62d 100644 --- a/.github/workflows/create_rc_pr.yml +++ b/.github/workflows/create_rc_pr.yml @@ -106,6 +106,7 @@ jobs: env: MATRIX: ${{ matrix.value }} run: | + git fetch if ${{ env.IS_AGENT6_RELEASE == 'true' }}; then inv -e release.create-rc -r "$MATRIX" --slack-webhook=${{ secrets.AGENT_RELEASE_SYNC_SLACK_WEBHOOK }} --patch-version else diff --git a/tasks/collector.py b/tasks/collector.py index 74f3a439bd9fd..052fda7e8dcbe 100644 --- a/tasks/collector.py +++ b/tasks/collector.py @@ -496,7 +496,7 @@ def update(self): @task(post=[tidy]) -def update(ctx): +def update(_): updater = CollectorVersionUpdater() updater.update() print("Update complete.") @@ -505,12 +505,12 @@ def update(ctx): @task() def pull_request(ctx): # Save current Git configuration - original_config = {'user.name': get_git_config('user.name'), 'user.email': get_git_config('user.email')} + original_config = {'user.name': get_git_config(ctx, 'user.name'), 'user.email': get_git_config(ctx, 'user.email')} try: # Set new Git configuration - set_git_config('user.name', 'github-actions[bot]') - set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com') + set_git_config(ctx, 'user.name', 'github-actions[bot]') + set_git_config(ctx, 'user.email', 'github-actions[bot]@users.noreply.github.com') # Perform Git operations ctx.run('git add .') @@ -533,4 +533,4 @@ def pull_request(ctx): print("No changes detected, skipping PR creation.") finally: # Revert to original Git configuration - revert_git_config(original_config) + revert_git_config(ctx, original_config) diff --git a/tasks/libs/common/git.py b/tasks/libs/common/git.py index b1dc1b1cd7683..54b95f28f18a8 100644 --- a/tasks/libs/common/git.py +++ b/tasks/libs/common/git.py @@ -1,7 +1,6 @@ from __future__ import annotations import os -import subprocess import sys import tempfile from contextlib import contextmanager @@ -295,18 +294,21 @@ def get_last_release_tag(ctx, repo, pattern): return last_tag_commit, last_tag_name -def get_git_config(key): - result = subprocess.run(['git', 'config', '--get', key], capture_output=True, text=True) - return result.stdout.strip() if result.returncode == 0 else None +def get_git_config(ctx, key): + try: + result = ctx.run(f'git config --get {key}') + except Exit: + return None + return result.stdout.strip() if result.return_code == 0 else None -def set_git_config(key, value): - subprocess.run(['git', 'config', key, value]) +def set_git_config(ctx, key, value): + ctx.run(f'git config {key} {value}') -def revert_git_config(original_config): +def revert_git_config(ctx, original_config): for key, value in original_config.items(): if value is None: - subprocess.run(['git', 'config', '--unset', key]) + ctx.run(f'git config --unset {key}', hide=True) else: - subprocess.run(['git', 'config', key, value]) + ctx.run(f'git config {key} {value}', hide=True) diff --git a/tasks/libs/common/utils.py b/tasks/libs/common/utils.py index 56e96ba23403f..4ba452adff56e 100644 --- a/tasks/libs/common/utils.py +++ b/tasks/libs/common/utils.py @@ -23,7 +23,7 @@ from tasks.libs.common.color import Color, color_message from tasks.libs.common.constants import ALLOWED_REPO_ALL_BRANCHES, REPO_PATH -from tasks.libs.common.git import get_commit_sha, get_default_branch +from tasks.libs.common.git import get_commit_sha, get_default_branch, set_git_config from tasks.libs.releasing.version import get_version from tasks.libs.types.arch import Arch @@ -506,6 +506,15 @@ def is_pr_context(branch, pr_id, test_name): return True +def set_gitconfig_in_ci(ctx): + """ + Set username and email when runing git "write" commands in CI + """ + if running_in_ci(): + set_git_config('user.name', 'github-actions[bot]') + set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com') + + @contextmanager def gitlab_section(section_name, collapsed=False, echo=False): """ diff --git a/tasks/release.py b/tasks/release.py index 077834b279923..1feaaea57700a 100644 --- a/tasks/release.py +++ b/tasks/release.py @@ -39,6 +39,7 @@ ) from tasks.libs.common.gomodules import get_default_modules from tasks.libs.common.user_interactions import yes_no_question +from tasks.libs.common.utils import set_gitconfig_in_ci from tasks.libs.common.worktree import agent_context from tasks.libs.pipeline.notifications import ( DEFAULT_JIRA_PROJECT, @@ -254,6 +255,7 @@ def tag_modules( if push: tags_list = ' '.join(tags) + set_gitconfig_in_ci(ctx) ctx.run(f"git push origin {tags_list}{force_option}") print(f"Pushed tag {tags_list}") print(f"Created module tags for version {agent_version}") @@ -289,6 +291,7 @@ def tag_version( if push: tags_list = ' '.join(tags) + set_gitconfig_in_ci(ctx) ctx.run(f"git push origin {tags_list}{force_option}") print(f"Pushed tag {tags_list}") print(f"Created tags for version {agent_version}") @@ -349,6 +352,7 @@ def finish(ctx, release_branch, upstream="origin"): commit_message = f"'Final updates for release.json and Go modules for {new_version} release'" + set_gitconfig_in_ci(ctx) ok = try_git_command(ctx, f"git commit -m {commit_message}") if not ok: raise Exit( @@ -437,12 +441,6 @@ def create_rc(ctx, release_branch, patch_version=False, upstream="origin", slack with agent_context(ctx, release_branch): github = GithubAPI(repository=GITHUB_REPO_NAME) - github_action = os.environ.get("GITHUB_ACTIONS") - - if github_action: - set_git_config('user.name', 'github-actions[bot]') - set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com') - upstream = f"https://x-access-token:{os.environ.get('GITHUB_TOKEN')}@github.com/{GITHUB_REPO_NAME}.git" # Get the version of the highest major: useful for some logging & to get # the version to use for Go submodules updates @@ -496,10 +494,10 @@ def create_rc(ctx, release_branch, patch_version=False, upstream="origin", slack ctx.run("git add release.json") ctx.run("git ls-files . | grep 'go.mod$' | xargs git add") + set_gitconfig_in_ci(ctx) ok = try_git_command( ctx, f"git commit --no-verify -m 'Update release.json and Go modules for {new_highest_version}'", - github_action, ) if not ok: raise Exit( @@ -680,6 +678,7 @@ def _main(): # Step 2 - Push newly created release branch to the remote repository print(color_message("Pushing new branch to the upstream repository", "bold")) + set_gitconfig_in_ci(ctx) res = ctx.run(f"git push --set-upstream {upstream} {release_branch}", warn=True) if res.exited is None or res.exited > 0: raise Exit( @@ -871,6 +870,7 @@ def cleanup(ctx, release_branch): ctx.run("git add release.json") commit_message = f"Update last_stable to {version}" + set_gitconfig_in_ci(ctx) ok = try_git_command(ctx, f"git commit -m '{commit_message}'") if not ok: raise Exit( @@ -1184,6 +1184,7 @@ def check_for_changes(ctx, release_branch, warning_mode=False): with clone(ctx, repo_name, repo['branch'], options="--filter=blob:none --no-checkout"): # We can add the new commit now to be used by release candidate creation print(f"Creating new tag {next_version} on {repo_name}", file=sys.stderr) + set_gitconfig_in_ci(ctx) ctx.run(f"git tag {next_version}") ctx.run(f"git push origin tag {next_version}") # This repo has changes, the next check is not needed