-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (103 loc) · 3.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"flag"
"fmt"
"gitlab.com/schehata/gitlab-pipeline-trigger/gitlab"
"log"
"os"
"strings"
"time"
)
// Configuration models the configurations needed to run the
// triggers, and/or extra parameters that might be passed to a pipeline
type Configuration struct {
Host string
APIToken string
TriggerToken string
TargetBranch string
ProjectID int
WaitForStatus bool
ExtraParams map[string]string
}
func parseFlags() (conf Configuration) {
flag.StringVar(&conf.Host, "h", "https://gitlab.com", "Gitlab Host URL (default: https://gitlab.com)")
flag.StringVar(&conf.APIToken, "a", "", "API Token")
flag.StringVar(&conf.TriggerToken, "t", "", "Trigger Token")
flag.StringVar(&conf.TargetBranch, "b", "master", "Target Branch (default: master)")
flag.IntVar(&conf.ProjectID, "p", 0, "Project ID")
flag.BoolVar(&conf.WaitForStatus, "w", false, "Wait for pipeline to finish")
conf.ExtraParams = make(map[string]string)
flag.Parse()
extraParams := flag.Args()
// loop over extra parameters and add them as variables[key]=value to Post Form
for _, param := range extraParams {
splitted := strings.Split(param, "=")
if len(splitted) < 2 {
err := fmt.Errorf("failed to parse argument %s, use syntax: key=value", param)
fmt.Println(err)
os.Exit(1)
}
key := fmt.Sprintf("variables[%s]", splitted[0])
conf.ExtraParams[key] = splitted[1]
}
return
}
func isConfigurationValid(conf Configuration) (err error) {
if conf.APIToken == "" {
err = fmt.Errorf("please set the API Token, pass -a $API_TOKEN to the command line")
}
if conf.TriggerToken == "" {
err = fmt.Errorf("please set the Trigger Token, pass -t $TRIGGER_TOKEN to the command line")
}
if conf.ProjectID == 0 {
err = fmt.Errorf("please set the Project ID, pass -p $PROJECT_ID to the command line")
}
return
}
func main() {
conf := parseFlags()
err := isConfigurationValid(conf)
if err != nil {
log.Fatal(err)
}
client := gitlab.New(conf.Host, conf.APIToken)
pipeline, err := client.TriggerPipeline(conf.ProjectID, conf.TriggerToken, conf.TargetBranch, conf.ExtraParams)
if err != nil {
log.Fatal(err)
}
fmt.Println("Triggered New Pipeline: ", pipeline.WebURL)
fmt.Printf("By user: %s(%s)\n", pipeline.User.Name, pipeline.User.Username)
fmt.Println("Created At:", pipeline.CreatedAt)
if !conf.WaitForStatus {
return
}
finished := false
fmt.Println("Getting ready to check pipeline status every 5 seconds...")
for finished == false {
pipeline, err = client.CheckPipelineStatus(pipeline.ID, conf.ProjectID)
if err != nil {
log.Fatal(err)
}
fmt.Print(".")
switch pipeline.Status {
case "failed":
finished = true
log.Printf("pipeline %d failed\n", pipeline.ID)
break
case "cancled":
finished = true
log.Printf("pipeline %d cancled\n", pipeline.ID)
break
case "success":
finished = true
log.Printf("pipeline %d succeeded", pipeline.ID)
break
default:
time.Sleep(5 * time.Second)
}
}
pipeline, err = client.CheckPipelineStatus(pipeline.ID, conf.ProjectID)
if err != nil {
log.Fatal(err)
}
}