Skip to content

Commit

Permalink
feat(cmd): add .semrelrc support
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed May 24, 2017
1 parent 0085ad3 commit 9b409bf
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions cmd/semantic-release/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
Expand All @@ -11,6 +12,7 @@ import (
"io/ioutil"
"log"
"os"
"strings"
)

var SRVERSION string
Expand All @@ -24,6 +26,21 @@ func errorHandler(logger *log.Logger) func(error) {
}
}

type SemRelConfig struct {
MaintainedRange string `json:"maintainedRange"`
}

func loadConfig() *SemRelConfig {
f, err := os.OpenFile(".semrelrc", os.O_RDONLY, 0)
if err != nil {
return &SemRelConfig{}
}
src := &SemRelConfig{}
json.NewDecoder(f).Decode(src)
f.Close()
return src
}

func main() {
token := flag.String("token", os.Getenv("GITHUB_TOKEN"), "github token")
slug := flag.String("slug", os.Getenv("TRAVIS_REPO_SLUG"), "slug of the repository")
Expand Down Expand Up @@ -62,18 +79,38 @@ func main() {
exitIfError(err)
logger.Println("found default branch: " + defaultBranch)

currentBranch := condition.GetCurrentBranch()
if currentBranch == "" {
exitIfError(fmt.Errorf("current branch not found"))
}
logger.Println("found current branch: " + currentBranch)

config := loadConfig()
if config.MaintainedRange != "" && currentBranch == defaultBranch {
exitIfError(fmt.Errorf("maintained range not allowed on default branch"))
}

if config.MaintainedRange != "" {
logger.Println("found maintained range: " + config.MaintainedRange)
defaultBranch = "*"
}

if !*noci {
logger.Println("running CI condition...")
exitIfError(condition.Travis(*token, defaultBranch, isPrivate))
}

logger.Println("getting latest release...")
release, err := repo.GetLatestRelease("")
release, err := repo.GetLatestRelease(config.MaintainedRange)
exitIfError(err)
logger.Println("found version: " + release.Version.String())

if strings.Contains(config.MaintainedRange, "-") && release.Version.Prerelease() == "" {
exitIfError(fmt.Errorf("no prerelease for this version possible"))
}

logger.Println("getting commits...")
commits, err := repo.GetCommits()
commits, err := repo.GetCommits(currentBranch)
exitIfError(err)

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

logger.Println("creating release...")
exitIfError(repo.CreateRelease(commits, release, newVer))
exitIfError(repo.CreateRelease(commits, release, newVer, currentBranch))

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

0 comments on commit 9b409bf

Please sign in to comment.