-
Notifications
You must be signed in to change notification settings - Fork 58
/
github_graphql.go
161 lines (147 loc) · 4.82 KB
/
github_graphql.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 resource
import (
"context"
"encoding/base64"
"errors"
"regexp"
"strconv"
"time"
"github.com/google/go-github/v66/github"
"github.com/shurcooL/githubv4"
)
func (g *GitHubClient) listReleasesV4EnterPrice() ([]*github.RepositoryRelease, error) {
if g.clientV4 == nil {
return nil, errors.New("github graphql is not been initialised")
}
var listReleasesEnterprise struct {
Repository struct {
Releases struct {
Edges []struct {
Node struct {
ReleaseObjectEnterprise
}
} `graphql:"edges"`
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
} `graphql:"pageInfo"`
} `graphql:"releases(first:$releasesCount, after: $releaseCursor, orderBy: {field: CREATED_AT, direction: DESC})"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}
vars := map[string]interface{}{
"repositoryOwner": githubv4.String(g.owner),
"repositoryName": githubv4.String(g.repository),
"releaseCursor": (*githubv4.String)(nil),
"releasesCount": githubv4.Int(100),
}
var allReleases []*github.RepositoryRelease
for {
if err := g.clientV4.Query(context.TODO(), &listReleasesEnterprise, vars); err != nil {
return nil, err
}
for _, r := range listReleasesEnterprise.Repository.Releases.Edges {
r := r
publishedAt, _ := time.ParseInLocation(time.RFC3339, r.Node.PublishedAt.Time.Format(time.RFC3339), time.UTC)
createdAt, _ := time.ParseInLocation(time.RFC3339, r.Node.CreatedAt.Time.Format(time.RFC3339), time.UTC)
var releaseID int64
decodedID, err := base64.StdEncoding.DecodeString(r.Node.ID)
if err != nil {
return nil, err
}
re := regexp.MustCompile(`.*[^\d]`)
decodedID = re.ReplaceAll(decodedID, []byte(""))
if string(decodedID) == "" {
return nil, errors.New("bad release id from graph ql api")
}
releaseID, err = strconv.ParseInt(string(decodedID), 10, 64)
if err != nil {
return nil, err
}
allReleases = append(allReleases, &github.RepositoryRelease{
ID: &releaseID,
TagName: &r.Node.TagName,
Name: &r.Node.Name,
Prerelease: &r.Node.IsPrerelease,
Draft: &r.Node.IsDraft,
URL: &r.Node.URL,
PublishedAt: &github.Timestamp{Time: publishedAt},
CreatedAt: &github.Timestamp{Time: createdAt},
})
}
if !listReleasesEnterprise.Repository.Releases.PageInfo.HasNextPage {
break
}
vars["releaseCursor"] = listReleasesEnterprise.Repository.Releases.PageInfo.EndCursor
}
return allReleases, nil
}
func (g *GitHubClient) listReleasesV4() ([]*github.RepositoryRelease, error) {
if g.clientV4 == nil {
return nil, errors.New("github graphql is not been initialised")
}
var listReleases struct {
Repository struct {
Releases struct {
Edges []struct {
Node struct {
ReleaseObject
}
} `graphql:"edges"`
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
} `graphql:"pageInfo"`
} `graphql:"releases(first:$releasesCount, after: $releaseCursor, orderBy: {field: CREATED_AT, direction: DESC})"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}
vars := map[string]interface{}{
"repositoryOwner": githubv4.String(g.owner),
"repositoryName": githubv4.String(g.repository),
"releaseCursor": (*githubv4.String)(nil),
"releasesCount": githubv4.Int(100),
}
var allReleases []*github.RepositoryRelease
for {
if err := g.clientV4.Query(context.TODO(), &listReleases, vars); err != nil {
return nil, err
}
for _, r := range listReleases.Repository.Releases.Edges {
r := r
publishedAt, _ := time.ParseInLocation(time.RFC3339, r.Node.PublishedAt.Time.Format(time.RFC3339), time.UTC)
createdAt, _ := time.ParseInLocation(time.RFC3339, r.Node.CreatedAt.Time.Format(time.RFC3339), time.UTC)
var releaseID int64
if r.Node.DatabaseId == 0 {
decodedID, err := base64.StdEncoding.DecodeString(r.Node.ID)
if err != nil {
return nil, err
}
re := regexp.MustCompile(`.*[^\d]`)
decodedID = re.ReplaceAll(decodedID, []byte(""))
if string(decodedID) == "" {
return nil, errors.New("bad release id from graph ql api")
}
releaseID, err = strconv.ParseInt(string(decodedID), 10, 64)
if err != nil {
return nil, err
}
} else {
releaseID = int64(r.Node.DatabaseId)
}
allReleases = append(allReleases, &github.RepositoryRelease{
ID: &releaseID,
TagName: &r.Node.TagName,
Name: &r.Node.Name,
Prerelease: &r.Node.IsPrerelease,
Draft: &r.Node.IsDraft,
URL: &r.Node.URL,
PublishedAt: &github.Timestamp{Time: publishedAt},
CreatedAt: &github.Timestamp{Time: createdAt},
})
}
if !listReleases.Repository.Releases.PageInfo.HasNextPage {
break
}
vars["releaseCursor"] = listReleases.Repository.Releases.PageInfo.EndCursor
}
return allReleases, nil
}