Skip to content

Commit

Permalink
environment list
Browse files Browse the repository at this point in the history
  • Loading branch information
3ximus committed Feb 18, 2024
1 parent b271c68 commit 83284a6
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 12 deletions.
29 changes: 24 additions & 5 deletions api/bb-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,26 +335,45 @@ func GetPipelineList(repository string, nResults int, targetBranch string) <-cha
query += fmt.Sprintf("&target.branch=%s", targetBranch)
}

var prevResponse BBPaginatedResponse[Pipeline]
var pipelineResponse BBPaginatedResponse[Pipeline]
response := bbApiGet(fmt.Sprintf("repositories/%s/pipelines?sort=-created_on&pagelen=%d%s", repository, nResults, query))
err := json.Unmarshal(response, &prevResponse)
err := json.Unmarshal(response, &pipelineResponse)
cobra.CheckErr(err)
for _, pipeline := range prevResponse.Values {
for _, pipeline := range pipelineResponse.Values {
channel <- pipeline
}
}()
return channel
}

func GetPipeline(repository string, id int) <-chan Pipeline {
func GetPipeline(repository string, id string) <-chan Pipeline {
channel := make(chan Pipeline)
go func() {
defer close(channel)
var pipeline Pipeline
response := bbApiGet(fmt.Sprintf("repositories/%s/pipelines/%d", repository, id))
response := bbApiGet(fmt.Sprintf("repositories/%s/pipelines/%s", repository, id))
err := json.Unmarshal(response, &pipeline)
cobra.CheckErr(err)
channel <- pipeline
}()
return channel
}

func GetEnvironmentList(repository string, status bool) <-chan Environment {
channel := make(chan Environment)
go func() {
defer close(channel)

var environmentResponse BBPaginatedResponse[Environment]
response := bbApiGet(fmt.Sprintf("repositories/%s/environments", repository))
err := json.Unmarshal(response, &environmentResponse)
cobra.CheckErr(err)
for _, env := range environmentResponse.Values {
if status {
env.Status = <-GetPipeline(repository, env.Lock.Triggerer.PipelineUUID)
}
channel <- env
}
}()
return channel
}
19 changes: 18 additions & 1 deletion api/bb-types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ type CommitStatus struct {
UpdatedOn time.Time `json:"updated_on"`
}

type Environment struct {
UUID string `json:"uuid"`
Name string
Category struct {
Name string
}
EnvironmentType struct {
Name string
} `json:"environment_type"`
Lock struct {
Triggerer struct {
PipelineUUID string `json:"pipeline_uuid"`
}
}
Status Pipeline
}

type Pipeline struct {
BuildNumber int `json:"build_number"`
State struct {
Expand All @@ -93,7 +110,7 @@ type Pipeline struct {
Links struct{ Html struct{ Href string } }
} `json:"pullrequest"`
}
Trigger struct{
Trigger struct {
Name string
}
Author User `json:"creator"`
Expand Down
29 changes: 29 additions & 0 deletions cmd/environment/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package environment

import (
"bb/util"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var EnvironmentCmd = &cobra.Command{
Use: "environment",
Aliases: []string{"env"},
Short: "Manage environments [env]",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
err := viper.BindPFlag("repo", cmd.Flags().Lookup("repo"))
cobra.CheckErr(err)
if curRepo := util.GetCurrentRepo(); curRepo != "" {
viper.SetDefault("repo", curRepo)
}
if !viper.IsSet("repo") {
cobra.CheckErr("repo is not defined")
}
},
}

func init() {
EnvironmentCmd.AddCommand(ListCmd)
EnvironmentCmd.PersistentFlags().StringP("repo", "R", "", "selected repository")
}
34 changes: 34 additions & 0 deletions cmd/environment/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package environment

import (
"bb/api"
"bb/util"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var ListCmd = &cobra.Command{
Use: "list",
Short: "List environments from a repository",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
status, _ := cmd.Flags().GetBool("status")
for environment := range api.GetEnvironmentList(viper.GetString("repo"), status) {
if status {
if environment.Status.State.Result.Name == "" {
fmt.Printf("%s ", util.FormatPipelineState(environment.Status.State.Name))
} else {
fmt.Printf("%s ", util.FormatPipelineState(environment.Status.State.Result.Name))
}
}
fmt.Printf("%s \033[37m%s\033[m", environment.Name, environment.EnvironmentType.Name)
fmt.Println()
}
},
}

func init() {
ListCmd.Flags().BoolP("status", "s", false, "show deployment status of each environment")
}
4 changes: 2 additions & 2 deletions cmd/pipeline/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ var ListCmd = &cobra.Command{
nResults, _ := cmd.Flags().GetInt("number-results")
showAuthor, _ := cmd.Flags().GetBool("author")
targetBranch, _ := cmd.Flags().GetString("target")
pipelineChannel := api.GetPipelineList(viper.GetString("repo"), nResults, targetBranch)
for pipeline := range pipelineChannel {

for pipeline := range api.GetPipelineList(viper.GetString("repo"), nResults, targetBranch) {
if pipeline.State.Result.Name == "" {
fmt.Printf("%s", util.FormatPipelineState(pipeline.State.Name))
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/pipeline/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var ViewCmd = &cobra.Command{
cobra.CheckErr(err)
}

pipeline := <-api.GetPipeline(repo, id)
pipeline := <-api.GetPipeline(repo, fmt.Sprintf("%d", id))

if pipeline.State.Result.Name == "" {
fmt.Printf("%s", util.FormatPipelineState(pipeline.State.Name))
Expand Down
4 changes: 1 addition & 3 deletions cmd/pr/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ var ListCmd = &cobra.Command{
status, _ := cmd.Flags().GetBool("status")
participants, _ := cmd.Flags().GetBool("participants")

prChannel := api.GetPrList(viper.GetString("repo"), states, author, search, source, destination, pages, status, participants)

count := 0
for pr := range prChannel {
for pr := range api.GetPrList(viper.GetString("repo"), states, author, search, source, destination, pages, status, participants) {
fmt.Printf("%s \033[1;32m#%d\033[m %s \033[1;34m[ %s \033[m→\033[1;34m %s ]\033[m \033[33m%s\033[m", util.FormatPrState(pr.State), pr.ID, pr.Title, pr.Source.Branch.Name, pr.Destination.Branch.Name, pr.Author.Nickname)
if status {
fmt.Printf(" %s", util.FormatPipelineState(pr.Status.State))
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bb/cmd/issue"
"bb/cmd/pipeline"
"bb/cmd/pr"
"bb/cmd/environment"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -36,6 +37,7 @@ func init() {

RootCmd.AddCommand(auth.AuthCmd)
RootCmd.AddCommand(pr.PrCmd)
RootCmd.AddCommand(environment.EnvironmentCmd)
RootCmd.AddCommand(issue.IssueCmd)
RootCmd.AddCommand(pipeline.PipelineCmd)
}
Expand Down

0 comments on commit 83284a6

Please sign in to comment.