-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontexts.go
111 lines (89 loc) · 2.3 KB
/
contexts.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
package codefresh
type (
IContextAPI interface {
GetGitContexts() (error, *[]ContextPayload)
GetGitContextByName(name string) (error, *ContextPayload)
GetDefaultGitContext() (error, *ContextPayload)
}
context struct {
codefresh *codefresh
}
ContextPayload struct {
Metadata struct {
Name string `json:"name"`
}
Spec struct {
Type string `json:"type"`
Data struct {
Auth struct {
Type string `json:"type"`
Username string `json:"username"`
Password string `json:"password"`
ApiHost string `json:"apiHost"`
// for gitlab
ApiURL string `json:"apiURL"`
ApiPathPrefix string `json:"apiPathPrefix"`
SshPrivateKey string `json:"sshPrivateKey"`
AppId string `json:"appId"`
InstallationId string `json:"installationId"`
PrivateKey string `json:"privateKey"`
} `json:"auth"`
} `json:"data"`
} `json:"spec"`
}
GitContextsQs struct {
Type []string `url:"type"`
Decrypt string `url:"decrypt"`
}
)
func newContextAPI(codefresh *codefresh) IContextAPI {
return &context{codefresh}
}
func (c context) GetGitContexts() (error, *[]ContextPayload) {
var result []ContextPayload
qs := GitContextsQs{
Type: []string{"git.github", "git.gitlab", "git.github-app"},
Decrypt: "true",
}
resp, err := c.codefresh.requestAPI(&requestOptions{
method: "GET",
path: "/api/contexts",
qs: qs,
})
if err != nil {
return err, nil
}
err = c.codefresh.decodeResponseInto(resp, &result)
defer resp.Body.Close()
return err, &result
}
func (c context) GetGitContextByName(name string) (error, *ContextPayload) {
var result ContextPayload
var qs = map[string]string{
"decrypt": "true",
}
resp, err := c.codefresh.requestAPI(&requestOptions{
method: "GET",
path: "/api/contexts/" + name,
qs: qs,
})
if err != nil {
return err, nil
}
err = c.codefresh.decodeResponseInto(resp, &result)
defer resp.Body.Close()
return nil, &result
}
func (c context) GetDefaultGitContext() (error, *ContextPayload) {
var result ContextPayload
resp, err := c.codefresh.requestAPI(&requestOptions{
method: "GET",
path: "/api/contexts/git/default",
})
if err != nil {
return err, nil
}
err = c.codefresh.decodeResponseInto(resp, &result)
defer resp.Body.Close()
return err, &result
}