Skip to content

Commit 9b409bf

Browse files
feat(cmd): add .semrelrc support
1 parent 0085ad3 commit 9b409bf

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

cmd/semantic-release/main.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"flag"
78
"fmt"
@@ -11,6 +12,7 @@ import (
1112
"io/ioutil"
1213
"log"
1314
"os"
15+
"strings"
1416
)
1517

1618
var SRVERSION string
@@ -24,6 +26,21 @@ func errorHandler(logger *log.Logger) func(error) {
2426
}
2527
}
2628

29+
type SemRelConfig struct {
30+
MaintainedRange string `json:"maintainedRange"`
31+
}
32+
33+
func loadConfig() *SemRelConfig {
34+
f, err := os.OpenFile(".semrelrc", os.O_RDONLY, 0)
35+
if err != nil {
36+
return &SemRelConfig{}
37+
}
38+
src := &SemRelConfig{}
39+
json.NewDecoder(f).Decode(src)
40+
f.Close()
41+
return src
42+
}
43+
2744
func main() {
2845
token := flag.String("token", os.Getenv("GITHUB_TOKEN"), "github token")
2946
slug := flag.String("slug", os.Getenv("TRAVIS_REPO_SLUG"), "slug of the repository")
@@ -62,18 +79,38 @@ func main() {
6279
exitIfError(err)
6380
logger.Println("found default branch: " + defaultBranch)
6481

82+
currentBranch := condition.GetCurrentBranch()
83+
if currentBranch == "" {
84+
exitIfError(fmt.Errorf("current branch not found"))
85+
}
86+
logger.Println("found current branch: " + currentBranch)
87+
88+
config := loadConfig()
89+
if config.MaintainedRange != "" && currentBranch == defaultBranch {
90+
exitIfError(fmt.Errorf("maintained range not allowed on default branch"))
91+
}
92+
93+
if config.MaintainedRange != "" {
94+
logger.Println("found maintained range: " + config.MaintainedRange)
95+
defaultBranch = "*"
96+
}
97+
6598
if !*noci {
6699
logger.Println("running CI condition...")
67100
exitIfError(condition.Travis(*token, defaultBranch, isPrivate))
68101
}
69102

70103
logger.Println("getting latest release...")
71-
release, err := repo.GetLatestRelease("")
104+
release, err := repo.GetLatestRelease(config.MaintainedRange)
72105
exitIfError(err)
73106
logger.Println("found version: " + release.Version.String())
74107

108+
if strings.Contains(config.MaintainedRange, "-") && release.Version.Prerelease() == "" {
109+
exitIfError(fmt.Errorf("no prerelease for this version possible"))
110+
}
111+
75112
logger.Println("getting commits...")
76-
commits, err := repo.GetCommits()
113+
commits, err := repo.GetCommits(currentBranch)
77114
exitIfError(err)
78115

79116
logger.Println("calculating new version...")
@@ -88,7 +125,7 @@ func main() {
88125
}
89126

90127
logger.Println("creating release...")
91-
exitIfError(repo.CreateRelease(commits, release, newVer))
128+
exitIfError(repo.CreateRelease(commits, release, newVer, currentBranch))
92129

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

0 commit comments

Comments
 (0)