Skip to content

Commit

Permalink
Add ability to ignore specific directories (#24)
Browse files Browse the repository at this point in the history
* Add ability to ignore specific directories

* Use same slice for append

* Return to using go-git release versions

* Fix bug where merges were only performed once

* Clarify how we check for mergeable status

* Only check for no commits if len of errors greater than 0

* Increase number of retries and add error messages
  • Loading branch information
TheJokersThief authored Jun 1, 2023
1 parent b15450b commit aa3c09f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ This assumes that you block merge to mainline branches with branch protections l
"requires a PR", "required approvers: 1", "required status checks" and that they're
not handled on the honour system.

This just checks the GitHub [MergeableState](https://docs.github.com/en/graphql/reference/enums#mergeablestate)
This just checks the GitHub "mergeable state" is "clean" (terms determined by GitHub)
to see if the PR can be merged.

```bash
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/avast/retry-go/v4 v4.3.4
github.com/bradleyfalzon/ghinstallation/v2 v2.4.0
github.com/charmbracelet/lipgloss v0.7.1
github.com/go-git/go-git/v5 v5.6.2-0.20230515192228-4ff2213cc542
github.com/go-git/go-git/v5 v5.7.0
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-github/v52 v52.0.0
github.com/gosimple/slug v1.13.1
Expand All @@ -21,7 +21,7 @@ require (

require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230417170513-8ee5748c52b5 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
Expand Down Expand Up @@ -51,9 +51,9 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/skeema/knownhosts v1.1.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
Expand Down
44 changes: 8 additions & 36 deletions go.sum

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions pkg/actions/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ func (r *Replace) Run(log *logrus.Entry) error {
go r.findAndReplaceWorker(log, files, errChan)
}

matches, err := filepathx.Glob(r.BaseDir + "/" + r.Glob)
globPattern := r.BaseDir + "/" + r.Glob
matches, err := filepathx.Glob(globPattern)
if err != nil {
logrus.WithField("pattern", globPattern).Error("Error globbing file path: ", err)
return err
}
matches = r.removeBlacklistedDirectories(matches)
Expand Down Expand Up @@ -100,7 +102,7 @@ func (r *Replace) removeBlacklistedDirectories(matches []string) []string {
return newMatches
}

func (r *Replace) findAndReplaceWorker(log *logrus.Entry, files <-chan string, errors chan<- error) {
func (r *Replace) findAndReplaceWorker(log *logrus.Entry, files <-chan string, errChan chan<- error) {
for file := range files {
content, readErr := os.ReadFile(file)
if !strings.Contains(string(content), r.OldString) || readErr != nil {
Expand All @@ -111,14 +113,14 @@ func (r *Replace) findAndReplaceWorker(log *logrus.Entry, files <-chan string, e

f, err := os.Open(file)
if err != nil {
errors <- err
errChan <- errors.New("couldn't open file: " + err.Error())
continue
}

// create temp file
tmp, err := os.CreateTemp(os.TempDir(), "replace-*")
if err != nil {
errors <- err
errChan <- errors.New("couldn't create temporary file: " + err.Error())
continue
}

Expand All @@ -128,25 +130,25 @@ func (r *Replace) findAndReplaceWorker(log *logrus.Entry, files <-chan string, e

_, err = io.Copy(tmp, reader)
if err != nil {
errors <- err
errChan <- errors.New("couldn't copy file: " + err.Error())
continue
}

// make sure the tmp file was successfully written to
if err := tmp.Close(); err != nil {
errors <- err
errChan <- errors.New("couldn't close file: " + err.Error())
continue
}

// close the file we're reading from
if err := f.Close(); err != nil {
errors <- err
errChan <- err
continue
}

// overwrite the original file with the temp file
if err := os.Rename(tmp.Name(), file); err != nil {
errors <- err
errChan <- errors.New("couldn't rename file: " + err.Error())
continue
}
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/core/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ func (b *Banshee) MergeApproved() error {
}

for _, pr := range prList {
b.log.Debug("Checking if ", *pr.HTMLURL, " is mergeable")
if *pr.MergeableState == "mergeable" {
b.log.Info("Merging", *pr.HTMLURL)
b.log.WithField("mergeable", *pr.MergeableState).Debug("Checking if ", *pr.HTMLURL, " is mergeable")
if *pr.MergeableState == "clean" {
b.log.Info("Merging ", *pr.HTMLURL)
mergeErr := b.GithubClient.MergePullRequest(pr)
return mergeErr
if mergeErr != nil {
return mergeErr
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/core/migrate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -136,7 +137,7 @@ func (b *Banshee) handleRepo(log *logrus.Entry, org, repo string) (string, error
// if dirty, commit with action.Description as message
addErr := tree.AddGlob("./")
if addErr != nil {
return "", addErr
return "", errors.New("adding error: " + addErr.Error())
}

_, commitErr := tree.Commit(action.Description, &git.CommitOptions{
Expand Down
2 changes: 1 addition & 1 deletion pkg/github/pull_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (gc *GithubClient) CreatePullRequest(org, repo, title, body, base_branch, m
var errResponse *github.ErrorResponse
ghErr := errors.As(err, &errResponse)
if ghErr {
if strings.Contains(errResponse.Errors[0].Message, "No commits between") {
if len(errResponse.Errors) > 0 && strings.Contains(errResponse.Errors[0].Message, "No commits between") {
return "", nil
}
return "", err
Expand Down
4 changes: 2 additions & 2 deletions pkg/github/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var (
defaultRetryOptions = []retry.Option{
retry.Delay(5 * time.Second),
retry.MaxJitter(3 * time.Second),
retry.Attempts(3),
retry.MaxDelay(time.Second * 10),
retry.Attempts(10),
retry.MaxDelay(time.Second * 30),
retry.LastErrorOnly(true),
}
)
Expand Down

0 comments on commit aa3c09f

Please sign in to comment.