-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_type.go
98 lines (84 loc) · 2.35 KB
/
find_type.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
package github
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/acs-dl/github-module-svc/internal/data"
"github.com/acs-dl/github-module-svc/internal/helpers"
"gitlab.com/distributed_lab/logan/v3/errors"
)
func (g *github) FindType(link string) (*TypeSub, error) {
repo, err := g.GetRepositoryFromApi(link)
if err != nil {
return nil, err
}
if repo != nil {
return &TypeSub{data.Repository, *repo}, err
}
org, err := g.GetOrganizationFromApi(link)
if err != nil {
return nil, err
}
if org != nil {
return &TypeSub{data.Organization, *org}, nil
}
return nil, nil
}
func (g *github) GetRepositoryFromApi(link string) (*data.Sub, error) {
params := data.RequestParams{
Method: http.MethodGet,
Link: fmt.Sprintf("https://api.github.com/repos/%s", link),
Body: nil,
Query: nil,
Header: map[string]string{
"Accept": data.AcceptHeader,
"Authorization": "Bearer " + g.superUserToken,
"X-GitHub-Api-Version": data.GithubApiVersionHeader,
},
Timeout: time.Second * 30,
}
res, err := helpers.MakeHttpRequest(params)
if err != nil {
return nil, errors.Wrap(err, "failed to make http request")
}
res, err = helpers.HandleHttpResponseStatusCode(res, params)
if err != nil {
return nil, errors.Wrap(err, "failed to check response status code")
}
if res == nil {
return nil, nil
}
var response data.Sub
if err = json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal body")
}
response.Type = data.Repository
return &response, nil
}
func (g *github) GetOrganizationFromApi(link string) (*data.Sub, error) {
params := data.RequestParams{
Method: http.MethodGet,
Link: fmt.Sprintf("https://api.github.com/orgs/%s", link),
Body: nil,
Query: nil,
Header: map[string]string{
"Accept": data.AcceptHeader,
"Authorization": "Bearer " + g.superUserToken,
"X-GitHub-Api-Version": data.GithubApiVersionHeader,
},
Timeout: time.Second * 30,
}
res, err := helpers.MakeHttpRequest(params)
if err != nil {
return nil, errors.Wrap(err, "failed to make http request")
}
res, err = helpers.HandleHttpResponseStatusCode(res, params)
if err != nil {
return nil, errors.Wrap(err, "failed to check response status code")
}
if res == nil {
return nil, nil
}
return populateGetOrganizationResponse(res)
}