From 4edee8fe5e787f4756f74e962cbeb6e602bf7446 Mon Sep 17 00:00:00 2001 From: Srijan Saurav <68371686+srijan-deepsource@users.noreply.github.com> Date: Mon, 28 Aug 2023 16:30:50 +0530 Subject: [PATCH] hotfix: don't early exit with error if root is not a git dir (#224) * hotfix: don't early exit with error if root is not a git dir This broke reporting workflow in a case where artifact was being reported from a docker container. Previously, Fetching head manually was done in the end, after checking all env variables. In the last change, we were still doing an early exit when the git rev parse command threw an error, not allowing the flow to reach till extracting git commit from env variable set in docker container. This change restores the previous workflow. * move GIT_COMMIT_SHA env lookup to the top * don't make another env lookup call --- command/report/git.go | 33 +++++++++++++-------------------- command/report/report.go | 2 ++ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/command/report/git.go b/command/report/git.go index cb0c4e58..165c3705 100644 --- a/command/report/git.go +++ b/command/report/git.go @@ -2,12 +2,9 @@ package report import ( "bytes" - "fmt" "os" "os/exec" "strings" - - "github.com/getsentry/sentry-go" ) // gitGetHead accepts a git directory and returns head commit OID / error @@ -18,11 +15,21 @@ func gitGetHead(workspaceDir string) (headOID string, warning string, err error) return } - // get the top commit manually, using git command + // 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 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 injectedSHA, isManuallyInjectedSHA := os.LookupEnv("GIT_COMMIT_SHA"); isManuallyInjectedSHA { + return injectedSHA, "", nil + } + + // get the top commit manually, using git command. We will be using this if there's no env variable set for extracting commit. headOID, err = fetchHeadManually(workspaceDir) if err != nil { - fmt.Println(err) - sentry.CaptureException(err) return } @@ -38,20 +45,6 @@ func gitGetHead(workspaceDir string) (headOID string, warning string, err error) return } - // 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 there weren't any special cases. Return the manually found headOID. return } diff --git a/command/report/report.go b/command/report/report.go index bbba3add..23ac222f 100644 --- a/command/report/report.go +++ b/command/report/report.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "os" "strings" "time" @@ -182,6 +183,7 @@ func (opts *ReportOptions) Run() int { headCommitOID, warning, err := gitGetHead(currentDir) if err != nil { fmt.Fprintln(os.Stderr, "DeepSource | Error | Unable to get commit OID HEAD. Make sure you are running the CLI from a git repository") + log.Println(err) sentry.CaptureException(err) return 1 }