Skip to content

Commit 344a06d

Browse files
chore: use go-git for git ops in test coverage workflow (#83)
* chore: use go-git for test coverage git related ops Signed-off-by: Siddhant N Trivedi <siddhant@deepsource.io> * chore: add sentry exception and print error Signed-off-by: Siddhant N Trivedi <siddhant@deepsource.io> * chore: support GIT_COMMIT_SHA as an env var for docker container reporting Signed-off-by: Siddhant N Trivedi <siddhant@deepsource.io> * Build without arch when building locally Co-authored-by: Sourya Vatsyayan <sourya@deepsource.io>
1 parent 03df826 commit 344a06d

4 files changed

Lines changed: 154 additions & 42 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ build:
22
cd cmd/deepsource && GOOS=linux GOARCH=amd64 go build -tags static_all -o /tmp/deepsource .
33

44
build_local:
5-
cd cmd/deepsource && GOOS=darwin GOARCH=amd64 go build -tags static_all -o /tmp/deepsource .
5+
cd cmd/deepsource && go build -tags static_all -o /tmp/deepsource .
66

77
test:
88
CGO_ENABLED=0 go test --cover -coverprofile=coverage.out -v ./command/report/tests/... -run TestReportKeyValueWorkflow

command/report/git.go

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
package report
22

33
import (
4-
"bytes"
4+
"fmt"
55
"os"
6-
"os/exec"
7-
"strings"
6+
7+
"github.com/getsentry/sentry-go"
8+
"github.com/go-git/go-git/v5"
9+
"github.com/go-git/go-git/v5/plumbing"
810
)
911

1012
// gitGetHead accepts a git directory and returns head commit OID / error
1113
func gitGetHead(workspaceDir string) (string, error) {
14+
// TRAVIS CI
1215
// Get USER env variable.
13-
envUser := os.Getenv("USER")
14-
if envUser == "travis" {
16+
if envUser := os.Getenv("USER"); envUser == "travis" {
1517
// Travis creates a merge commit for pull requests on forks.
1618
// The head of commit is this merge commit, which does not match the commit of deepsource check.
17-
1819
// Fetch value of pull request SHA. If this is a PR, it will return SHA of HEAD commit of the PR, else "".
19-
prSHA := os.Getenv("TRAVIS_PULL_REQUEST_SHA")
20-
2120
// If prSHA is not empty, that means we got an SHA, which is HEAD. Return this.
22-
if len(prSHA) > 0 {
21+
if prSHA := os.Getenv("TRAVIS_PULL_REQUEST_SHA"); len(prSHA) > 0 {
2322
return prSHA, nil
2423
}
25-
2624
}
2725

28-
// Check if it is a GitHub Action Environment, If it is then get
29-
// the HEAD from `GITHUB_SHA` environment
26+
// GITHUB ACTIONS
27+
// Check if it is a GitHub Action Environment
28+
// If it is: then get the HEAD from `GITHUB_SHA` environment
3029
if _, isGitHubEnv := os.LookupEnv("GITHUB_ACTIONS"); isGitHubEnv {
3130
// When GITHUB_REF is not set, GITHUB_SHA points to original commit.
3231
// When set, it points to the "latest *merge* commit in the branch".
@@ -36,27 +35,44 @@ func gitGetHead(workspaceDir string) (string, error) {
3635
}
3736
}
3837

38+
// Check if the `GIT_COMMIT_SHA` environment variable exists. If yes, return this as
39+
// the latest commit sha.
40+
// This is used in cases when the user:
41+
// 1. Is using a CI other than GH Actions/ Travis CI (handled above)
42+
// 2. Wants to report tcv from inside a docker container in which they are running tests.
43+
// In this scenario, the container doesn't have data about the latest git commit sha so
44+
// it is injected by the user manually while running the container.
45+
// Example:
46+
// GIT_COMMIT_SHA=$(git --no-pager rev-parse HEAD | tr -d '\n')
47+
// docker run -e DEEPSOURCE_DSN -e GIT_COMMIT_SHA ...
48+
if _, isManuallyInjectedSHA := os.LookupEnv("GIT_COMMIT_SHA"); isManuallyInjectedSHA {
49+
return os.Getenv("GIT_COMMIT_SHA"), nil
50+
}
51+
3952
// If we are here, it means this is neither GitHub Action on default branch,
4053
// nor a travis env with PR. Continue to fetch the headOID via the git command.
41-
headOID := ""
42-
43-
cmd := exec.Command("git", "--no-pager", "rev-parse", "HEAD")
44-
cmd.Dir = workspaceDir
45-
46-
var stdout, stderr bytes.Buffer
47-
cmd.Stdout = &stdout
48-
cmd.Stderr = &stderr
49-
50-
err := cmd.Run()
51-
52-
outStr, _ := stdout.String(), stderr.String()
53-
54+
headOID, err := fetchHeadManually(workspaceDir)
5455
if err != nil {
55-
return headOID, err
56+
fmt.Println(err)
57+
sentry.CaptureException(err)
58+
return "", err
5659
}
60+
return headOID, nil
61+
}
5762

58-
// Trim newline suffix from Commit OID
59-
headOID = strings.TrimSuffix(outStr, "\n")
63+
// Fetches the latest commit hash using the command `git rev-parse HEAD`
64+
// through go-git
65+
func fetchHeadManually(directoryPath string) (string, error) {
66+
// Open a new repository targeting the given path (the .git folder)
67+
repo, err := git.PlainOpen(directoryPath)
68+
if err != nil {
69+
return "", err
70+
}
6071

61-
return headOID, nil
72+
// Resolve revision into a sha1 commit
73+
commitHash, err := repo.ResolveRevision(plumbing.Revision("HEAD"))
74+
if err != nil {
75+
return "", err
76+
}
77+
return commitHash.String(), nil
6278
}

go.mod

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,32 @@ go 1.16
55
require (
66
github.com/AlecAivazis/survey/v2 v2.2.12
77
github.com/Jeffail/gabs/v2 v2.6.1
8+
github.com/Microsoft/go-winio v0.5.1 // indirect
9+
github.com/ProtonMail/go-crypto v0.0.0-20211112122917-428f8eabeeb3 // indirect
810
github.com/cli/browser v1.1.0
911
github.com/deepsourcelabs/graphql v0.2.2
1012
github.com/fatih/color v1.12.0
1113
github.com/getsentry/sentry-go v0.6.0
14+
github.com/go-git/go-git v4.7.0+incompatible
15+
github.com/go-git/go-git-fixtures/v4 v4.2.2 // indirect
16+
github.com/go-git/go-git/v5 v5.4.2
1217
github.com/google/go-cmp v0.5.5 // indirect
18+
github.com/kevinburke/ssh_config v1.1.0 // indirect
19+
github.com/kr/pretty v0.3.0 // indirect
1320
github.com/matryer/is v1.4.0 // indirect
1421
github.com/mattn/go-isatty v0.0.14 // indirect
1522
github.com/mitchellh/mapstructure v1.4.1 // indirect
1623
github.com/pelletier/go-toml v1.9.2
1724
github.com/pterm/pterm v0.12.23
25+
github.com/rogpeppe/go-internal v1.8.0 // indirect
26+
github.com/sergi/go-diff v1.2.0 // indirect
1827
github.com/spf13/cobra v1.1.3
1928
github.com/spf13/viper v1.7.1
29+
github.com/xanzy/ssh-agent v0.3.1 // indirect
2030
github.com/xeipuuv/gojsonschema v1.2.0
21-
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
22-
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 // indirect
23-
golang.org/x/text v0.3.6 // indirect
31+
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e // indirect
32+
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
33+
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
34+
golang.org/x/text v0.3.7 // indirect
2435
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
2536
)

0 commit comments

Comments
 (0)