Skip to content

Commit

Permalink
added git-coauthor (#2539)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube authored Sep 22, 2024
1 parent 401c675 commit 20917b4
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
30 changes: 30 additions & 0 deletions completers/git-coauthor_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gh"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "git-coauthor",
Short: "Add a co-author to the last commit",
Long: "https://github.com/tj/git-extras/blob/master/Commands.md#git-coauthor",
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute() error {
return rootCmd.Execute()
}
func init() {
carapace.Gen(rootCmd).Standalone()

rootCmd.Flags().Bool("help", false, "show help")

carapace.Gen(rootCmd).PositionalCompletion(
gh.ActionUsers(gh.HostOpts{}),
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return gh.ActionUserEmails(gh.OwnerOpts{Owner: c.Args[0]})
}),
)
}
7 changes: 7 additions & 0 deletions completers/git-coauthor_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/carapace-sh/carapace-bin/completers/git-coauthor_completer/cmd"

func main() {
cmd.Execute()
}
46 changes: 46 additions & 0 deletions pkg/actions/tools/gh/email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package gh

import (
"fmt"

"github.com/carapace-sh/carapace"
)

type emailQuery struct {
Data struct {
User struct {
Name string
Email string
}
Organization struct {
Name string
Email string
}
}
}

// ActionUserEmails completes email for given user
func ActionUserEmails(opts OwnerOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var queryResult emailQuery
return graphQlAction(opts.repo(), fmt.Sprintf(`user(login: %#v) { name email }`, opts.Owner), &queryResult, func() carapace.Action {
if queryResult.Data.User.Email == "" {
queryResult.Data.User.Email = fmt.Sprintf(`%v@users.noreply.github.com`, opts.Owner)
}
return carapace.ActionValuesDescribed(queryResult.Data.User.Email, queryResult.Data.User.Email)
})
})
}

// ActionOrganizationEmails completes email for given organization
func ActionOrganizationEmails(opts OwnerOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var queryResult emailQuery
return graphQlAction(opts.repo(), fmt.Sprintf(`organization(login: %#v) { name email }`, opts.Owner), &queryResult, func() carapace.Action {
if queryResult.Data.Organization.Email == "" {
queryResult.Data.Organization.Email = fmt.Sprintf(`%v@organizations.noreply.github.com`, opts.Owner)
}
return carapace.ActionValuesDescribed(queryResult.Data.Organization.Email, queryResult.Data.Organization.Email)
})
})
}

0 comments on commit 20917b4

Please sign in to comment.