Skip to content

Commit

Permalink
Fix: push only two branches (elastic#47)
Browse files Browse the repository at this point in the history
* Fix: fetch and push only relevant branches

* Fix refs

* Fix: comment
  • Loading branch information
mtojek authored Aug 13, 2020
1 parent 84d2f0b commit 57d61bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func promoteCommandAction(cmd *cobra.Command, args []string) error {
}

// Push changes
err = promote.PushChanges(githubUser, repository)
err = promote.PushChanges(githubUser, repository, newSourceStage, newDestinationStage)
if err != nil {
return errors.Wrapf(err, "pushing changes failed")
}
Expand Down
15 changes: 12 additions & 3 deletions internal/promote/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ func CloneRepository(user, stage string) (*git.Repository, error) {

err = r.Fetch(&git.FetchOptions{
RemoteName: remoteName,
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:refs/heads/HEAD"},
RefSpecs: []config.RefSpec{
"HEAD:refs/heads/HEAD",
"refs/heads/snapshot:refs/heads/snapshot",
"refs/heads/staging:refs/heads/staging",
"refs/heads/production:refs/heads/production",
},
})
if err != nil {
return nil, errors.Wrap(err, "fetch remote branches failed")
Expand Down Expand Up @@ -370,15 +375,19 @@ func RemovePackages(r *git.Repository, sourceStage string, packages PackageVersi
return newSourceStage, nil
}

// PushChanges method pushes all branches to the remote repository.
func PushChanges(user string, r *git.Repository) error {
// PushChanges method pushes branches to the remote repository.
func PushChanges(user string, r *git.Repository, newSourceStage, newDestinationStage string) error {
authToken, err := github.AuthToken()
if err != nil {
return errors.Wrap(err, "reading auth token failed")
}

err = r.Push(&git.PushOptions{
RemoteName: user,
RefSpecs: []config.RefSpec{
config.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s", newSourceStage, newSourceStage)),
config.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s", newDestinationStage, newDestinationStage)),
},
Auth: &http.BasicAuth{
Username: user,
Password: authToken,
Expand Down

0 comments on commit 57d61bf

Please sign in to comment.