Skip to content

Commit

Permalink
adds last successfull build stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ericrenard committed Nov 1, 2019
1 parent 23ebf79 commit a142c2a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
45 changes: 45 additions & 0 deletions cmd/last_successful_build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"fmt"
"log"

"github.com/molotovtv/tc/tc"
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(buildsCmd)
}

var buildsCmd = &cobra.Command{
Use: "last-build-success",
Short: "Get successful builds by branch",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Printf("builds-success [env] [branch]\n")
return
}
env := args[0]
branch := args[1]
fmt.Printf("Env:%s ; Branch:%s\n", env, branch)

c, err := tc.LoadConfig()
if err != nil {
log.Fatal(err)
}

buildTypeID, err := c.BuildTypeID(env)
if err != nil {
log.Fatal(err)
}

res, err := tc.LastBuildSuccessByBranch(c, buildTypeID, branch)
if err != nil {
log.Fatal(err)
}

fmt.Printf("%+v\n", res)
},
}
12 changes: 8 additions & 4 deletions tc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,25 @@ func (c Config) BuildTypeID(env string) (string, error) {
if err != nil {
return "", err
}
return c.BuildTypeIDForProject(currentProject, env)
}

func (c Config) BuildTypeIDForProject(project string, env string) (string, error) {
if c.BuildIDs == nil {
c.BuildIDs = make(map[string]map[string]string)
}
if envs, ok := c.BuildIDs[currentProject]; ok {
if envs, ok := c.BuildIDs[project]; ok {
if id, ok := envs[env]; ok {
return id, nil
}
} else {
c.BuildIDs[currentProject] = make(map[string]string)
c.BuildIDs[project] = make(map[string]string)
}

fmt.Printf("Set buildID for project %s in env %s: ", currentProject, env)
fmt.Printf("Set buildID for project %s in env %s: ", project, env)
var id string
fmt.Scanln(&id)
c.BuildIDs[currentProject][env] = id
c.BuildIDs[project][env] = id
if err := c.Save(); err != nil {
return "", err
}
Expand Down
39 changes: 38 additions & 1 deletion tc/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type buildResponse struct {
}

// BuildStatus ...
type BuildStatus string
type BuildStatus string

const (
// BuildStatusSuccess ...
Expand Down Expand Up @@ -105,6 +105,43 @@ func LastBuild(config Config, buildTypeID string) (Build, error) {
return Build{}, nil
}

func LastBuildSuccessByBranch(config Config, buildTypeID string, branch string) (Build, error) {
req, err := http.NewRequest(
http.MethodGet,
fmt.Sprintf("%s/app/rest/builds?locator=buildType:(id:%s),branch:%s,running:false,status:success,defaultFilter:false", config.URL, buildTypeID, branch),
nil,
)
if err != nil {
return Build{}, errors.WithStack(err)
}

req.Header.Add("Content-Type", "application/xml")
req.Header.Add("Accept", "application/json")
req.SetBasicAuth(config.UserName, config.Password)

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return Build{}, errors.WithStack(err)
}
defer res.Body.Close()

if res.StatusCode != 200 {
body, _ := ioutil.ReadAll(res.Body)
return Build{}, fmt.Errorf("error making request: %s", string(body))
}

builds := buildResponse{}
if err := json.NewDecoder(res.Body).Decode(&builds); err != nil {
return Build{}, errors.WithStack(err)
}

if len(builds.Builds) > 0 {
return builds.Builds[0], nil
}
return Build{}, nil
}

func GetBuild(config Config, buildID int) (DetailedBuild, error) {
req, err := http.NewRequest(
http.MethodGet,
Expand Down

0 comments on commit a142c2a

Please sign in to comment.