-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
scm_summary.go
40 lines (32 loc) · 1.02 KB
/
scm_summary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package runsummary
import (
"github.com/vercel/turbo/cli/internal/ci"
"github.com/vercel/turbo/cli/internal/env"
"github.com/vercel/turbo/cli/internal/scm"
"github.com/vercel/turbo/cli/internal/turbopath"
)
type scmState struct {
Type string `json:"type"`
Sha string `json:"sha"`
Branch string `json:"branch"`
}
// 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(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 = envVars[vendor.ShaEnvVar]
state.Branch = envVars[vendor.BranchEnvVar]
}
// Otherwise fallback to using `git`
if state.Branch == "" {
state.Branch = scm.GetCurrentBranch(dir)
}
if state.Sha == "" {
state.Sha = scm.GetCurrentSha(dir)
}
return state
}