generated from parrogo/gomod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeclimate.go
161 lines (131 loc) · 3.64 KB
/
codeclimate.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Package ccaddrepo allows to add a GitHub repository to CodeClimate
// and setup its report id as a CC_TEST_REPORTER_ID secret in the repository.
//
// This two function works together in order to automate the setup of
// a GitHub repository.
//
// If you are using this package in GitHub Actions,
// you can easily publish coverages reports to CodeClimate
// using e.g. [paambaati/codeclimate-action](https://github.com/paambaati/codeclimate-action)
package ccaddrepo
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
const ccBaseURL = "https://api.codeclimate.com/v1/"
const ccGetURL = "https://api.codeclimate.com/v1/repos"
const ccURL = "https://api.codeclimate.com/v1/github/repos"
const ccBodyFormat = `{"data":{"type": "repos","attributes": {"url": "https://github.com/%s"}}}`
// CodeClimate represent an authenticated
// session on Code Climate.
type CodeClimate string
func (cc CodeClimate) doRequest(method string, URL string, body io.Reader, response interface{}) (string, error) {
req, err := http.NewRequest(method, ccBaseURL+URL, body)
if err != nil {
return "", err
}
req.Header.Add("Accept", "application/vnd.api+json")
req.Header.Add("Authorization", "Token token="+string(cc))
req.Header.Add("Content-Type", "application/vnd.api+json")
var c http.Client
res, err := c.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
resbuf, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
if response == nil {
return string(resbuf), nil
}
err = json.Unmarshal(resbuf, response)
if err != nil {
return "", err
}
return string(resbuf), nil
}
// GetRepoID returns the ID of a repository
func (cc CodeClimate) GetRepoID(reposlug string) (string, error) {
var response struct {
Data []struct {
ID string
}
}
rowdata, err := cc.doRequest("GET", "repos?github_slug="+reposlug, nil, &response)
if err != nil {
return "", err
}
if len(response.Data) == 0 {
return "", fmt.Errorf("repository not found: %s\nRESPONSE: %s", reposlug, rowdata)
}
data := response.Data[0]
return data.ID, nil
}
// DeleteRepo remove a repository from CodeClimate
func (cc CodeClimate) DeleteRepo(repoid string) error {
_, err := cc.doRequest("DELETE", "repos/"+repoid, nil, nil)
if err != nil {
return err
}
return nil
}
// GetOwnOrgID returns the ID of an organization
func (cc CodeClimate) GetOwnOrgID(orgname string) (string, error) {
var response struct {
Data []struct {
ID string
Attributes struct {
Name string
}
}
}
rowdata, err := cc.doRequest("GET", "orgs", nil, &response)
if err != nil {
return "", err
}
for _, data := range response.Data {
if data.Attributes.Name == orgname {
return data.ID, nil
}
}
return "", fmt.Errorf("org ID `%s` not found in response data\nRESPONSE:\n%v", orgname, rowdata)
}
// RepoInfo represents the information retrieved from a repo
type RepoInfo struct {
Data struct {
ID string
Attributes struct {
TestReporterID string `json:"test_reporter_id"`
BadgeTokenID string `json:"badge_token"`
}
}
}
// AddRepo create a repository within an organization
// and return the reporter ID.
func (cc CodeClimate) AddRepo(reposlug string) (*RepoInfo, error) {
var response RepoInfo
parts := strings.Split(reposlug, "/")
org := parts[0]
orgID, err := cc.GetOwnOrgID(org)
if err != nil {
return nil, err
}
URL := fmt.Sprintf("orgs/%s/repos", orgID)
var body bytes.Buffer
_, err = body.Write([]byte(fmt.Sprintf(ccBodyFormat, reposlug)))
if err != nil {
return nil, err
}
_, err = cc.doRequest("POST", URL, &body, &response)
if err != nil {
return nil, err
}
//fmt.Println(res)
return &response, nil
}