Skip to content

Commit

Permalink
chore: use go-git for git ops in test coverage workflow (#83)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
siddhant-deepsource and sourya-deepsource authored Dec 11, 2021
1 parent 03df826 commit 344a06d
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ build:
cd cmd/deepsource && GOOS=linux GOARCH=amd64 go build -tags static_all -o /tmp/deepsource .

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

test:
CGO_ENABLED=0 go test --cover -coverprofile=coverage.out -v ./command/report/tests/... -run TestReportKeyValueWorkflow
Expand Down
74 changes: 45 additions & 29 deletions command/report/git.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package report

import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"

"github.com/getsentry/sentry-go"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)

// gitGetHead accepts a git directory and returns head commit OID / error
func gitGetHead(workspaceDir string) (string, error) {
// TRAVIS CI
// Get USER env variable.
envUser := os.Getenv("USER")
if envUser == "travis" {
if envUser := os.Getenv("USER"); envUser == "travis" {
// Travis creates a merge commit for pull requests on forks.
// The head of commit is this merge commit, which does not match the commit of deepsource check.

// Fetch value of pull request SHA. If this is a PR, it will return SHA of HEAD commit of the PR, else "".
prSHA := os.Getenv("TRAVIS_PULL_REQUEST_SHA")

// If prSHA is not empty, that means we got an SHA, which is HEAD. Return this.
if len(prSHA) > 0 {
if prSHA := os.Getenv("TRAVIS_PULL_REQUEST_SHA"); len(prSHA) > 0 {
return prSHA, nil
}

}

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

// Check if the `GIT_COMMIT_SHA` environment variable exists. If yes, return this as
// the latest commit sha.
// This is used in cases when the user:
// 1. Is using a CI other than GH Actions/ Travis CI (handled above)
// 2. Wants to report tcv from inside a docker container in which they are running tests.
// In this scenario, the container doesn't have data about the latest git commit sha so
// it is injected by the user manually while running the container.
// Example:
// GIT_COMMIT_SHA=$(git --no-pager rev-parse HEAD | tr -d '\n')
// docker run -e DEEPSOURCE_DSN -e GIT_COMMIT_SHA ...
if _, isManuallyInjectedSHA := os.LookupEnv("GIT_COMMIT_SHA"); isManuallyInjectedSHA {
return os.Getenv("GIT_COMMIT_SHA"), nil
}

// If we are here, it means this is neither GitHub Action on default branch,
// nor a travis env with PR. Continue to fetch the headOID via the git command.
headOID := ""

cmd := exec.Command("git", "--no-pager", "rev-parse", "HEAD")
cmd.Dir = workspaceDir

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

err := cmd.Run()

outStr, _ := stdout.String(), stderr.String()

headOID, err := fetchHeadManually(workspaceDir)
if err != nil {
return headOID, err
fmt.Println(err)
sentry.CaptureException(err)
return "", err
}
return headOID, nil
}

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

return headOID, nil
// Resolve revision into a sha1 commit
commitHash, err := repo.ResolveRevision(plumbing.Revision("HEAD"))
if err != nil {
return "", err
}
return commitHash.String(), nil
}
17 changes: 14 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,32 @@ go 1.16
require (
github.com/AlecAivazis/survey/v2 v2.2.12
github.com/Jeffail/gabs/v2 v2.6.1
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20211112122917-428f8eabeeb3 // indirect
github.com/cli/browser v1.1.0
github.com/deepsourcelabs/graphql v0.2.2
github.com/fatih/color v1.12.0
github.com/getsentry/sentry-go v0.6.0
github.com/go-git/go-git v4.7.0+incompatible
github.com/go-git/go-git-fixtures/v4 v4.2.2 // indirect
github.com/go-git/go-git/v5 v5.4.2
github.com/google/go-cmp v0.5.5 // indirect
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/matryer/is v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.9.2
github.com/pterm/pterm v0.12.23
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
github.com/xanzy/ssh-agent v0.3.1 // indirect
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
Loading

0 comments on commit 344a06d

Please sign in to comment.