Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
Refactor(pipeline): use go-gitlab
Browse files Browse the repository at this point in the history
  • Loading branch information
profclems committed Aug 4, 2020
1 parent 70b2b05 commit 619fc6c
Showing 1 changed file with 27 additions and 61 deletions.
88 changes: 27 additions & 61 deletions commands/pipeline.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
package commands

import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"text/tabwriter"

"github.com/logrusorgru/aurora"
"github.com/xanzy/go-gitlab"
)

//use struct instead of interface
//(json.Unmarshal causing big integer which pipline ID value to change)
type pipline struct {
ID int64 `json:"id"`
Sha string `json:"sha"`
Ref string `json:"ref"`
Status string `json:"status"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
WebURL string `json:"web_url"`
}

func displayMultiplePiplines(m []pipline) {
func displayMultiplePipelines(m []*gitlab.PipelineInfo) {
// initialize tabwriter
w := new(tabwriter.Writer)

Expand All @@ -32,34 +20,33 @@ func displayMultiplePiplines(m []pipline) {

defer w.Flush()
if len(m) > 0 {
fmt.Printf("Showing piplines %d of %d on %s\n\n", len(m), len(m), GetEnv("GITLAB_REPO"))
for i := 0; i < len(m); i++ {

duration := TimeAgo(m[i].CreatedAt)
fmt.Printf("Showing pipelines %d of %d on %s\n\n", len(m), len(m), getRepo())
for _, pipeline := range m {
duration := TimeAgo(*pipeline.CreatedAt)

if m[i].Status == "success" {
_, _ = fmt.Fprintln(w, aurora.Green(fmt.Sprint("#", m[i].ID)), "\t", m[i].Ref, "\t", aurora.Magenta(duration))
if pipeline.Status == "success" {
_, _ = fmt.Fprintln(w, aurora.Green(fmt.Sprint("#", pipeline.ID)), "\t", pipeline.Ref, "\t", aurora.Magenta(duration))
} else {
_, _ = fmt.Fprintln(w, aurora.Red(fmt.Sprint("#", m[i].ID)), "\t", m[i].Ref, "\t", aurora.Magenta(duration))
_, _ = fmt.Fprintln(w, aurora.Red(fmt.Sprint("#", pipeline.ID)), "\t", pipeline.Ref, "\t", aurora.Magenta(duration))
}
}
} else {
fmt.Println("No Pipelines available on " + GetEnv("GITLAB_REPO"))
fmt.Println("No Pipelines available on " + getRepo())
}
}

func deletePipeline(cmdArgs map[string]string, arrFlags map[int]string) {
pipelineID := strings.Trim(arrFlags[1], " ")
git, repo := InitGitlabClient()

if CommandArgExists(cmdArgs, pipelineID) {
arrIds := strings.Split(strings.Trim(pipelineID, "[] "), ",")
for _, i2 := range arrIds {
fmt.Println("Deleting Pipeline #" + i2)
queryStrings := "/" + i2
resp := MakeRequest("{}", "projects/"+GetEnv("GITLAB_PROJECT_ID")+"/pipelines"+queryStrings, "DELETE")
if resp["responseCode"] == 204 {
bodyString := resp["responseMessage"]
fmt.Println(bodyString)
pipeline, _ := git.Pipelines.DeletePipeline(repo, stringToInt(i2))
if pipeline.StatusCode == 204 {
fmt.Println(aurora.Green("Pipeline Deleted Successfully"))
} else if resp["responseCode"] == 404 {
} else if pipeline.StatusCode == 404 {
fmt.Println(aurora.Red("Pipeline does not exist"))
} else {
fmt.Println(aurora.Red("Could not complete request."))
Expand All @@ -71,47 +58,26 @@ func deletePipeline(cmdArgs map[string]string, arrFlags map[int]string) {
fmt.Println("Usage: glab pipeline delete <pipeline-id>")
}
}

func listPipeline(cmdArgs map[string]string, _ map[int]string) {
var queryStrings = "status="
if CommandArgExists(cmdArgs, "all") {
queryStrings = ""
} else if CommandArgExists(cmdArgs, "failed") {
queryStrings += "failed&"
git, repo := InitGitlabClient()
l := &gitlab.ListProjectPipelinesOptions{}
if CommandArgExists(cmdArgs, "failed") {
l.Status = gitlab.BuildState("failed")
} else {
queryStrings += "success&"
l.Status = gitlab.BuildState("success")
}
if CommandArgExists(cmdArgs, "order_by") {
queryStrings += "order_by=" + cmdArgs["order_by"] + "&"
l.OrderBy = gitlab.String(cmdArgs["order_by"])
}
if CommandArgExists(cmdArgs, "sort") {
queryStrings += "sort=" + cmdArgs["sort"] + "&"
}
queryStrings = strings.Trim(queryStrings, "& ")
if len(queryStrings) > 0 {
queryStrings = "?" + queryStrings
l.Sort = gitlab.String(cmdArgs["sort"])
}

resp := MakeRequest("{}", "projects/"+GetEnv("GITLAB_PROJECT_ID")+"/pipelines"+queryStrings, "GET")

if resp["responseCode"] == 200 {
bodyString := resp["responseMessage"]
if _, ok := bodyString.(string); ok {
// fmt.Println(bodyString.(string))
var m []pipline
err := json.Unmarshal([]byte(bodyString.(string)), &m)
if err != nil {
log.Fatal(err)
}

fmt.Println()

displayMultiplePiplines(m)
fmt.Println()

}
} else {
fmt.Println(resp["responseCode"], resp["responseMessage"])
mergeRequests, _, err := git.Pipelines.ListProjectPipelines(repo, l)
if err != nil {
log.Fatal(err)
}
displayMultiplePipelines(mergeRequests)
}

// ExecPipeline is exported
Expand Down

0 comments on commit 619fc6c

Please sign in to comment.