Skip to content

Commit

Permalink
Avoid pushing empty branches to remote (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalDelarea authored May 1, 2023
1 parent 3d89a12 commit c47b536
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions commands/createfixpullrequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (cfp *CreateFixPullRequestsCmd) openFixingPullRequest(impactedPackage, fixB
}

log.Info("Pushing branch:", fixBranchName, "...")
if err = cfp.gitManager.Push(false); err != nil {
if err = cfp.gitManager.Push(false, fixBranchName); err != nil {
return
}

Expand Down Expand Up @@ -257,7 +257,7 @@ func (cfp *CreateFixPullRequestsCmd) openAggregatedPullRequest(fixBranchName str
return
}
log.Info("Pushing branch:", fixBranchName, "...")
if err = cfp.gitManager.Push(true); err != nil {
if err = cfp.gitManager.Push(true, fixBranchName); err != nil {
return
}
if !exists {
Expand Down
4 changes: 2 additions & 2 deletions commands/scanandfixrepos.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func (saf *ScanAndFixRepositories) downloadAndRunScanAndFix(repository *utils.Fr

func (saf ScanAndFixRepositories) setCommitBuildStatus(client vcsclient.VcsClient, repoConfig *utils.FrogbotRepoConfig, state vcsclient.CommitStatus, commitHash, description string) error {
if err := client.SetCommitStatus(context.Background(), state, repoConfig.RepoOwner, repoConfig.RepoName, commitHash, utils.FrogbotCreatorName, description, utils.CommitStatusDetailsUrl); err != nil {
return fmt.Errorf("failed to mark last commit as scanned due to: %s", err.Error())
return fmt.Errorf("failed to mark last commit build status due to: %s", err.Error())
}
log.Info("Commit '%s' in repo '%s', has successfully marked as scanned", commitHash, repoConfig.RepoName)
log.Info("Commit'", commitHash, "'in repo '", repoConfig.RepoName, "', has successfully marked as scanned")
return nil
}

Expand Down
15 changes: 9 additions & 6 deletions commands/utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"errors"
"fmt"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
Expand All @@ -19,6 +20,8 @@ import (
"github.com/jfrog/jfrog-client-go/utils/log"
)

const refFormat = "refs/heads/%s:refs/heads/%[1]s"

type GitManager struct {
// repository represents a git repository as a .git dir.
repository *git.Repository
Expand Down Expand Up @@ -200,21 +203,21 @@ func (gm *GitManager) BranchExistsInRemote(branchName string) (bool, error) {
return false, nil
}

func (gm *GitManager) Push(force bool) error {
func (gm *GitManager) Push(force bool, branchName string) error {
if gm.dryRun {
// On dry run do not push to any remote
return nil
}
// Pushing to remote
err := gm.repository.Push(&git.PushOptions{
if err := gm.repository.Push(&git.PushOptions{
RemoteName: gm.remoteName,
Auth: gm.auth,
Force: force,
})
if err != nil {
err = fmt.Errorf("git push failed with error: %s", err.Error())
RefSpecs: []config.RefSpec{config.RefSpec(fmt.Sprintf(refFormat, branchName))},
}); err != nil {
return fmt.Errorf("git push failed with error: %s", err.Error())
}
return err
return nil
}

// IsClean returns true if all the files are in Unmodified status.
Expand Down

0 comments on commit c47b536

Please sign in to comment.