-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add gitbranch and sha into spaces payload #4734
Merged
mehulkar
merged 36 commits into
main
from
mehulkar/turbo-1049-add-git-sha-and-branch-to-run
May 3, 2023
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
7da2236
Add gitbranch and sha into spaces payload
mehulkar 305be40
rm debug
mehulkar 084a1e7
comments
mehulkar 5f1a8ae
Merge branch 'main' into mehulkar/turbo-1049-add-git-sha-and-branch-t…
mehulkar 8cfeb18
return struct instead of tuple
mehulkar 213b9f5
Use branch --show-current to get branch name instead
mehulkar 0d57d8c
ONly use stdout, ignore stderr
mehulkar 8d1db8d
git state
mehulkar 4de2dbf
look up env vars more simply
mehulkar c8553ff
rm comment
mehulkar 0975950
Merge branch 'main' into mehulkar/turbo-1049-add-git-sha-and-branch-t…
mehulkar 9455443
Merge branch 'main' into mehulkar/turbo-1049-add-git-sha-and-branch-t…
mehulkar ef0621b
Move gitState to run summary instead of just spaces
mehulkar da9917a
add tests, fix key, remove newline char
mehulkar 1e9d30d
Set CWD when getting git info
mehulkar dd4a603
compile err
mehulkar 35dda2b
Merge branch 'main' into mehulkar/turbo-1049-add-git-sha-and-branch-t…
mehulkar c1e3d68
branch names can be anything
mehulkar bffe3ce
stringify at the end
mehulkar 2199840
Merge branch 'main' into mehulkar/turbo-1049-add-git-sha-and-branch-t…
mehulkar 21ff859
Use scm key/struct naming for run summary, keep git fields for spaces
mehulkar f1daab1
Fix tests
mehulkar c480e15
add unit tests for git branch/sha
mehulkar ced3468
more logging on fail
mehulkar 23c13ac
print byte string correctly
mehulkar cdda67c
setup git first
9363c2e
Merge branch 'main' into mehulkar/turbo-1049-add-git-sha-and-branch-t…
6e5aa1c
yes
fae2b88
only set local git user in test, so it doesn't blow up dev machines
c443620
set local without flag
29410e3
set --global git config in tests and reset after
mehulkar b37ef03
Merge branch 'main' into mehulkar/turbo-1049-add-git-sha-and-branch-t…
ae52ed8
Merge branch 'main' into mehulkar/turbo-1049-add-git-sha-and-branch-t…
467feb2
show which cmd failed
90ee2f5
Ignore errors when no global config exists
9440bbe
Merge branch 'main' into mehulkar/turbo-1049-add-git-sha-and-branch-t…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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(dir turbopath.AbsoluteSystemPath) *scmState { | ||
allEnvVars := env.GetEnvMap() | ||
|
||
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] | ||
} | ||
|
||
// Otherwise fallback to using `git` | ||
if state.Branch == "" { | ||
state.Branch = scm.GetCurrentBranch(dir) | ||
} | ||
|
||
if state.Sha == "" { | ||
state.Sha = scm.GetCurrentSha(dir) | ||
} | ||
|
||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package scm | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/vercel/turbo/cli/internal/fs" | ||
"github.com/vercel/turbo/cli/internal/turbopath" | ||
) | ||
|
||
func TestGetCurrentBranchMain(t *testing.T) { | ||
targetbranch := "main" | ||
testDir := getTestDir(t, "myrepo") | ||
originalName, originalEmail := getOriginalConfig(testDir) | ||
|
||
// Setup git | ||
gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) | ||
gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) | ||
gitCommand(t, testDir, []string{"init"}) | ||
|
||
gitCommand(t, testDir, []string{"checkout", "-B", targetbranch}) | ||
branch := GetCurrentBranch(testDir) | ||
assert.Equal(t, branch, targetbranch) | ||
|
||
// cleanup | ||
gitRm(t, testDir) | ||
gitCommand(t, testDir, []string{"config", "--global", "user.email", originalEmail}) | ||
gitCommand(t, testDir, []string{"config", "--global", "user.name", originalName}) | ||
} | ||
|
||
func TestGetCurrentBranchNonMain(t *testing.T) { | ||
targetbranch := "mybranch" | ||
testDir := getTestDir(t, "myrepo") | ||
|
||
originalName, originalEmail := getOriginalConfig(testDir) | ||
|
||
// Setup git | ||
gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) | ||
gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) | ||
gitCommand(t, testDir, []string{"init"}) | ||
gitCommand(t, testDir, []string{"checkout", "-B", targetbranch}) | ||
|
||
branch := GetCurrentBranch(testDir) | ||
assert.Equal(t, branch, targetbranch) | ||
|
||
// cleanup | ||
gitRm(t, testDir) | ||
gitCommand(t, testDir, []string{"config", "--global", "user.email", originalEmail}) | ||
gitCommand(t, testDir, []string{"config", "--global", "user.name", originalName}) | ||
} | ||
|
||
func TestGetCurrentSHA(t *testing.T) { | ||
testDir := getTestDir(t, "myrepo") | ||
originalName, originalEmail := getOriginalConfig(testDir) | ||
|
||
// Setup git | ||
gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) | ||
gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) | ||
gitCommand(t, testDir, []string{"init"}) | ||
|
||
// initial sha is blank because there are no commits | ||
initSha := GetCurrentSha(testDir) | ||
assert.True(t, initSha == "", "initial sha is empty") | ||
|
||
// first commit | ||
gitCommand(t, testDir, []string{"commit", "--allow-empty", "-am", "new commit"}) | ||
sha1 := GetCurrentSha(testDir) | ||
assert.True(t, sha1 != "sha on commit 1 is not empty") | ||
|
||
// second commit | ||
gitCommand(t, testDir, []string{"commit", "--allow-empty", "-am", "new commit"}) | ||
sha2 := GetCurrentSha(testDir) | ||
assert.True(t, sha2 != "", "sha on commit 2 is not empty") | ||
assert.True(t, sha2 != sha1, "sha on commit 2 changes from commit 1") | ||
|
||
// cleanup | ||
gitRm(t, testDir) | ||
gitCommand(t, testDir, []string{"config", "--global", "user.email", originalEmail}) | ||
gitCommand(t, testDir, []string{"config", "--global", "user.name", originalName}) | ||
} | ||
|
||
// Helper functions | ||
func getTestDir(t *testing.T, testName string) turbopath.AbsoluteSystemPath { | ||
defaultCwd, err := os.Getwd() | ||
if err != nil { | ||
t.Errorf("failed to get cwd: %v", err) | ||
} | ||
cwd, err := fs.CheckedToAbsoluteSystemPath(defaultCwd) | ||
if err != nil { | ||
t.Fatalf("cwd is not an absolute directory %v: %v", defaultCwd, err) | ||
} | ||
|
||
return cwd.UntypedJoin("testdata", testName) | ||
} | ||
|
||
func gitRm(t *testing.T, dir turbopath.AbsoluteSystemPath) { | ||
cmd := exec.Command("rm", []string{"-rf", ".git"}...) | ||
cmd.Dir = dir.ToString() | ||
if out, err := cmd.Output(); err != nil { | ||
t.Fatalf("Failed to cleanup git dir: %s\n%v", out, err) | ||
} | ||
} | ||
|
||
func getOriginalConfig(cwd turbopath.AbsoluteSystemPath) (string, string) { | ||
// Ignore errors. If there was an error, it's likely because there was no value for these | ||
// configs (e.g. in CI), so git is returning non-zero status code. This is ok, and we'll use the | ||
// zero-value empty strings. | ||
name, _ := _gitCommand(cwd, []string{"config", "--global", "user.name"}) | ||
email, _ := _gitCommand(cwd, []string{"config", "--global", "user.name"}) | ||
|
||
return name, email | ||
} | ||
|
||
func gitCommand(t *testing.T, cwd turbopath.AbsoluteSystemPath, args []string) string { | ||
out, err := _gitCommand(cwd, args) | ||
|
||
if err != nil { | ||
t.Fatalf("Failed git command %s: %s\n%v", args, out, err) | ||
} | ||
|
||
return string(out) | ||
} | ||
|
||
func _gitCommand(cwd turbopath.AbsoluteSystemPath, args []string) (string, error) { | ||
cmd := exec.Command("git", args...) | ||
cmd.Dir = cwd.ToString() | ||
out, err := cmd.CombinedOutput() | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(out), nil | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,7 @@ Setup | |
"globalCacheInputs", | ||
"id", | ||
"packages", | ||
"scm", | ||
"tasks", | ||
"turboVersion", | ||
"version" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I thought there was even the remotest chance that we would add alternative SCM support prior to removing this code I'd be asking for an enum.
If you're feeling industrious I'd head that direction, but very non-blocking.