Skip to content

Commit 4edee8f

Browse files
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
1 parent 5104c0a commit 4edee8f

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

command/report/git.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ package report
22

33
import (
44
"bytes"
5-
"fmt"
65
"os"
76
"os/exec"
87
"strings"
9-
10-
"github.com/getsentry/sentry-go"
118
)
129

1310
// 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)
1815
return
1916
}
2017

21-
// get the top commit manually, using git command
18+
// Check if the `GIT_COMMIT_SHA` environment variable exists. If yes, return this as
19+
// the latest commit sha.
20+
// This is used in cases when the user wants to report tcv from inside a docker container in which they are running tests.
21+
// In this scenario, the container doesn't have data about the latest git commit sha so
22+
// it is injected by the user manually while running the container.
23+
// Example:
24+
// GIT_COMMIT_SHA=$(git --no-pager rev-parse HEAD | tr -d '\n')
25+
// docker run -e DEEPSOURCE_DSN -e GIT_COMMIT_SHA ...
26+
if injectedSHA, isManuallyInjectedSHA := os.LookupEnv("GIT_COMMIT_SHA"); isManuallyInjectedSHA {
27+
return injectedSHA, "", nil
28+
}
29+
30+
// get the top commit manually, using git command. We will be using this if there's no env variable set for extracting commit.
2231
headOID, err = fetchHeadManually(workspaceDir)
2332
if err != nil {
24-
fmt.Println(err)
25-
sentry.CaptureException(err)
2633
return
2734
}
2835

@@ -38,20 +45,6 @@ func gitGetHead(workspaceDir string) (headOID string, warning string, err error)
3845
return
3946
}
4047

41-
// Check if the `GIT_COMMIT_SHA` environment variable exists. If yes, return this as
42-
// the latest commit sha.
43-
// This is used in cases when the user:
44-
// 1. Is using a CI other than GH Actions/ Travis CI (handled above)
45-
// 2. Wants to report tcv from inside a docker container in which they are running tests.
46-
// In this scenario, the container doesn't have data about the latest git commit sha so
47-
// it is injected by the user manually while running the container.
48-
// Example:
49-
// GIT_COMMIT_SHA=$(git --no-pager rev-parse HEAD | tr -d '\n')
50-
// docker run -e DEEPSOURCE_DSN -e GIT_COMMIT_SHA ...
51-
if _, isManuallyInjectedSHA := os.LookupEnv("GIT_COMMIT_SHA"); isManuallyInjectedSHA {
52-
return os.Getenv("GIT_COMMIT_SHA"), "", nil
53-
}
54-
5548
// If we are here, it means there weren't any special cases. Return the manually found headOID.
5649
return
5750
}

command/report/report.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"log"
89
"os"
910
"strings"
1011
"time"
@@ -182,6 +183,7 @@ func (opts *ReportOptions) Run() int {
182183
headCommitOID, warning, err := gitGetHead(currentDir)
183184
if err != nil {
184185
fmt.Fprintln(os.Stderr, "DeepSource | Error | Unable to get commit OID HEAD. Make sure you are running the CLI from a git repository")
186+
log.Println(err)
185187
sentry.CaptureException(err)
186188
return 1
187189
}

0 commit comments

Comments
 (0)