Skip to content

Commit 6fce1a0

Browse files
fix(ci_visibility): use git tag as branch name if no branch name available [backport 3.8] (#13494)
Backport 88cbdc8 from #13468 to 3.8. When called from a GitHub action triggered on a _tag_ push (not a branch push), we would have no branch name available and make some requests to the backend with a `null` branch, causing a 400 to be returned. This PR changes it so that the tag name is used as the branch name in such circumstances. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) (cherry picked from commit 88cbdc8)
1 parent c17a3bd commit 6fce1a0

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

ddtrace/internal/ci_visibility/git_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class GitData:
1515
def get_git_data_from_tags(tags: t.Dict[str, t.Any]) -> GitData:
1616
return GitData(
1717
repository_url=tags.get(ci.git.REPOSITORY_URL),
18-
branch=tags.get(ci.git.BRANCH),
18+
branch=tags.get(ci.git.BRANCH) or tags.get(ci.git.TAG),
1919
commit_sha=tags.get(ci.git.COMMIT_SHA),
2020
commit_message=tags.get(ci.git.COMMIT_MESSAGE),
2121
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
CI Visibility: This fix resolves an issue where running from a GitHub action triggered on a tag push would cause the
5+
branch name to be null, causing errors when fetching Test Optimization settings from the backend.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from ddtrace.ext import ci
2+
from ddtrace.internal.ci_visibility.git_data import GitData
3+
from ddtrace.internal.ci_visibility.git_data import get_git_data_from_tags
4+
5+
6+
def test_git_data_with_branch():
7+
tags = {
8+
ci.git.REPOSITORY_URL: "github.com/some-org/some-repo",
9+
ci.git.BRANCH: "some-branch",
10+
ci.git.COMMIT_SHA: "some-sha",
11+
ci.git.COMMIT_MESSAGE: "this is a message",
12+
}
13+
14+
expected_git_data = GitData(
15+
repository_url="github.com/some-org/some-repo",
16+
branch="some-branch",
17+
commit_sha="some-sha",
18+
commit_message="this is a message",
19+
)
20+
21+
assert get_git_data_from_tags(tags) == expected_git_data
22+
23+
24+
def test_git_data_with_tag():
25+
tags = {
26+
ci.git.REPOSITORY_URL: "github.com/some-org/some-repo",
27+
ci.git.TAG: "v1.2.3",
28+
ci.git.COMMIT_SHA: "some-sha",
29+
ci.git.COMMIT_MESSAGE: "this is a message",
30+
}
31+
32+
expected_git_data = GitData(
33+
repository_url="github.com/some-org/some-repo",
34+
branch="v1.2.3",
35+
commit_sha="some-sha",
36+
commit_message="this is a message",
37+
)
38+
39+
assert get_git_data_from_tags(tags) == expected_git_data
40+
41+
42+
def test_git_data_with_neither_branch_nor_tag():
43+
tags = {
44+
ci.git.REPOSITORY_URL: "github.com/some-org/some-repo",
45+
ci.git.COMMIT_SHA: "some-sha",
46+
ci.git.COMMIT_MESSAGE: "this is a message",
47+
}
48+
49+
expected_git_data = GitData(
50+
repository_url="github.com/some-org/some-repo",
51+
branch=None,
52+
commit_sha="some-sha",
53+
commit_message="this is a message",
54+
)
55+
56+
assert get_git_data_from_tags(tags) == expected_git_data

0 commit comments

Comments
 (0)