Skip to content

Commit

Permalink
hotfix: don't early exit with error if root is not a git dir (#224)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
srijan-deepsource authored Aug 28, 2023
1 parent 5104c0a commit 4edee8f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
33 changes: 13 additions & 20 deletions command/report/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions command/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strings"
"time"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 4edee8f

Please sign in to comment.