Skip to content

Commit

Permalink
internal/civisibility: add extra tags to github action pull_requests …
Browse files Browse the repository at this point in the history
…runs (#2922)
  • Loading branch information
tonyredondo authored Oct 10, 2024
1 parent fc58656 commit 2f2854f
Show file tree
Hide file tree
Showing 4 changed files with 573 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/civisibility/constants/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ const (
// GitTag indicates the current git tag.
// This constant is used to tag traces with the tag name associated with the current commit.
GitTag = "git.tag"

// GitHeadCommit indicates the GIT head commit hash.
GitHeadCommit = "git.commit.head_sha"

// GitPrBaseCommit indicates the GIT PR base commit hash.
GitPrBaseCommit = "git.pull_request.base_branch_sha"

// GitPrBaseBranch indicates the GIT PR base branch name.
GitPrBaseBranch = "git.pull_request.base_branch"
)
32 changes: 32 additions & 0 deletions internal/civisibility/utils/ci_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,38 @@ func extractGithubActions() map[string]string {
tags[constants.CIEnvVars] = string(jsonString)
}

// Extract PR information from the github event json file
eventFilePath := os.Getenv("GITHUB_EVENT_PATH")
if stats, ok := os.Stat(eventFilePath); ok == nil && !stats.IsDir() {
if eventFile, err := os.Open(eventFilePath); err == nil {
defer eventFile.Close()

var eventJson struct {
PullRequest struct {
Base struct {
Sha string `json:"sha"`
Ref string `json:"ref"`
} `json:"base"`
Head struct {
Sha string `json:"sha"`
} `json:"head"`
} `json:"pull_request"`
}

eventDecoder := json.NewDecoder(eventFile)
if eventDecoder.Decode(&eventJson) == nil {
tags[constants.GitHeadCommit] = eventJson.PullRequest.Head.Sha
tags[constants.GitPrBaseCommit] = eventJson.PullRequest.Base.Sha
tags[constants.GitPrBaseBranch] = eventJson.PullRequest.Base.Ref
}
}
}

// Fallback if GitPrBaseBranch is not set
if tmpVal, ok := tags[constants.GitPrBaseBranch]; !ok || tmpVal == "" {
tags[constants.GitPrBaseBranch] = os.Getenv("GITHUB_BASE_REF")
}

return tags
}

Expand Down
42 changes: 42 additions & 0 deletions internal/civisibility/utils/ci_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"path/filepath"
"strings"
"testing"

"gopkg.in/DataDog/dd-trace-go.v1/internal/civisibility/constants"
)

func setEnvs(t *testing.T, env map[string]string) {
Expand Down Expand Up @@ -114,3 +116,43 @@ func TestTags(t *testing.T) {
})
}
}

func TestGitHubEventFile(t *testing.T) {
originalEventPath := os.Getenv("GITHUB_EVENT_PATH")
originalBaseRef := os.Getenv("GITHUB_BASE_REF")
defer func() {
os.Setenv("GITHUB_EVENT_PATH", originalEventPath)
os.Setenv("GITHUB_BASE_REF", originalBaseRef)
}()

os.Unsetenv("GITHUB_EVENT_PATH")
os.Unsetenv("GITHUB_BASE_REF")

checkValue := func(tags map[string]string, key, expectedValue string) {
if tags[key] != expectedValue {
t.Fatalf("Key: %s, the actual value (%s) is different to the expected value (%s)", key, tags[key], expectedValue)
}
}

t.Run("with event file", func(t *testing.T) {
eventFile := "testdata/fixtures/github-event.json"
t.Setenv("GITHUB_EVENT_PATH", eventFile)
t.Setenv("GITHUB_BASE_REF", "my-base-ref") // this should be ignored in favor of the event file value

tags := extractGithubActions()
expectedHeadCommit := "df289512a51123083a8e6931dd6f57bb3883d4c4"
expectedBaseCommit := "52e0974c74d41160a03d59ddc73bb9f5adab054b"
expectedBaseRef := "main"

checkValue(tags, constants.GitHeadCommit, expectedHeadCommit)
checkValue(tags, constants.GitPrBaseCommit, expectedBaseCommit)
checkValue(tags, constants.GitPrBaseBranch, expectedBaseRef)
})

t.Run("no event file", func(t *testing.T) {
t.Setenv("GITHUB_BASE_REF", "my-base-ref") // this should be ignored in favor of the event file value

tags := extractGithubActions()
checkValue(tags, constants.GitPrBaseBranch, "my-base-ref")
})
}
Loading

0 comments on commit 2f2854f

Please sign in to comment.