/
pipeline.go
97 lines (82 loc) · 3.57 KB
/
pipeline.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
package logic
import (
"encoding/json"
"github.com/klovercloud-ci-cd/api-service/config"
v1 "github.com/klovercloud-ci-cd/api-service/core/v1"
"github.com/klovercloud-ci-cd/api-service/core/v1/service"
"net/http"
)
type pipelineService struct {
httpClient service.HttpClient
websocketClient service.WebsocketClient
}
func (p pipelineService) Create(companyId, repositoryId, url string, payload interface{}) (httpCode int, body interface{}) {
marshal, err := json.Marshal(payload)
if err != nil {
return http.StatusBadRequest, err
}
header := make(map[string]string)
header["token"] = config.Token
header["Content-Type"] = "application/json"
code, err := p.httpClient.Post(config.KlovercloudIntegrationMangerUrl+"/pipelines?url="+url+"&companyId="+companyId+"&repositoryId="+repositoryId, header, marshal)
return code, err
}
func (p pipelineService) Update(companyId, repositoryId, url string, payload interface{}) (httpCode int, body interface{}) {
marshal, err := json.Marshal(payload)
if err != nil {
return http.StatusBadRequest, err
}
header := make(map[string]string)
header["token"] = config.Token
header["Content-Type"] = "application/json"
code, err := p.httpClient.Put(config.KlovercloudIntegrationMangerUrl+"/pipelines?url="+url+"&companyId="+companyId+"&repositoryId="+repositoryId, header, marshal)
return code, err
}
func (p pipelineService) ReadEventsByCompanyId(c chan map[string]interface{}, companyId, userId string) {
p.websocketClient.Get(c, config.KlovercloudEventStoreWebSocketUrl+"/pipelines/ws?companyId="+companyId+"&userId="+userId, http.Header{"token": {config.Token}})
}
func (p pipelineService) ReadEventsByCompanyIdAndUserIdAndTime(c chan map[string]interface{}, companyId, userId, from string) {
p.websocketClient.Get(c, config.KlovercloudEventStoreWebSocketUrl+"/pipelines/ws?companyId="+companyId+"&userId="+userId+"&from="+from, http.Header{"token": {config.Token}})
}
func (p pipelineService) Get(companyId, repositoryId, applicationId, url, revision, action, from, to string) (httpCode int, body interface{}) {
var response interface{}
header := make(map[string]string)
header["token"] = config.Token
var code int
var b []byte
var err error
if action == "dashboard_data" {
code, b, err = p.httpClient.Get(config.KlovercloudEventStoreUrl+"/pipelines?action="+action+"&companyId="+companyId+"&from="+from+"&to="+to, header)
} else {
code, b, err = p.httpClient.Get(config.KlovercloudIntegrationMangerUrl+"/pipelines?action="+action+"&companyId="+companyId+"&repositoryId="+repositoryId+"&applicationId="+applicationId+"&url="+url+"&revision="+revision, header)
}
if err != nil {
return code, err
}
err = json.Unmarshal(b, &response)
if err != nil {
return http.StatusBadRequest, err
}
return code, response
}
func (p pipelineService) GetByProcessId(companyId, processId, action string, option v1.Pagination) (httpCode int, body interface{}) {
var response interface{}
header := make(map[string]string)
header["token"] = config.Token
code, b, err := p.httpClient.Get(config.KlovercloudEventStoreUrl+"/pipelines/"+processId+"?action="+action+"&companyId="+companyId+"&order=&page="+option.Page+"&limit="+option.Limit, header)
if err != nil {
return code, err
}
err = json.Unmarshal(b, &response)
if err != nil {
return http.StatusBadRequest, err
}
return code, response
}
// NewPipelineService returns Pipeline type service
func NewPipelineService(publisher service.HttpClient, websocketClient service.WebsocketClient) service.Pipeline {
return pipelineService{
httpClient: publisher,
websocketClient: websocketClient,
}
}