forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
app.go
102 lines (84 loc) · 2.71 KB
/
app.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
package github
import (
"context"
"fmt"
"net/http"
"time"
"github.com/jenkins-x/go-scm/scm"
)
const (
// https://developer.github.com/changes/2016-09-14-Integrations-Early-Access/
mediaTypeIntegrationPreview = "application/vnd.github.machine-man-preview+json"
)
type appService struct {
client *wrapper
}
type installationToken struct {
Token *string `json:"token,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
}
func (s *appService) CreateInstallationToken(ctx context.Context, id int64) (*scm.InstallationToken, *scm.Response, error) {
path := fmt.Sprintf("app/installations/%v/access_tokens", id)
out := new(installationToken)
req := &scm.Request{
Method: http.MethodPost,
Path: path,
Header: map[string][]string{
// TODO: remove custom Accept header when this API fully launches.
"Accept": {mediaTypeIntegrationPreview},
},
}
res, err := s.client.doRequest(ctx, req, nil, out)
return convertInstallationToken(out), res, err
}
func (s *appService) GetRepositoryInstallation(ctx context.Context, fullName string) (*scm.Installation, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/installation", fullName)
out := new(installation)
req := &scm.Request{
Method: http.MethodGet,
Path: path,
Header: map[string][]string{
// TODO: remove custom Accept header when this API fully launches.
"Accept": {mediaTypeIntegrationPreview},
},
}
res, err := s.client.doRequest(ctx, req, nil, out)
return convertInstallation(out), res, err
}
func (s *appService) GetOrganisationInstallation(ctx context.Context, organisation string) (*scm.Installation, *scm.Response, error) {
path := fmt.Sprintf("orgs/%s/installation", organisation)
out := new(installation)
req := &scm.Request{
Method: http.MethodGet,
Path: path,
Header: map[string][]string{
// TODO: remove custom Accept header when this API fully launches.
"Accept": {mediaTypeIntegrationPreview},
},
}
res, err := s.client.doRequest(ctx, req, nil, out)
return convertInstallation(out), res, err
}
func (s *appService) GetUserInstallation(ctx context.Context, user string) (*scm.Installation, *scm.Response, error) {
path := fmt.Sprintf("users/%s/installation", user)
out := new(installation)
req := &scm.Request{
Method: http.MethodGet,
Path: path,
Header: map[string][]string{
// TODO: remove custom Accept header when this API fully launches.
"Accept": {mediaTypeIntegrationPreview},
},
}
res, err := s.client.doRequest(ctx, req, nil, out)
return convertInstallation(out), res, err
}
func convertInstallationToken(src *installationToken) *scm.InstallationToken {
dst := &scm.InstallationToken{
ExpiresAt: src.ExpiresAt,
}
if src.Token != nil {
dst.Token = *src.Token
}
return dst
}