Skip to content

Commit

Permalink
Add originationUser into Run metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mehulkar committed May 11, 2023
1 parent 96ab801 commit 28d7c98
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 deletions.
27 changes: 17 additions & 10 deletions cli/internal/ci/vendors.go
Expand Up @@ -21,6 +21,9 @@ type Vendor struct {

// The name of the environment variable that contains the current checked out branch
BranchEnvVar string

// The name of the environment variable that contains the user using turbo
UsernameEnvVar string
}

// Vendors is a list of common CI/CD vendors (from https://github.com/watson/ci-info/blob/master/vendors.json)
Expand Down Expand Up @@ -112,12 +115,14 @@ var Vendors = []Vendor{
Constant: "EAS",
Env: vendorEnvs{Any: []string{"EAS_BUILD"}},
},
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
{
Name: "GitHub Actions",
Constant: "GITHUB_ACTIONS",
Env: vendorEnvs{Any: []string{"GITHUB_ACTIONS"}},
ShaEnvVar: "GITHUB_SHA",
BranchEnvVar: "GITHUB_REF_NAME",
Name: "GitHub Actions",
Constant: "GITHUB_ACTIONS",
Env: vendorEnvs{Any: []string{"GITHUB_ACTIONS"}},
ShaEnvVar: "GITHUB_SHA",
BranchEnvVar: "GITHUB_REF_NAME",
UsernameEnvVar: "GITHUB_ACTOR",
},
{
Name: "GitLab CI",
Expand Down Expand Up @@ -231,12 +236,14 @@ var Vendors = []Vendor{
Constant: "TRAVIS",
Env: vendorEnvs{Any: []string{"TRAVIS"}},
},
// https://vercel.com/docs/concepts/projects/environment-variables/system-environment-variables
{
Name: "Vercel",
Constant: "VERCEL",
Env: vendorEnvs{Any: []string{"NOW_BUILDER", "VERCEL"}},
ShaEnvVar: "VERCEL_GIT_COMMIT_SHA",
BranchEnvVar: "VERCEL_GIT_COMMIT_REF",
Name: "Vercel",
Constant: "VERCEL",
Env: vendorEnvs{Any: []string{"NOW_BUILDER", "VERCEL"}},
ShaEnvVar: "VERCEL_GIT_COMMIT_SHA",
BranchEnvVar: "VERCEL_GIT_COMMIT_REF",
UsernameEnvVar: "VERCEL_GIT_COMMIT_AUTHOR_LOGIN",
},
{
Name: "Visual Studio App Center",
Expand Down
1 change: 1 addition & 0 deletions cli/internal/runsummary/format_json.go
Expand Up @@ -74,4 +74,5 @@ type nonMonorepoRunSummary struct {
ExecutionSummary *executionSummary `json:"execution,omitempty"`
Tasks []*TaskSummary `json:"tasks"`
SCM *scmState `json:"scm"`
User string `json:"originationUser"`
}
23 changes: 22 additions & 1 deletion cli/internal/runsummary/run_summary.go
Expand Up @@ -11,7 +11,10 @@ import (

"github.com/mitchellh/cli"
"github.com/segmentio/ksuid"
"github.com/vercel/turbo/cli/internal/ci"
"github.com/vercel/turbo/cli/internal/client"
"github.com/vercel/turbo/cli/internal/env"
"github.com/vercel/turbo/cli/internal/scm"
"github.com/vercel/turbo/cli/internal/spinner"
"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/util"
Expand Down Expand Up @@ -69,6 +72,7 @@ type RunSummary struct {
ExecutionSummary *executionSummary `json:"execution,omitempty"`
Tasks []*TaskSummary `json:"tasks"`
SCM *scmState `json:"scm"`
User string `json:"originationUser"`
}

// NewRunSummary returns a RunSummary instance
Expand Down Expand Up @@ -100,6 +104,7 @@ func NewRunSummary(

executionSummary := newExecutionSummary(synthesizedCommand, repoPath, startAt, profile)

envVars := env.GetEnvMap()
return Meta{
RunSummary: &RunSummary{
ID: ksuid.New(),
Expand All @@ -111,7 +116,8 @@ func NewRunSummary(
FrameworkInference: runOpts.FrameworkInference,
Tasks: []*TaskSummary{},
GlobalHashSummary: globalHashSummary,
SCM: getSCMState(repoRoot),
SCM: getSCMState(envVars, repoRoot),
User: getUser(envVars, repoRoot),
},
ui: ui,
runType: runType,
Expand Down Expand Up @@ -329,3 +335,18 @@ func (rsm *Meta) postTaskSummaries(runID string) []error {

return nil
}

func getUser(envVars env.EnvironmentVariableMap, dir turbopath.AbsoluteSystemPath) string {
var username string

if ci.IsCi() {
vendor := ci.Info()
username = envVars[vendor.UsernameEnvVar]
}

if username == "" {
username = scm.GetCurrentUser(dir)
}

return username
}
7 changes: 3 additions & 4 deletions cli/internal/runsummary/scm_summary.go
Expand Up @@ -16,16 +16,15 @@ type scmState struct {
// getSCMState returns the sha and branch when in a git repo
// Otherwise it should return empty strings right now.
// We my add handling of other scms and non-git tracking in the future.
func getSCMState(dir turbopath.AbsoluteSystemPath) *scmState {
allEnvVars := env.GetEnvMap()
func getSCMState(envVars env.EnvironmentVariableMap, dir turbopath.AbsoluteSystemPath) *scmState {

state := &scmState{Type: "git"}

// If we're in CI, try to get the values we need from environment variables
if ci.IsCi() {
vendor := ci.Info()
state.Sha = allEnvVars[vendor.ShaEnvVar]
state.Branch = allEnvVars[vendor.BranchEnvVar]
state.Sha = envVars[vendor.ShaEnvVar]
state.Branch = envVars[vendor.BranchEnvVar]
}

// Otherwise fallback to using `git`
Expand Down
5 changes: 2 additions & 3 deletions cli/internal/runsummary/spaces.go
Expand Up @@ -28,9 +28,7 @@ type spacesRunPayload struct {
Client spacesClientSummary `json:"client"` // Details about the turbo client
GitBranch string `json:"gitBranch"`
GitSha string `json:"gitSha"`

// TODO: we need to add these in
// originationUser string
User string `json:"originationUser"`
}

// spacesCacheStatus is the same as TaskCacheSummary so we can convert
Expand Down Expand Up @@ -74,6 +72,7 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload {
Context: context,
GitBranch: rsm.RunSummary.SCM.Branch,
GitSha: rsm.RunSummary.SCM.Sha,
User: rsm.RunSummary.User,
Client: spacesClientSummary{
ID: "turbo",
Name: "Turbo",
Expand Down
14 changes: 14 additions & 0 deletions cli/internal/scm/scm.go
Expand Up @@ -78,3 +78,17 @@ func GetCurrentSha(dir turbopath.AbsoluteSystemPath) string {
}
return strings.TrimRight(string(out), "\n")
}

// GetCurrentUser returns the local user.name
// We do not specify a --local or --global flag so it should
// resolve the value the same way git does when creating a commit.
func GetCurrentUser(dir turbopath.AbsoluteSystemPath) string {
cmd := exec.Command("git", []string{"config", "user.name"}...)
cmd.Dir = dir.ToString()

out, err := cmd.Output()
if err != nil {
return ""
}
return strings.TrimRight(string(out), "\n")
}

0 comments on commit 28d7c98

Please sign in to comment.