Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submit originatingUser with Run #4875

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -73,5 +73,6 @@ type nonMonorepoRunSummary struct {
FrameworkInference bool `json:"frameworkInference"`
ExecutionSummary *executionSummary `json:"execution,omitempty"`
Tasks []*TaskSummary `json:"tasks"`
User string `json:"user"`
SCM *scmState `json:"scm"`
}
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 @@ -68,6 +71,7 @@ type RunSummary struct {
FrameworkInference bool `json:"frameworkInference"`
ExecutionSummary *executionSummary `json:"execution,omitempty"`
Tasks []*TaskSummary `json:"tasks"`
User string `json:"user"`
mehulkar marked this conversation as resolved.
Show resolved Hide resolved
SCM *scmState `json:"scm"`
}

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"`
mehulkar marked this conversation as resolved.
Show resolved Hide resolved
}

// 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")
}
1 change: 1 addition & 0 deletions turborepo-tests/integration/tests/dry_json/monorepo.t
Expand Up @@ -80,6 +80,7 @@ Setup
"scm",
"tasks",
"turboVersion",
"user",
"version"
]

Expand Down
Expand Up @@ -84,6 +84,7 @@ Setup
}
}
],
"user": "Turbo Test",
"scm": {
"type": "git",
"sha": "[a-z0-9]+", (re)
Expand Down
Expand Up @@ -78,6 +78,7 @@ Setup
}
}
],
"user": "Turbo Test",
"scm": {
"type": "git",
"sha": "[a-z0-9]+", (re)
Expand Down
Expand Up @@ -145,6 +145,7 @@ Setup
}
}
],
"user": "Turbo Test",
"scm": {
"type": "git",
"sha": "[a-z0-9]+", (re)
Expand Down
1 change: 1 addition & 0 deletions turborepo-tests/integration/tests/run_summary/monorepo.t
Expand Up @@ -40,6 +40,7 @@ Setup
"scm",
"tasks",
"turboVersion",
"user",
"version"
]

Expand Down
Expand Up @@ -39,6 +39,7 @@ Check
"scm",
"tasks",
"turboVersion",
"user",
"version"
]

Expand Down