Skip to content

Commit f5831b1

Browse files
sourya-deepsourcemohi7solanki
authored andcommitted
[Fix #21] Fix commit sha in case of github actions PR
Signed-off-by: Sourya Vatsyayan <sourya@deepsource.io>
1 parent ccb583e commit f5831b1

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

git.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ func gitGetHead(workspaceDir string) (string, error) {
2727

2828
// Check if it is a GitHub Action Environment, If it is then get
2929
// the HEAD from `GITHUB_SHA` environment
30-
_, isGitHubEnv := os.LookupEnv("GITHUB_ACTIONS")
31-
32-
if isGitHubEnv {
33-
return os.Getenv("GITHUB_SHA"), nil
30+
if _, isGitHubEnv := os.LookupEnv("GITHUB_ACTIONS"); isGitHubEnv {
31+
// When GITHUB_REF is not set, GITHUB_SHA points to original commit.
32+
// When set, it points to the "latest *merge* commit in the branch".
33+
// Ref: https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_request
34+
if _, isBranchCommit := os.LookupEnv("GITHUB_REF"); !isBranchCommit {
35+
return os.Getenv("GITHUB_SHA"), nil
36+
}
3437
}
3538

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

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

0 commit comments

Comments
 (0)