Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion cmd/gitsemver/gitsemver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,21 @@ var bumpCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
project := newProjectOrPanic(cmd)
versionFilenamesAndKeys := getVersionFilenamesAndKeysOrFail(cmd)
username := getUsernameOrFail(cmd)
password := getPasswordOrFail(cmd)
latest := getLatestVersionOrFail(project)
next := getNextVersionOrFail(project)

if err := project.Bump(versionFilenamesAndKeys); err != nil {
var auth gitsemver.AuthMethod = nil

if username != "" && password != "" {
auth = &gitsemver.BasicAuth{
Username: username,
Password: password,
}
}

if err := project.Bump(versionFilenamesAndKeys, auth); err != nil {
log.Fatalln(err)
}

Expand Down Expand Up @@ -65,6 +76,8 @@ func init() {
rootCmd.PersistentFlags().StringP("project", "p", "", "Project")

bumpCmd.Flags().StringArrayP("version-file", "f", make([]string, 0), "Specify version files to be updated with the new version in the format `filename:key` (i.e. `package.json:\"version\"`)")
bumpCmd.Flags().StringP("username", "u", "", "Username to use in HTTP basic authentication")
bumpCmd.Flags().StringP("password", "P", "", "Password to use in HTTP basic authentication")
}

func newProjectOrPanic(cmd *cobra.Command) *gitsemver.Project {
Expand Down Expand Up @@ -112,3 +125,21 @@ func getVersionFilenamesAndKeysOrFail(cmd *cobra.Command) []string {

return versionFilenamesAndKeys
}

func getUsernameOrFail(cmd *cobra.Command) string {
username, err := cmd.Flags().GetString("username")
if err != nil {
log.Fatalln(err)
}

return username
}

func getPasswordOrFail(cmd *cobra.Command) string {
password, err := cmd.Flags().GetString("password")
if err != nil {
log.Fatalln(err)
}

return password
}
10 changes: 5 additions & 5 deletions internal/git/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
)

type Commit = object.Commit
type Hash = plumbing.Hash
type Repository = git.Repository
type AuthMethod = transport.AuthMethod
type BasicAuth = http.BasicAuth

func OpenRepository(path string) (*Repository, error) {
return git.PlainOpen(path)
Expand Down Expand Up @@ -125,7 +128,7 @@ func CreateTag(repo *Repository, name, message string) error {
return err
}

func PushTagsToOrigin(repo *Repository) error {
func PushTagsToOrigin(repo *Repository, auth AuthMethod) error {
// skip pushing if remote doesn't exist
if _, err := repo.Remote("origin"); err != nil {
return nil
Expand All @@ -135,10 +138,7 @@ func PushTagsToOrigin(repo *Repository) error {
RemoteName: "origin",
Progress: os.Stdout,
RefSpecs: []config.RefSpec{config.RefSpec("refs/tags/*:refs/tags/*")},
Auth: &http.BasicAuth{
Username: "git",
Password: os.Getenv("GITHUB_TOKEN"),
},
Auth: auth,
}

if err := repo.Push(pushOpts); err != nil {
Expand Down
6 changes: 4 additions & 2 deletions pkg/gitsemver/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

type Increment int64
type Change string
type AuthMethod = git.AuthMethod
type BasicAuth = git.BasicAuth

const (
Major Increment = 4
Expand Down Expand Up @@ -215,7 +217,7 @@ func (p *Project) NextVersionIncrement() (Increment, error) {
return increment, nil
}

func (p *Project) Bump(versionFilenamesAndKeys []string) error {
func (p *Project) Bump(versionFilenamesAndKeys []string, auth AuthMethod) error {
latest, err := p.LatestVersion()
if err != nil {
return err
Expand Down Expand Up @@ -251,7 +253,7 @@ func (p *Project) Bump(versionFilenamesAndKeys []string) error {
return err
}

if err := git.PushTagsToOrigin(p.Repo()); err != nil {
if err := git.PushTagsToOrigin(p.Repo(), auth); err != nil {
return err
}

Expand Down