Skip to content

Commit

Permalink
Merge pull request #2334 from carapace-sh/gh-enterprise
Browse files Browse the repository at this point in the history
gh: enterprise - support custom host
  • Loading branch information
rsteube authored Apr 4, 2024
2 parents e259ada + 919fd0a commit c7a70c4
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 56 deletions.
31 changes: 24 additions & 7 deletions completers/gh_completer/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"

"github.com/carapace-sh/carapace"
Expand Down Expand Up @@ -34,10 +33,18 @@ func ActionConfigHosts() carapace.Action {

func ApiV3Action(cmd *cobra.Command, query string, v interface{}, transform func() carapace.Action) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if repo, err := repoOverride(cmd); err != nil {
if repo, err := repoOverride(cmd, c); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return carapace.ActionExecCommand("gh", "api", "--hostname", repo.RepoHost(), "--preview", "mercy", query)(func(output []byte) carapace.Action {
args := []string{
"api",
"--preview", "mercy",
query,
}
if repo.RepoHost() != "" {
args = append(args, "--hostname", repo.RepoHost())
}
return carapace.ActionExecCommand("gh", args...)(func(output []byte) carapace.Action {
if err := json.Unmarshal(output, &v); err != nil {
return carapace.ActionMessage("failed to unmarshall response: " + err.Error())
}
Expand All @@ -61,10 +68,20 @@ func GraphQlAction(cmd *cobra.Command, query string, v interface{}, transform fu
queryParams = "(" + queryParams + ")"
}

if repo, err := repoOverride(cmd); err != nil {
if repo, err := repoOverride(cmd, c); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return carapace.ActionExecCommand(ghExecutable(), "api", "--hostname", repo.RepoHost(), "--header", "Accept: application/vnd.github.merge-info-preview+json", "graphql", "-F", "owner="+repo.RepoOwner(), "-F", "repo="+repo.RepoName(), "-f", fmt.Sprintf("query=query%v {%v}", queryParams, query))(func(output []byte) carapace.Action {
args := []string{
"api", "graphql",
"--header", "Accept: application/vnd.github.merge-info-preview+json",
"-F", "owner=" + repo.RepoOwner(),
"-F", "repo=" + repo.RepoName(),
"-f", fmt.Sprintf("query=query%v {%v}", queryParams, query),
}
if repo.RepoHost() != "" {
args = append(args, "--hostname", repo.RepoHost())
}
return carapace.ActionExecCommand(ghExecutable(), args...)(func(output []byte) carapace.Action {
if err := json.Unmarshal(output, &v); err != nil {
return carapace.ActionMessage("failed to unmarshall response: " + err.Error())
}
Expand All @@ -74,12 +91,12 @@ func GraphQlAction(cmd *cobra.Command, query string, v interface{}, transform fu
})
}

func repoOverride(cmd *cobra.Command) (ghrepo.Interface, error) {
func repoOverride(cmd *cobra.Command, c carapace.Context) (ghrepo.Interface, error) {
repoOverride := ""
if flag := cmd.Flag("repo"); flag != nil {
repoOverride = flag.Value.String()
}
if repoFromEnv := os.Getenv("GH_REPO"); repoOverride == "" && repoFromEnv != "" {
if repoFromEnv := c.Getenv("GH_REPO"); repoOverride == "" && repoFromEnv != "" {
repoOverride = repoFromEnv
}
if repoOverride != "" {
Expand Down
4 changes: 2 additions & 2 deletions completers/gh_completer/cmd/action/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type artifactQuery struct {

func ActionWorkflowArtifactNames(cmd *cobra.Command, runId string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand All @@ -43,7 +43,7 @@ func ActionWorkflowArtifactNames(cmd *cobra.Command, runId string) carapace.Acti

func ActionWorkflowArtifactIds(cmd *cobra.Command, runId string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions completers/gh_completer/cmd/action/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func ActionCaches(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand All @@ -17,7 +17,7 @@ func ActionCaches(cmd *cobra.Command) carapace.Action {
}
func ActionCacheRefs(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func ActionContents(cmd *cobra.Command, path string, branch string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type environmentQuery struct {

func ActionEnvironments(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func ActionIssueFields() carapace.Action {

func actionIssue(cmd *cobra.Command, issueId string, f func(i issue) carapace.Action) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type jobQuery struct {

func ActionWorkflowJobs(cmd *cobra.Command, runId string, opts RunOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type deployKey struct {

func ActionDeployKeys(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type label struct {

func ActionLabels(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions completers/gh_completer/cmd/action/pullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func ActionPullRequestFields() carapace.Action {

func actionPullRequests(cmd *cobra.Command, id string, f func(p pullrequest) carapace.Action) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down Expand Up @@ -165,7 +165,7 @@ type commit struct {

func ActionPullRequestCommits(cmd *cobra.Command, id string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func ActionRulesets(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type runQuery struct {

func ActionWorkflowRuns(cmd *cobra.Command, opts RunOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func ActionTeams(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions completers/gh_completer/cmd/action/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func ActionIssueTemplates(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand All @@ -18,7 +18,7 @@ func ActionIssueTemplates(cmd *cobra.Command) carapace.Action {

func ActionPullRequestTemplates(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type repoTopicsQuery struct {

func ActionRepoTopics(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func ActionVariables(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/action/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type workFlowQuery struct {

func ActionWorkflows(cmd *cobra.Command, opts WorkflowOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
repo, err := repoOverride(cmd, c)
if err != nil {
return carapace.ActionMessage(err.Error())
}
Expand Down
11 changes: 11 additions & 0 deletions completers/gh_completer/cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/carapace-sh/carapace-bin/completers/gh_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/net/http"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var apiCmd = &cobra.Command{
Expand Down Expand Up @@ -48,4 +49,14 @@ func init() {
).Invoke(c).Merge().ToA()
}),
)

carapace.Gen(apiCmd).PreInvoke(func(cmd *cobra.Command, _ *pflag.Flag, action carapace.Action) carapace.Action {
if f := cmd.Flag("hostname"); f.Changed {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
c.Setenv("GH_HOST", f.Value.String()) // TODO provide Action.SetEnv or similar in carapace
return action.Invoke(c).ToA()
})
}
return action
})
}
1 change: 1 addition & 0 deletions pkg/actions/env/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func init() {
"GH_PATH": "set the path to the gh executable",
},
VariableCompletion: map[string]carapace.Action{
"GH_HOST": gh.ActionConfigHosts(),
"GH_REPO": gh.ActionOwnerRepositories(gh.HostOpts{}),
"GH_EDITOR": bridge.ActionCarapaceBin().Split(),
"GH_BROWSER": bridge.ActionCarapaceBin().Split(),
Expand Down
12 changes: 9 additions & 3 deletions pkg/actions/tools/gh/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import (

func apiV3Action(opts RepoOpts, query string, v interface{}, transform func() carapace.Action) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if opts.Host == "" {
opts.Host = "github.com"
args := []string{
"api",
"--preview", "mercy",
query,
}
return carapace.ActionExecCommand("gh", "api", "--hostname", opts.Host, "--preview", "mercy", query)(func(output []byte) carapace.Action {
if opts.Host != "" {
args = append(args, "--hostname", opts.Host)
}

return carapace.ActionExecCommand("gh", args...)(func(output []byte) carapace.Action {
if err := json.Unmarshal(output, &v); err != nil {
return carapace.ActionMessage("failed to unmarshall response: " + err.Error())
}
Expand Down
59 changes: 33 additions & 26 deletions pkg/actions/tools/gh/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,43 @@ import (
)

func graphQlAction(opts RepoOpts, query string, v interface{}, transform func() carapace.Action) carapace.Action {
return actionHostConfig(func(config hostConfig) carapace.Action {
params := make([]string, 0)
if strings.Contains(query, "$owner") {
params = append(params, "$owner: String!")
}
if strings.Contains(query, "$repo") {
params = append(params, "$repo: String!")
}
queryParams := strings.Join(params, ",")
if queryParams != "" {
queryParams = "(" + queryParams + ")"
}

if opts.Host == "" {
opts.Host = "github.com"
}
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return actionHostConfig(func(config hostConfig) carapace.Action {
params := make([]string, 0)
if strings.Contains(query, "$owner") {
params = append(params, "$owner: String!")
}
if strings.Contains(query, "$repo") {
params = append(params, "$repo: String!")
}
queryParams := strings.Join(params, ",")
if queryParams != "" {
queryParams = "(" + queryParams + ")"
}

if opts.Owner == "@me" {
conf, ok := config[opts.Host]
if !ok {
return carapace.ActionMessage("unknown host")
if opts.Owner == "@me" {
conf, ok := config[opts.Host]
if !ok {
return carapace.ActionMessage("unknown host")
}
opts.Owner = conf.User
}
opts.Owner = conf.User
}

return carapace.ActionExecCommand("gh", "api", "--hostname", opts.Host, "--header", "Accept: application/vnd.github.merge-info-preview+json", "graphql", "-F", "owner="+opts.Owner, "-F", "repo="+opts.Name, "-f", fmt.Sprintf("query=query%v {%v}", queryParams, query))(func(output []byte) carapace.Action {
if err := json.Unmarshal(output, &v); err != nil {
return carapace.ActionMessage("failed to unmarshall response: " + err.Error())
args := []string{"api", "graphql",
"--header", "Accept: application/vnd.github.merge-info-preview+json",
"-F", "owner=" + opts.Owner,
"-F", "repo=" + opts.Name,
"-f", fmt.Sprintf("query=query%v {%v}", queryParams, query),
}
if opts.Host != "" {
args = append(args, "--hostname", opts.Host)
}
return transform()
return carapace.ActionExecCommand("gh", args...)(func(output []byte) carapace.Action {
if err := json.Unmarshal(output, &v); err != nil {
return carapace.ActionMessage("failed to unmarshall response: " + err.Error())
}
return transform()
})
})
})
}

0 comments on commit c7a70c4

Please sign in to comment.