Skip to content

Commit

Permalink
fix: always append hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn Yu authored and Shawn Yu committed May 14, 2024
1 parent 585190d commit 95c8c00
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 23 deletions.
5 changes: 2 additions & 3 deletions cmd/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func listRunsForWorkflow(workflowID int64) ([]*github.WorkflowRun, error) {
workflowRun, err := gh.New[github.WorkflowRuns]().
Arg("api").
Arg(fmt.Sprintf("/repos/{owner}/{repo}/actions/workflows/%d/runs?per_page=100&page=%d", workflowID, page)).
AppendHostName().
Exec()
if err != nil {
return nil, err
Expand All @@ -105,7 +104,8 @@ func findWorkflowByName(name string) (*github.Workflow, error) {
var repoWorkflowDefinitions []*github.Workflow
page := 1
for {
workflows, err := gh.New[github.Workflows]().Arg("api").Arg(fmt.Sprintf("/repos/{owner}/{repo}/actions/workflows?per_page=100&page=%d", page)).AppendHostName().Exec()
workflows, err := gh.New[github.Workflows]().Arg("api").Arg(fmt.Sprintf("/repos/{owner}/{repo}/actions/workflows?per_page=100&page=%d", page)).
Exec()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,7 +133,6 @@ func deleteWorkflowRun(workflowID int64) error {
Arg("--method").
Arg("DELETE").
ParseOutputJson(false).
AppendHostName().
Exec()

return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/dispatch/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var dispatchCmd = &cobra.Command{
}
}

s := utils.RandomSpinner("Looking for new workflow run\n")
s := utils.RandomSpinner("Looking for new workflow run")

newWorkflow, err := utils.TrackNewWorkflowRun(workflowName, func() {
_, err := gh.New[any]().
Expand Down
2 changes: 1 addition & 1 deletion cmd/force/force.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var forceCmd = &cobra.Command{
workflowName = *name
}

s := utils.RandomSpinner("Looking for new workflow run\n")
s := utils.RandomSpinner("Looking for new workflow run")

newWorkflow, err := utils.TrackNewWorkflowRun(workflowName, func() {
git.Commit([]string{"--amend", "--no-edit"})
Expand Down
2 changes: 1 addition & 1 deletion cmd/push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ If no workflow run as been started, this command will wait indefinite until a ne
workflowName = *name
}

s := utils.RandomSpinner("Looking for new workflow run\n")
s := utils.RandomSpinner("Looking for new workflow run")

newWorkflow, err := utils.TrackNewWorkflowRun(workflowName, func() {
git.Push(false)
Expand Down
14 changes: 1 addition & 13 deletions gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import (
type Cmd[T any] struct {
// Arguments passed to the command
args []string
// Append `--hostname` flag the gh cli command.
//
// Some commands such as `gh workflow run` does not support the `--hostname` flag. Default: false
appendHostName bool
// Parse the output of the gh command as json. Default: true
parseOutputJson bool
}
Expand All @@ -33,14 +29,6 @@ func (c *Cmd[T]) Arg(a string) *Cmd[T] {
return c
}

// AppendHostName appends the host name to the gh cli command if the hostname is configured
//
// Default: false
func (c *Cmd[T]) AppendHostName() *Cmd[T] {
c.appendHostName = true
return c
}

// ParseOutputJson toggles parsing the output as json. Defaults: true
func (c *Cmd[T]) ParseOutputJson(parse bool) *Cmd[T] {
c.parseOutputJson = parse
Expand All @@ -55,7 +43,7 @@ func (c *Cmd[T]) Exec() (output *T, err error) {
if err != nil {
return nil, err
}
if c.appendHostName && cfg.HostName != "" {
if cfg.HostName != "" {
c.args = append(c.args, "--hostname", cfg.HostName)
}

Expand Down
14 changes: 10 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (
//
// Will return an error if no workflow with `name` is found
func GetWorkflowRunByName(name string) (*github.WorkflowRun, error) {
workflowRuns, err := gh.New[github.WorkflowRuns]().Arg("api").Arg("/repos/{owner}/{repo}/actions/runs").AppendHostName().Exec()
workflowRuns, err := gh.New[github.WorkflowRuns]().
Arg("api").
Arg("/repos/{owner}/{repo}/actions/runs").
Exec()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -75,20 +78,23 @@ func OpenInBrowser(args []string) error {
// RandomSpinner creates a random spinner
func RandomSpinner(suffix string) *spinner.Spinner {
s := spinner.New(spinner.CharSets[rand.Intn(90)], 100*time.Millisecond)
s.Suffix = suffix
s.Suffix = suffix + "\n"
return s
}

// SelectRepoWorkflowName gets all defined workflows in the repo.
//
// If there are more than 1 workflow defined, prompt the user to select a workflow. Otherwise return the only workflow
// If there are more than 1 workflow defined, prompt the user to select a workflow. Otherwise, return the only workflow
// name in the repo
func SelectRepoWorkflowName() (workflowName *string, err error) {
// All defined workflows in the repo
var repoWorkflowDefinitions []*github.Workflow
page := 1
for {
workflows, err := gh.New[github.Workflows]().Arg("api").Arg(fmt.Sprintf("/repos/{owner}/{repo}/actions/workflows?per_page=100&page=%d", page)).AppendHostName().Exec()
workflows, err := gh.New[github.Workflows]().
Arg("api").
Arg(fmt.Sprintf("/repos/{owner}/{repo}/actions/workflows?per_page=100&page=%d", page)).
Exec()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 95c8c00

Please sign in to comment.