Skip to content

Commit

Permalink
gh: added body link completion
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Apr 13, 2022
1 parent c8dbdad commit 20a7337
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 0 deletions.
42 changes: 42 additions & 0 deletions completers/gh_completer/cmd/action/body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package action

import (
"fmt"
"regexp"
"strings"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

func ActionBodyLinks(cmd *cobra.Command) carapace.Action {
return carapace.ActionMultiParts(" ", func(c carapace.Context) carapace.Action {
keywords := []string{"close", "closes", "closed", "fix", "fixes", "fixed", "resolve", "resolves", "resolved"} // official keywords
keywords = append(keywords, "causes", "caused-by", "related") // additional keywords

re := regexp.MustCompile(fmt.Sprintf("^(%v)$", strings.Join(keywords, "|")))
if last := len(c.Parts) - 1; last < 0 || !re.MatchString(c.Parts[last]) {
return carapace.ActionValues(keywords...).Invoke(c).Suffix(" #").ToA()
}

return carapace.ActionMultiParts("#", func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
if strings.Contains(c.CallbackValue, "/") {
return ActionOwnerRepositories(cmd).Invoke(c).Suffix("#").ToA()
}
return ActionOwnerRepositories(cmd)
case 1:
fakeCmd := cobra.Command{}
fakeCmd.Flags().String("repo", c.Parts[0], "")
fakeCmd.Flag("repo").Changed = true
return carapace.Batch(
ActionIssues(&fakeCmd, IssueOpts{Open: true, Closed: true}),
ActionPullRequests(&fakeCmd, PullRequestOpts{Open: true, Closed: true, Merged: true}),
).ToA()
default:
return carapace.ActionValues()
}
})
})
}
1 change: 1 addition & 0 deletions completers/gh_completer/cmd/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func init() {
issueCmd.AddCommand(issue_commentCmd)

carapace.Gen(issue_commentCmd).FlagCompletion(carapace.ActionMap{
"body": action.ActionBodyLinks(issue_commentCmd),
"body-file": carapace.ActionFiles(),
})
carapace.Gen(issue_commentCmd).PositionalCompletion(
Expand Down
1 change: 1 addition & 0 deletions completers/gh_completer/cmd/issue_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func init() {
"assignee": carapace.ActionMultiParts(",", func(c carapace.Context) carapace.Action {
return action.ActionAssignableUsers(issue_createCmd).Invoke(c).Filter(c.Parts).ToA()
}),
"body": action.ActionBodyLinks(issue_createCmd),
"label": carapace.ActionMultiParts(",", func(c carapace.Context) carapace.Action {
return action.ActionLabels(issue_createCmd).Invoke(c).Filter(c.Parts).ToA()
}),
Expand Down
1 change: 1 addition & 0 deletions completers/gh_completer/cmd/issue_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func init() {
return action.ActionLabels(issue_editCmd).Invoke(c).Filter(c.Parts).ToA()
}),
"add-project": action.ActionProjects(issue_editCmd, action.ProjectOpts{Open: true}),
"body": action.ActionBodyLinks(issue_editCmd),
"body-file": carapace.ActionFiles(),
"milestone": action.ActionMilestones(issue_editCmd),
"remove-assignee": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
Expand Down
1 change: 1 addition & 0 deletions completers/gh_completer/cmd/pr_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func init() {
prCmd.AddCommand(pr_commentCmd)

carapace.Gen(pr_commentCmd).FlagCompletion(carapace.ActionMap{
"body": action.ActionBodyLinks(pr_commentCmd),
"body-file": carapace.ActionFiles(),
})

Expand Down
1 change: 1 addition & 0 deletions completers/gh_completer/cmd/pr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func init() {
return action.ActionAssignableUsers(pr_createCmd).Invoke(c).Filter(c.Parts).ToA()
}),
"base": action.ActionBranches(pr_createCmd),
"body": action.ActionBodyLinks(pr_createCmd),
"head": action.ActionBranches(pr_createCmd),
"label": carapace.ActionMultiParts(",", func(c carapace.Context) carapace.Action {
return action.ActionLabels(pr_createCmd).Invoke(c).Filter(c.Parts).ToA()
Expand Down
1 change: 1 addition & 0 deletions completers/gh_completer/cmd/pr_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func init() {
return action.ActionAssignableUsers(pr_editCmd).Invoke(c).Filter(c.Parts).ToA()
}),
"base": action.ActionBranches(pr_editCmd),
"body": action.ActionBodyLinks(pr_editCmd),
"body-file": carapace.ActionFiles(),
"milestone": action.ActionMilestones(pr_editCmd),
"remove-assignee": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
Expand Down
1 change: 1 addition & 0 deletions completers/gh_completer/cmd/pr_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func init() {
prCmd.AddCommand(pr_mergeCmd)

carapace.Gen(pr_mergeCmd).FlagCompletion(carapace.ActionMap{
"body": action.ActionBodyLinks(pr_mergeCmd),
"body-file": carapace.ActionFiles(),
})

Expand Down
1 change: 1 addition & 0 deletions completers/gh_completer/cmd/pr_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func init() {
prCmd.AddCommand(pr_reviewCmd)

carapace.Gen(pr_reviewCmd).FlagCompletion(carapace.ActionMap{
"body": action.ActionBodyLinks(pr_reviewCmd),
"body-file": carapace.ActionFiles(),
})

Expand Down

0 comments on commit 20a7337

Please sign in to comment.