Created
November 24, 2017 09:42
-
-
Save zmf/fb6cfc0764b374ed642f0256d34cf0d3 to your computer and use it in GitHub Desktop.
small Go program to get last status of gitlab CI pipeline
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
package main | |
import ( | |
"encoding/json" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
) | |
type pipeline struct { | |
ID int `json:"id"` | |
Sha string `json:"sha"` | |
Ref string `json:"ref"` | |
Status string `json:"status"` | |
} | |
type gError struct { | |
Message string `json:"message"` | |
} | |
func main() { | |
gitlabAPIBase := flag.String("gitlab", "", "gitlab API base, e.g: https://my.gitlab.org/api/v4") | |
httpUser := flag.String("user", "", "user for http auth") | |
httpPass := flag.String("pass", "", "password for http auth") | |
gitlabToken := flag.String("token", "", "gitlab token") | |
project := flag.String("project", "", "full project name e.g. org/test-project") | |
ref := flag.String("ref", "", "branch/tag") | |
flag.Parse() | |
if *gitlabAPIBase == "" { | |
log.Fatal("you need to define your gitlab API base") | |
os.Exit(1) | |
} | |
if *project == "" { | |
log.Fatal("you need to define a project name") | |
os.Exit(1) | |
} | |
if *ref == "" { | |
log.Fatal("you need to define a ref") | |
os.Exit(1) | |
} | |
auth := false | |
if *httpUser != "" && *httpPass != "" { | |
auth = true | |
} | |
rurl := *gitlabAPIBase + "/projects/" + url.QueryEscape(*project) + "/pipelines" | |
client := &http.Client{} | |
req, _ := http.NewRequest("GET", rurl, nil) | |
if auth { | |
req.SetBasicAuth(*httpUser, *httpPass) | |
} | |
if *gitlabToken != "" { | |
req.Header.Set("PRIVATE-TOKEN", *gitlabToken) | |
} | |
response, err := client.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} else { | |
defer response.Body.Close() | |
if response.Status == "401 Unauthorized" { | |
log.Printf("Unauthorized request. Try passing -user/-pass arguments\n") | |
os.Exit(1) | |
} | |
var pipelines []pipeline | |
data, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
err = json.Unmarshal(data, &pipelines) | |
if err != nil { | |
//check if we can unmarshal an error | |
var gErrorData gError | |
anotherErr := json.Unmarshal(data, &gErrorData) | |
if anotherErr != nil { | |
log.Fatal(err, anotherErr) | |
os.Exit(1) | |
} | |
fmt.Printf("%v\n", gErrorData) | |
return | |
} | |
maxID := 0 | |
lastStatus := "" | |
found := false | |
for _, pEntry := range pipelines { | |
if pEntry.ID > maxID && pEntry.Ref == *ref { | |
maxID = pEntry.ID | |
lastStatus = pEntry.Status | |
found = true | |
} | |
} | |
if found { | |
fmt.Printf("%s\n", lastStatus) | |
} else { | |
fmt.Printf("couldn't find pipeline data for specified ref\n") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment