-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.go
120 lines (106 loc) · 3.11 KB
/
gitlab.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
package api
import (
"encoding/base64"
"encoding/json"
"log"
"net/http"
"strconv"
"strings"
gitutils "github.com/adedayo/checkmate-core/pkg/git"
"github.com/adedayo/checkmate-core/pkg/util"
"github.com/adedayo/git-service-driver/pkg/gitlab"
)
func discoverGitLab(w http.ResponseWriter, r *http.Request) {
config, err := configManager.GetConfig()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var pagedSearch gitlab.GitLabPagedSearch
if err := json.NewDecoder(r.Body).Decode(&pagedSearch); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
gitService, err := config.FindService(pagedSearch.ServiceID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
proj, loc, err := gitlab.GetRepositories(r.Context(), gitService, &pagedSearch)
if err != nil {
log.Printf("Error: %v\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(gitlab.GitLabProjectSearchResult{
InstanceID: gitService.ID,
Projects: proj,
EndCursor: loc.EndCursor,
HasNextPage: loc.HasNextPage,
RemainingProjectsCount: getCount(loc),
})
}
func getCount(loc gitlab.GitLabCursorLocation) int64 {
if loc.HasNextPage {
if x, err := base64.RawStdEncoding.DecodeString(loc.EndCursor); err == nil {
var out struct {
ID string `json:"id"`
}
if e := json.Unmarshal(x, &out); e == nil {
n, err := strconv.ParseInt(out.ID, 10, 0)
if err != nil {
log.Printf("%v", e)
return 0
}
return n
} else {
log.Printf("%v", e)
}
} else {
log.Printf("%v", err)
}
}
return 0
}
func integrateGitLab(w http.ResponseWriter, r *http.Request) {
var detail gitutils.GitService
if err := json.NewDecoder(r.Body).Decode(&detail); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
detail.InstanceURL = strings.TrimSuffix(strings.TrimSpace(detail.InstanceURL), "/")
detail.GraphQLEndPoint = detail.InstanceURL + "/api/graphql"
detail.APIEndPoint = detail.InstanceURL + "/api"
detail.ID = util.NewRandomUUID().String()
detail.Type = gitutils.GitLab
config, err := configManager.GetConfig()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := config.AddService(&detail); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
json.NewEncoder(w).Encode(listIntegrations(gitutils.GitLab))
}
func deleteGitLabIntegration(w http.ResponseWriter, r *http.Request) {
var id struct {
ID string
}
if err := json.NewDecoder(r.Body).Decode(&id); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
config, err := configManager.GetConfig()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
delete(config.GitServices[gitutils.GitLab], id.ID)
configManager.SaveConfig(config)
json.NewEncoder(w).Encode(listIntegrations(gitutils.GitLab))
}
func getGitLabIntegrations(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(listIntegrations(gitutils.GitLab))
}