Skip to content

Commit 844d785

Browse files
fix: only consider commits that are part of the build
1 parent 9383886 commit 844d785

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

cmd/semantic-release/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func main() {
9696
defaultBranch = "*"
9797
}
9898

99+
currentSha := condition.GetCurrentSHA()
100+
logger.Println("found current sha: " + currentSha)
101+
99102
if !*noci {
100103
logger.Println("running CI condition...")
101104
exitIfError(condition.Travis(*token, defaultBranch, isPrivate))
@@ -111,7 +114,7 @@ func main() {
111114
}
112115

113116
logger.Println("getting commits...")
114-
commits, err := repo.GetCommits(currentBranch)
117+
commits, err := repo.GetCommits(currentSha)
115118
exitIfError(err)
116119

117120
logger.Println("calculating new version...")
@@ -132,7 +135,7 @@ func main() {
132135
}
133136

134137
logger.Println("creating release...")
135-
exitIfError(repo.CreateRelease(changelog, newVer, currentBranch))
138+
exitIfError(repo.CreateRelease(changelog, newVer, currentBranch, currentSha))
136139

137140
if *ghr {
138141
exitIfError(ioutil.WriteFile(".ghr", []byte(fmt.Sprintf("-u %s -r %s v%s", repo.Owner, repo.Repo, newVer.String())), 0644))

condition/condition.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ func GetCurrentBranch() string {
2020
}
2121
return readGitHead()
2222
}
23+
24+
func GetCurrentSHA() string {
25+
if val := os.Getenv("TRAVIS_COMMIT"); val != "" {
26+
return val
27+
}
28+
// TODO: resolve ref
29+
return GetCurrentBranch()
30+
}

semrel.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ func parseCommit(commit *github.RepositoryCommit) *Commit {
9898
return c
9999
}
100100

101-
func (repo *Repository) GetCommits(branch string) ([]*Commit, error) {
101+
func (repo *Repository) GetCommits(sha string) ([]*Commit, error) {
102102
opts := &github.CommitsListOptions{
103-
SHA: branch,
103+
SHA: sha,
104104
ListOptions: github.ListOptions{PerPage: 100},
105105
}
106106
commits, _, err := repo.Client.Repositories.ListCommits(repo.Ctx, repo.Owner, repo.Repo, opts)
@@ -181,9 +181,22 @@ func (repo *Repository) GetLatestRelease(vrange string) (*Release, error) {
181181
return &Release{lastRelease.SHA, &npver}, nil
182182
}
183183

184-
func (repo *Repository) CreateRelease(changelog string, newVersion *semver.Version, branch string) error {
184+
func (repo *Repository) CreateRelease(changelog string, newVersion *semver.Version, branch, sha string) error {
185185
tag := fmt.Sprintf("v%s", newVersion.String())
186186
hasPrerelease := newVersion.Prerelease() != ""
187+
188+
if branch != sha {
189+
ref := "refs/tags/" + tag
190+
tagOpts := &github.Reference{
191+
Ref: &ref,
192+
Object: &github.GitObject{SHA: &sha},
193+
}
194+
_, _, err := repo.Client.Git.CreateRef(repo.Ctx, repo.Owner, repo.Repo, tagOpts)
195+
if err != nil {
196+
return err
197+
}
198+
}
199+
187200
opts := &github.RepositoryRelease{
188201
TagName: &tag,
189202
Name: &tag,

semrel_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ func githubHandler(w http.ResponseWriter, r *http.Request) {
6969
json.NewEncoder(w).Encode(GITHUB_TAGS)
7070
return
7171
}
72+
if r.Method == "POST" && r.URL.Path == "/repos/owner/test-repo/git/refs" {
73+
var data map[string]string
74+
json.NewDecoder(r.Body).Decode(&data)
75+
r.Body.Close()
76+
if data["sha"] != "deadbeef" || data["ref"] != "refs/tags/v2.0.0" {
77+
http.Error(w, "invalid sha or ref", http.StatusBadRequest)
78+
return
79+
}
80+
fmt.Fprint(w, "{}")
81+
return
82+
}
7283
if r.Method == "POST" && r.URL.Path == "/repos/owner/test-repo/releases" {
7384
var data map[string]string
7485
json.NewDecoder(r.Body).Decode(&data)
@@ -176,7 +187,7 @@ func TestCreateRelease(t *testing.T) {
176187
repo, ts := getNewTestRepo(t)
177188
defer ts.Close()
178189
newVersion, _ := semver.NewVersion("2.0.0")
179-
err := repo.CreateRelease("", newVersion, "")
190+
err := repo.CreateRelease("", newVersion, "", "deadbeef")
180191
if err != nil {
181192
t.Fatal(err)
182193
}

0 commit comments

Comments
 (0)