-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (137 loc) · 4.02 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
)
var (
gitlabToken string
upvotesThreshold int
mergeRequestID int
pullURL string
rootCmd = &cobra.Command{
Short: "Check upvotes for a GitLab MR",
Version: version,
Run: func(cmd *cobra.Command, args []string) {
checkUpvotes()
},
}
version string
)
func init() {
rootCmd.PersistentFlags().StringVar(&gitlabToken, "token", getEnv("GITLAB_TOKEN", ""), "GitLab token (or set GITLAB_TOKEN env var)")
rootCmd.PersistentFlags().IntVar(&upvotesThreshold, "upvotes", getIntFromEnv("UPVOTES", 1), "Upvote threshold (or set UPVOTES env var)")
rootCmd.PersistentFlags().StringVar(&pullURL, "url", getEnv("PULL_URL", ""), "Merge request URL (or set PULL_URL env var)")
rootCmd.PersistentFlags().IntVar(&mergeRequestID, "pull-num", getIntFromEnv("PULL_NUM", 0), "Merge request ID (or set PULL_NUM env var)")
rootCmd.Use = filepath.Base(os.Args[0])
}
func main() {
if err := rootCmd.Execute(); err != nil {
log.Fatalf("Error: %s", err)
}
}
func checkUpvotes() {
if gitlabToken == "" {
log.Fatalf("GitLab token not provided.")
}
if pullURL == "" {
log.Fatalf("Merge request URL not provided.")
}
if mergeRequestID == 0 {
log.Fatalf("Merge request ID not provided.")
}
gitlabBaseURL, err := extractProtocolAndDomain(pullURL)
if err != nil {
log.Fatalf("Error extracting base URL: %s", err)
}
client, err := gitlab.NewClient(gitlabToken, gitlab.WithBaseURL(gitlabBaseURL))
if err != nil {
log.Fatalf("Failed to create client: %s", err)
}
projectNamespace, err := getProjectIDFromURL(pullURL)
if err != nil {
log.Fatalf("Failed to extract project namespace from URL: %s", err)
}
projectID, err := getNumericProjectID(client, projectNamespace)
if err != nil {
log.Fatalf("Failed to get numeric project ID: %s", err)
}
log.Printf("Numeric Project ID: %d", projectID)
mr, _, err := client.MergeRequests.GetMergeRequest(projectID, mergeRequestID, nil)
if err != nil {
log.Fatalf("Failed to get MR: %s", err)
}
log.Printf("Merge request Author: %s (%s)", mr.Author.Name, mr.Author.Username)
if mr.Upvotes >= upvotesThreshold {
log.Println("Merge request has sufficient upvotes.")
os.Exit(0)
} else {
log.Println("Merge request has insufficient upvotes.")
os.Exit(1)
}
}
func getProjectIDFromURL(mergeRequestURL string) (string, error) {
u, err := url.Parse(mergeRequestURL)
if err != nil {
return "", err
}
pathSegments := strings.Split(u.Path, "/")
if len(pathSegments) < 5 {
return "", fmt.Errorf("invalid URL format")
}
// Find the index where '-/merge_requests/' starts
mergeRequestsIndex := -1
for i, segment := range pathSegments {
if segment == "-" {
if i < len(pathSegments)-1 && pathSegments[i+1] == "merge_requests" {
mergeRequestsIndex = i
break
}
}
}
if mergeRequestsIndex == -1 {
return "", fmt.Errorf("invalid URL format")
}
// Concatenate all segments before '-/merge_requests/' to form the complete project path
projectPath := strings.Join(pathSegments[:mergeRequestsIndex], "/")[1:] // remove leading '/'
return projectPath, nil
}
func extractProtocolAndDomain(rawURL string) (string, error) {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return "", err
}
return fmt.Sprintf("%s://%s", parsedURL.Scheme, parsedURL.Host), nil
}
func getNumericProjectID(client *gitlab.Client, projectNamespace string) (int, error) {
project, _, err := client.Projects.GetProject(projectNamespace, nil)
if err != nil {
return 0, err
}
return project.ID, nil
}
func getIntFromEnv(envVar string, defaultValue int) int {
valueStr := os.Getenv(envVar)
if valueStr == "" {
return defaultValue
}
value, err := strconv.Atoi(valueStr)
if err != nil {
log.Printf("Could not convert env var '%s' to int: %s", envVar, err)
return defaultValue
}
return value
}
func getEnv(envVar, defaultValue string) string {
value := os.Getenv(envVar)
if value == "" {
return defaultValue
}
return value
}