-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make a new command to trigger a pipeline
- Loading branch information
1 parent
75976b1
commit a5daeef
Showing
15 changed files
with
279 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package pipeline | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/api/rest" | ||
"github.com/CircleCI-Public/circleci-cli/git" | ||
) | ||
|
||
type pipeline struct { | ||
rc *rest.Client | ||
} | ||
|
||
func New(rc *rest.Client) *pipeline { | ||
return &pipeline{rc: rc} | ||
} | ||
|
||
type Pipeline struct { | ||
ID string `json:"id"` | ||
Number int `json:"number"` | ||
State string `json:"state"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
Trigger Trigger `json:"trigger"` | ||
} | ||
|
||
type Trigger struct { | ||
Type string `json:"type"` | ||
ReceivedAt time.Time `json:"received_at"` | ||
Actor Actor `json:"actor"` | ||
} | ||
|
||
type Actor struct { | ||
Login string `json:"login"` | ||
AvatarURL string `json:"avatar_url"` | ||
} | ||
|
||
func (p *pipeline) Get(remote git.Remote) ([]Pipeline, error) { | ||
req, err := p.rc.NewRequest("GET", pipelineSlug(remote), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp := struct { | ||
Items []Pipeline `json:"items"` | ||
}{} | ||
_, err = p.rc.DoRequest(req, &resp) | ||
return resp.Items, err | ||
} | ||
|
||
type TriggerParameters struct { | ||
Branch string `json:"branch,omitempty"` | ||
} | ||
|
||
func (p *pipeline) Trigger(remote git.Remote, params *TriggerParameters) (*Pipeline, error) { | ||
req, err := p.rc.NewRequest("POST", pipelineSlug(remote), params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp := &Pipeline{} | ||
_, err = p.rc.DoRequest(req, resp) | ||
return resp, err | ||
} | ||
|
||
func pipelineSlug(remote git.Remote) *url.URL { | ||
return &url.URL{Path: fmt.Sprintf("project/%s/%s/%s/pipeline", | ||
strings.ToLower(string(remote.VcsType)), | ||
url.QueryEscape(remote.Organization), | ||
url.QueryEscape(remote.Project))} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package pipeline | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/olekukonko/tablewriter" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/api/pipeline" | ||
"github.com/CircleCI-Public/circleci-cli/api/rest" | ||
"github.com/CircleCI-Public/circleci-cli/git" | ||
"github.com/CircleCI-Public/circleci-cli/settings" | ||
) | ||
|
||
func NewCommand(config *settings.Config, preRunE validator) *cobra.Command { | ||
p := pipeline.New(rest.New(config.Host, config.RestEndpoint, config.Token)) | ||
cmd := &cobra.Command{ | ||
Use: "pipeline", | ||
Short: "Operate on pipelines", | ||
PreRunE: preRunE, | ||
} | ||
|
||
cmd.AddCommand(&cobra.Command{ | ||
Use: "trigger", | ||
Short: "Trigger a pipeline for the current project", | ||
PreRunE: preRunE, | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
remote, err := git.InferProjectFromGitRemotes() | ||
if err != nil { | ||
return errors.Wrap(err, "this command must be run from inside a git repository") | ||
} | ||
|
||
fmt.Printf("Triggering pipeline for: VCS=%q organization=%q project=%q\n", remote.VcsType, remote.Organization, remote.Project) | ||
|
||
pipe, err := p.Trigger(*remote, &pipeline.TriggerParameters{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
table := newPipelineTable() | ||
defer table.Render() | ||
appendPipeline(table, *pipe) | ||
|
||
return nil | ||
}, | ||
}) | ||
|
||
cmd.AddCommand(&cobra.Command{ | ||
Use: "list", | ||
Aliases: []string{"ls"}, | ||
Short: "List all pipelines for the current project", | ||
PreRunE: preRunE, | ||
RunE: func(cmd *cobra.Command, _ []string) error { | ||
remote, err := git.InferProjectFromGitRemotes() | ||
if err != nil { | ||
return errors.Wrap(err, "this command must be run from inside a git repository") | ||
} | ||
|
||
pipes, err := p.Get(*remote) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
table := newPipelineTable() | ||
defer table.Render() | ||
for _, pipe := range pipes { | ||
appendPipeline(table, pipe) | ||
} | ||
|
||
return nil | ||
}, | ||
}) | ||
|
||
return cmd | ||
} | ||
|
||
func newPipelineTable() *tablewriter.Table { | ||
table := tablewriter.NewWriter(os.Stdout) | ||
table.SetHeader([]string{ | ||
"ID", | ||
"Number", | ||
"Created At", | ||
"Updated At", | ||
"State", | ||
"Trigger Type", | ||
"Actor Login", | ||
}) | ||
return table | ||
} | ||
|
||
func appendPipeline(table *tablewriter.Table, pipe pipeline.Pipeline) { | ||
table.Append([]string{ | ||
pipe.ID, | ||
strconv.Itoa(pipe.Number), | ||
pipe.CreatedAt.Format(time.RFC3339), | ||
pipe.UpdatedAt.Format(time.RFC3339), | ||
pipe.State, | ||
pipe.Trigger.Type, | ||
pipe.Trigger.Actor.Login, | ||
}) | ||
} | ||
|
||
type validator func(cmd *cobra.Command, args []string) error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Usage: | ||
pipeline [command] | ||
|
||
Available Commands: | ||
list List all pipelines for the current project | ||
trigger Trigger a pipeline for the current project | ||
|
||
Use "pipeline [command] --help" for more information about a command. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Usage: | ||
pipeline list | ||
|
||
Aliases: | ||
list, ls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Usage: | ||
pipeline trigger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package pipeline | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"gotest.tools/v3/golden" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/CircleCI-Public/circleci-cli/settings" | ||
) | ||
|
||
func TestUsage(t *testing.T) { | ||
preRunE := func(cmd *cobra.Command, args []string) error { return nil } | ||
cmd := NewCommand(&settings.Config{}, preRunE) | ||
testSubCommandUsage(t, cmd.Name(), cmd) | ||
} | ||
|
||
func testSubCommandUsage(t *testing.T, prefix string, parent *cobra.Command) { | ||
t.Helper() | ||
t.Run(parent.Name(), func(t *testing.T) { | ||
golden.Assert(t, parent.UsageString(), fmt.Sprintf("%s-expected-usage.txt", prefix)) | ||
for _, cmd := range parent.Commands() { | ||
testSubCommandUsage(t, fmt.Sprintf("%s/%s", prefix, cmd.Name()), cmd) | ||
} | ||
}) | ||
} |
Oops, something went wrong.