Skip to content

Commit 15919ae

Browse files
committed
Merged pull_request events should resolve to the target branch.
1 parent c090764 commit 15919ae

2 files changed

Lines changed: 52 additions & 13 deletions

File tree

internal/gitsemver/gitsemver.go

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package gitsemver
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"io"
8+
"os"
79
"strconv"
810
"strings"
911
)
@@ -172,26 +174,52 @@ func (vs *GitSemVer) GetTag(repo string) (tag string, match bool, err error) {
172174
return
173175
}
174176

177+
type gitHubPullRequestEvent struct {
178+
PullRequest struct {
179+
Merged bool `json:"merged"`
180+
} `json:"pull_request"`
181+
}
182+
183+
func (vs *GitSemVer) isMergedGitHubPullRequest() bool {
184+
eventName := strings.TrimSpace(vs.Env.Getenv("GITHUB_EVENT_NAME"))
185+
if eventName == "pull_request" || eventName == "pull_request_target" {
186+
if eventPath := strings.TrimSpace(vs.Env.Getenv("GITHUB_EVENT_PATH")); eventPath != "" {
187+
if b, err := os.ReadFile(eventPath); /* #nosec G304 */ err == nil {
188+
var event gitHubPullRequestEvent
189+
if err = json.Unmarshal(b, &event); err == nil {
190+
return event.PullRequest.Merged
191+
}
192+
}
193+
}
194+
}
195+
return false
196+
}
197+
175198
func (vs *GitSemVer) getBranchGitHub(repo string) (branchName string, err error) {
176199
// Pull request events expose the source branch as GITHUB_HEAD_REF.
177200
// Prefer this so PR builds are not mistaken for base-branch releases.
178-
if branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_HEAD_REF")); branchName == "" {
179-
if branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_REF_NAME")); branchName != "" {
180-
if strings.TrimSpace(vs.Env.Getenv("GITHUB_REF_TYPE")) == "tag" {
181-
var branches []string
182-
if branches, err = vs.Git.GetBranchesFromTag(repo, branchName); err == nil {
183-
for _, branchName = range branches {
184-
if vs.IsReleaseBranch(branchName) {
185-
return
186-
}
201+
if branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_HEAD_REF")); branchName != "" {
202+
// Merged pull_request events should resolve to the target branch.
203+
if vs.isMergedGitHubPullRequest() {
204+
branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_BASE_REF"))
205+
}
206+
return
207+
}
208+
if branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_REF_NAME")); branchName != "" {
209+
if strings.TrimSpace(vs.Env.Getenv("GITHUB_REF_TYPE")) == "tag" {
210+
var branches []string
211+
if branches, err = vs.Git.GetBranchesFromTag(repo, branchName); err == nil {
212+
for _, branchName = range branches {
213+
if vs.IsReleaseBranch(branchName) {
214+
return
187215
}
188216
}
189-
branchName = ""
190217
}
191-
} else {
192-
// Fallback for contexts that only expose a base branch.
193-
branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_BASE_REF"))
218+
branchName = ""
194219
}
220+
} else {
221+
// Fallback for contexts that only expose a base branch.
222+
branchName = strings.TrimSpace(vs.Env.Getenv("GITHUB_BASE_REF"))
195223
}
196224
return
197225
}

internal/gitsemver/gitsemver_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,20 @@ func Test_VersionStringer_GetBranch(t *testing.T) {
196196
name, err = vs.GetBranch(".")
197197
isEqual(t, err, nil)
198198
isEqual(t, "feature/foo", name)
199+
eventPath := filepath.Join(t.TempDir(), "event.json")
200+
if err = os.WriteFile(eventPath, []byte(`{"pull_request":{"merged":true}}`), 0o600); err != nil {
201+
t.Fatal(err)
202+
}
203+
env["GITHUB_EVENT_NAME"] = "pull_request"
204+
env["GITHUB_EVENT_PATH"] = eventPath
205+
name, err = vs.GetBranch(".")
206+
isEqual(t, err, nil)
207+
isEqual(t, "main", name)
199208
delete(env, "GITHUB_HEAD_REF")
200209
delete(env, "GITHUB_BASE_REF")
201210
delete(env, "GITHUB_REF_NAME")
211+
delete(env, "GITHUB_EVENT_NAME")
212+
delete(env, "GITHUB_EVENT_PATH")
202213
git.branch = ""
203214

204215
git.branch = "detached"

0 commit comments

Comments
 (0)