Skip to content

Commit

Permalink
Log git and github commands to stdout
Browse files Browse the repository at this point in the history
commit-id:4b22620c
  • Loading branch information
ejoffe committed Jun 15, 2021
1 parent 804d798 commit 8911bab
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
3 changes: 2 additions & 1 deletion cmd/amend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

"github.com/ejoffe/spr/config"
"github.com/ejoffe/spr/git/realgit"
"github.com/ejoffe/spr/spr"
"github.com/jessevdk/go-flags"
Expand All @@ -31,7 +32,7 @@ func main() {
os.Exit(0)
}

gitcmd := realgit.NewGitCmd()
gitcmd := realgit.NewGitCmd(&config.Config{})

// check that we are inside a git dir
var output string
Expand Down
5 changes: 3 additions & 2 deletions cmd/spr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func main() {
os.Exit(0)
}

gitcmd := realgit.NewGitCmd()

gitcmd := realgit.NewGitCmd(&config.Config{})
// check that we are inside a git dir
var output string
err = gitcmd.Git("status --porcelain", &output)
Expand All @@ -71,6 +70,8 @@ func main() {
rake.YamlFileWriter(configfilepath),
)

gitcmd = realgit.NewGitCmd(&cfg)

if opts.Debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
rake.LoadSources(&cfg, rake.DebugWriter(os.Stdout))
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Config struct {
RequireApproval bool `default:"true" yaml:"requireApproval"`
ShowPRLink bool `default:"true" yaml:"showPRLink"`
CleanupRemoteBranch bool `default:"true" yaml:"cleanupRemoteBranch"`
LogGitCommands bool `default:"false" yaml:"logGitCommands"`
LogGitHubCalls bool `default:"false" yaml:"logGitHubCalls"`
}

func ConfigFilePath(gitcmd git.GitInterface) string {
Expand Down
12 changes: 10 additions & 2 deletions git/realgit/realcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (
"os/exec"
"strings"

"github.com/ejoffe/spr/config"
"github.com/rs/zerolog/log"
)

func NewGitCmd() *gitcmd {
initcmd := &gitcmd{}
func NewGitCmd(cfg *config.Config) *gitcmd {
initcmd := &gitcmd{
config: cfg,
}
var rootdir string
err := initcmd.Git("rev-parse --show-toplevel", &rootdir)
if err != nil {
Expand All @@ -19,11 +22,13 @@ func NewGitCmd() *gitcmd {
rootdir = strings.TrimSpace(rootdir)

return &gitcmd{
config: cfg,
rootdir: rootdir,
}
}

type gitcmd struct {
config *config.Config
rootdir string
}

Expand All @@ -32,6 +37,9 @@ func (c *gitcmd) Git(argStr string, output *string) error {
// if output is not nil it will be set to the output of the command

log.Debug().Msg("git " + argStr)
if c.config.LogGitCommands {
fmt.Printf("> git %s\n", argStr)
}
args := strings.Split(argStr, " ")
cmd := exec.Command("git", args...)
cmd.Dir = c.rootdir
Expand Down
27 changes: 26 additions & 1 deletion github/githubclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type client struct {
var pullRequestRegex = regexp.MustCompile(`pr/[a-zA-Z0-9_\-]+/([a-zA-Z0-9_\-/]+)/([a-f0-9]{8})$`)

func (c *client) GetInfo(ctx context.Context, gitcmd git.GitInterface) *github.GitHubInfo {
if c.config.LogGitHubCalls {
fmt.Printf("> github fetch pull requests\n")
}
var query struct {
Viewer struct {
Login string
Expand Down Expand Up @@ -176,7 +179,7 @@ func (c *client) CreatePullRequest(ctx context.Context,
err := c.api.Mutate(ctx, &mutation, input, nil)
check(err)

return &github.PullRequest{
pr := &github.PullRequest{
ID: mutation.CreatePullRequest.PullRequest.ID,
Number: mutation.CreatePullRequest.PullRequest.Number,
FromBranch: headRefName,
Expand All @@ -190,11 +193,21 @@ func (c *client) CreatePullRequest(ctx context.Context,
Stacked: false,
},
}

if c.config.LogGitHubCalls {
fmt.Printf("> github create %d: %s\n", pr.Number, pr.Title)
}

return pr
}

func (c *client) UpdatePullRequest(ctx context.Context,
info *github.GitHubInfo, pr *github.PullRequest, commit git.Commit, prevCommit *git.Commit) {

if c.config.LogGitHubCalls {
fmt.Printf("> github update %d - %s\n", pr.Number, pr.Title)
}

baseRefName := "master"
if prevCommit != nil {
baseRefName = branchNameFromCommit(info, *prevCommit)
Expand Down Expand Up @@ -250,6 +263,10 @@ func (c *client) CommentPullRequest(ctx context.Context, pr *github.PullRequest,
Err(err).
Msg("pull request update failed")
}

if c.config.LogGitHubCalls {
fmt.Printf("> github add comment %d: %s\n", pr.Number, pr.Title)
}
}

func (c *client) MergePullRequest(ctx context.Context, pr *github.PullRequest) {
Expand Down Expand Up @@ -277,6 +294,10 @@ func (c *client) MergePullRequest(ctx context.Context, pr *github.PullRequest) {
Msg("pull request merge failed")
}
check(err)

if c.config.LogGitHubCalls {
fmt.Printf("> github merge %d: %s\n", pr.Number, pr.Title)
}
}

func (c *client) ClosePullRequest(ctx context.Context, pr *github.PullRequest) {
Expand All @@ -300,6 +321,10 @@ func (c *client) ClosePullRequest(ctx context.Context, pr *github.PullRequest) {
Err(err).
Msg("pull request close failed")
}

if c.config.LogGitHubCalls {
fmt.Printf("> github close %d: %s\n", pr.Number, pr.Title)
}
}

func branchNameFromCommit(info *github.GitHubInfo, commit git.Commit) string {
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,14 @@ These are the available configurations:
| requireApproval | bool | true | require pull request approval in order to merge |
| showPRLink | bool | true | show full pull request http link |
| cleanupRemoteBranch | bool | true | delete remote branch after pull request merges |
| logGitCommands | bool | false | logs all git commands to stdout |
| logGitHubCalls | bool | false | logs all github api calls to stdout |

Happy Coding!
-------------
If you find a bug, feel free to open an issue. Pull requests are welcome.

If you find this script as useful as I do, add and star and tell your fellow githubers.
If you find this script as useful as I do, add a star and tell your fellow githubers.

License
-------
Expand Down

0 comments on commit 8911bab

Please sign in to comment.