Skip to content

Commit 5ea6a0f

Browse files
committed
feat(release): Set gitconfig before git write operations
1 parent 5a71ece commit 5ea6a0f

File tree

5 files changed

+35
-22
lines changed

5 files changed

+35
-22
lines changed

.github/workflows/create_rc_pr.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ jobs:
119119
env:
120120
MATRIX: ${{ matrix.value }}
121121
run: |
122+
git fetch
122123
if ${{ env.IS_AGENT6_RELEASE == 'true' }}; then
123124
inv -e release.create-rc -r "$MATRIX" --slack-webhook=${{ secrets.AGENT6_RELEASE_SLACK_WEBHOOK }} --patch-version
124125
else

tasks/collector.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def update(self):
509509

510510

511511
@task(post=[tidy])
512-
def update(ctx):
512+
def update(_):
513513
updater = CollectorVersionUpdater()
514514
updater.update()
515515
print("Update complete.")
@@ -518,12 +518,12 @@ def update(ctx):
518518
@task()
519519
def pull_request(ctx):
520520
# Save current Git configuration
521-
original_config = {'user.name': get_git_config('user.name'), 'user.email': get_git_config('user.email')}
521+
original_config = {'user.name': get_git_config(ctx, 'user.name'), 'user.email': get_git_config(ctx, 'user.email')}
522522

523523
try:
524524
# Set new Git configuration
525-
set_git_config('user.name', 'github-actions[bot]')
526-
set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com')
525+
set_git_config(ctx, 'user.name', 'github-actions[bot]')
526+
set_git_config(ctx, 'user.email', 'github-actions[bot]@users.noreply.github.com')
527527

528528
# Perform Git operations
529529
ctx.run('git add .')
@@ -554,4 +554,4 @@ def pull_request(ctx):
554554
print("No changes detected, skipping PR creation.")
555555
finally:
556556
# Revert to original Git configuration
557-
revert_git_config(original_config)
557+
revert_git_config(ctx, original_config)

tasks/libs/common/git.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import os
4-
import subprocess
54
import sys
65
import tempfile
76
from contextlib import contextmanager
@@ -304,18 +303,21 @@ def get_last_release_tag(ctx, repo, pattern):
304303
return last_tag_commit, last_tag_name
305304

306305

307-
def get_git_config(key):
308-
result = subprocess.run(['git', 'config', '--get', key], capture_output=True, text=True)
309-
return result.stdout.strip() if result.returncode == 0 else None
306+
def get_git_config(ctx, key):
307+
try:
308+
result = ctx.run(f'git config --get {key}')
309+
except Exit:
310+
return None
311+
return result.stdout.strip() if result.return_code == 0 else None
310312

311313

312-
def set_git_config(key, value):
313-
subprocess.run(['git', 'config', key, value])
314+
def set_git_config(ctx, key, value):
315+
ctx.run(f'git config {key} {value}')
314316

315317

316-
def revert_git_config(original_config):
318+
def revert_git_config(ctx, original_config):
317319
for key, value in original_config.items():
318320
if value is None:
319-
subprocess.run(['git', 'config', '--unset', key])
321+
ctx.run(f'git config --unset {key}', hide=True)
320322
else:
321-
subprocess.run(['git', 'config', key, value])
323+
ctx.run(f'git config {key} {value}', hide=True)

tasks/libs/common/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from tasks.libs.common.color import Color, color_message
2525
from tasks.libs.common.constants import ALLOWED_REPO_ALL_BRANCHES, REPO_PATH
26-
from tasks.libs.common.git import get_commit_sha, get_default_branch
26+
from tasks.libs.common.git import get_commit_sha, get_default_branch, set_git_config
2727
from tasks.libs.releasing.version import get_version
2828
from tasks.libs.types.arch import Arch
2929

@@ -501,6 +501,15 @@ def is_pr_context(branch, pr_id, test_name):
501501
return True
502502

503503

504+
def set_gitconfig_in_ci(ctx):
505+
"""
506+
Set username and email when runing git "write" commands in CI
507+
"""
508+
if running_in_ci():
509+
set_git_config('user.name', 'github-actions[bot]')
510+
set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com')
511+
512+
504513
@contextmanager
505514
def gitlab_section(section_name, collapsed=False, echo=False):
506515
"""

tasks/release.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
)
4242
from tasks.libs.common.gomodules import get_default_modules
4343
from tasks.libs.common.user_interactions import yes_no_question
44+
from tasks.libs.common.utils import set_gitconfig_in_ci
4445
from tasks.libs.common.worktree import agent_context
4546
from tasks.libs.pipeline.notifications import (
4647
DEFAULT_JIRA_PROJECT,
@@ -206,6 +207,7 @@ def tag_modules(
206207

207208
if push:
208209
tags_list = ' '.join(tags)
210+
set_gitconfig_in_ci(ctx)
209211
for idx in range(0, len(tags), TAG_BATCH_SIZE):
210212
batch_tags = tags[idx : idx + TAG_BATCH_SIZE]
211213
ctx.run(f"git push origin {' '.join(batch_tags)}{force_option}")
@@ -250,6 +252,7 @@ def tag_version(
250252
with agent_context(ctx, release_branch, skip_checkout=release_branch is None):
251253
tags = __tag_single_module(ctx, get_default_modules()["."], agent_version, commit, force_option, devel)
252254

255+
set_gitconfig_in_ci(ctx)
253256
# create or update the qualification tag using the force option (points tag to next RC)
254257
if is_agent6(ctx) and (start_qual or is_qualification(ctx, "6.53.x")):
255258
if FINAL_VERSION_RE.match(agent_version):
@@ -323,6 +326,7 @@ def finish(ctx, release_branch, upstream="origin"):
323326

324327
commit_message = f"'Final updates for release.json and Go modules for {new_version} release'"
325328

329+
set_gitconfig_in_ci(ctx)
326330
ok = try_git_command(ctx, f"git commit -m {commit_message}")
327331
if not ok:
328332
raise Exit(
@@ -394,12 +398,6 @@ def create_rc(ctx, release_branch, patch_version=False, upstream="origin", slack
394398

395399
with agent_context(ctx, release_branch):
396400
github = GithubAPI(repository=GITHUB_REPO_NAME)
397-
github_action = os.environ.get("GITHUB_ACTIONS")
398-
399-
if github_action:
400-
set_git_config('user.name', 'github-actions[bot]')
401-
set_git_config('user.email', 'github-actions[bot]@users.noreply.github.com')
402-
upstream = f"https://x-access-token:{os.environ.get('GITHUB_TOKEN')}@github.com/{GITHUB_REPO_NAME}.git"
403401

404402
# Get the version of the highest major: useful for some logging & to get
405403
# the version to use for Go submodules updates
@@ -454,10 +452,10 @@ def create_rc(ctx, release_branch, patch_version=False, upstream="origin", slack
454452
ctx.run("git add release.json")
455453
ctx.run("git ls-files . | grep 'go.mod$' | xargs git add")
456454

455+
set_gitconfig_in_ci(ctx)
457456
ok = try_git_command(
458457
ctx,
459458
f"git commit --no-verify -m 'Update release.json and Go modules for {new_highest_version}'",
460-
github_action,
461459
)
462460
if not ok:
463461
raise Exit(
@@ -673,6 +671,7 @@ def _main():
673671
# Step 2 - Push newly created release branch to the remote repository
674672

675673
print(color_message("Pushing new branch to the upstream repository", "bold"))
674+
set_gitconfig_in_ci(ctx)
676675
res = ctx.run(f"git push --set-upstream {upstream} {release_branch}", warn=True)
677676
if res.exited is None or res.exited > 0:
678677
raise Exit(
@@ -863,6 +862,7 @@ def cleanup(ctx, release_branch):
863862
ctx.run("git add release.json")
864863

865864
commit_message = f"Update last_stable to {version}"
865+
set_gitconfig_in_ci(ctx)
866866
ok = try_git_command(ctx, f"git commit -m '{commit_message}'")
867867
if not ok:
868868
raise Exit(
@@ -1175,6 +1175,7 @@ def check_for_changes(ctx, release_branch, warning_mode=False):
11751175
with clone(ctx, repo_name, repo['branch'], options="--filter=blob:none --no-checkout"):
11761176
# We can add the new commit now to be used by release candidate creation
11771177
print(f"Creating new tag {next_version} on {repo_name}", file=sys.stderr)
1178+
set_gitconfig_in_ci(ctx)
11781179
ctx.run(f"git tag {next_version}")
11791180
ctx.run(f"git push origin tag {next_version}")
11801181
# This repo has changes, the next check is not needed

0 commit comments

Comments
 (0)