-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (61 loc) · 1.46 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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"github.com/raojinlin/env-from-mr/parser"
)
var (
baseUrl string
token string
projectID string
mergeRequestID int
)
func exportEnv(env map[string]string) {
for k, v := range env {
fmt.Printf("export %s='%v'\n", k, v)
}
}
func main() {
flag.StringVar(&baseUrl, "url", "", "GitLab base url")
flag.StringVar(&token, "token", "", "GitLab api access token")
flag.StringVar(&projectID, "pid", "", "GitLab project id")
flag.IntVar(&mergeRequestID, "mr-iid", 0, "GitLab Merge Request internal ID")
flag.Parse()
if baseUrl == "" {
fmt.Println("GitLab url required.")
flag.Usage()
os.Exit(1)
}
if token == "" {
fmt.Println("GitLab access token required.")
flag.Usage()
os.Exit(1)
}
if projectID == "" {
fmt.Println("GitLab project id required.")
flag.Usage()
os.Exit(1)
}
// 没有指定ID,取环境变量CI_MERGE_REQUEST_IID
if mergeRequestID == 0 {
id := os.Getenv("CI_MERGE_REQUEST_IID")
if id == "" {
fmt.Println("No merge request id specify.")
os.Exit(1)
}
i, err := strconv.ParseInt(id, 10, 64)
if err != nil {
fmt.Println("Parse merge request id from env CI_MERGE_REQUEST_IID failed: ", err.Error())
os.Exit(2)
}
mergeRequestID = int(i)
}
mr, err := getMergeRequest(baseUrl, token, projectID, mergeRequestID)
if err != nil {
fmt.Println("Get merge request failed: ", err.Error())
os.Exit(1)
}
exportEnv(parser.Parse(mr.Description))
}