11package report
22
33import (
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
1113func 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}
0 commit comments