-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
71 lines (63 loc) · 1.77 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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/xanzy/go-gitlab"
)
type Query struct {
ProjectID int `json:"project_id"`
Environment string `json:"environment"`
}
func main() {
clientHTTP := (*http.Client)(nil)
if os.Getenv("HTTPS_SKIP_VERIFY") == "1" {
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
clientHTTP = &http.Client{Transport: transCfg}
}
client, err := gitlab.NewClient(
os.Getenv("GITLAB_TOKEN"),
gitlab.WithHTTPClient(clientHTTP),
gitlab.WithBaseURL(os.Getenv("GITLAB_URL")))
if err != nil {
log.Fatalf("Error creating gitlab client: %v", err)
}
ds := &DataSource{}
ds.HandleAnnotation(func(req AnnotationsRequest) ([]AnnotationsResponse, error) {
var query Query
if err := json.Unmarshal([]byte(req.Annotation.Query), &query); err != nil {
return nil, fmt.Errorf("couldn't parse query: %w", err)
}
ds, _, err := client.Deployments.ListProjectDeployments(
query.ProjectID,
&gitlab.ListProjectDeploymentsOptions{
OrderBy: gitlab.String("created_at"),
Sort: gitlab.String("desc"),
Status: gitlab.String("success"),
Environment: gitlab.String(query.Environment),
UpdatedAfter: &req.Range.From,
UpdatedBefore: &req.Range.To,
},
)
if err != nil {
return nil, fmt.Errorf("failed to get deployments: %w", err)
}
result := make([]AnnotationsResponse, 0)
for _, d := range ds {
result = append(result, AnnotationsResponse{
Annotation: req.Annotation,
Time: d.CreatedAt.Unix() * 1000,
Title: d.Deployable.Commit.Title,
})
}
return result, nil
})
if err := ds.ListenAndServe(os.Getenv("HTTP_ADDRESS")); err != nil {
log.Fatalf("Error running server: %v", err)
}
}