@@ -2,80 +2,93 @@ package github
2
2
3
3
import (
4
4
"golang.org/x/oauth2"
5
- "github.com/google/go-github/github"
6
5
"golang.org/x/net/context"
6
+ "github.com/shurcooL/githubql"
7
7
)
8
8
9
9
type githubManager struct {
10
10
Context context.Context
11
- Client * github .Client
11
+ Client * githubql .Client
12
12
}
13
13
14
14
func New (githubAccessToken string ) (* githubManager ) {
15
15
ctx := context .Background ()
16
- ts := oauth2 .StaticTokenSource (
16
+ src := oauth2 .StaticTokenSource (
17
17
& oauth2.Token {AccessToken : githubAccessToken },
18
18
)
19
- tc := oauth2 .NewClient (ctx , ts )
20
-
21
- client := github .NewClient (tc )
19
+ httpClient := oauth2 .NewClient (ctx , src )
20
+ client := githubql .NewClient (httpClient )
22
21
23
22
return & githubManager {Context : ctx , Client : client }
24
23
}
25
24
26
- func (gm * githubManager ) GetCommits (owner , repo string , lastCommitsNumber int ) ([]* github.RepositoryCommit , error ) {
27
- client := gm .Client
28
- ctx := gm .Context
29
-
30
- var fullCommitsList []* github.RepositoryCommit
31
- commitsLeft := lastCommitsNumber
32
- pageNumber := 1
33
- for commitsLeft > 0 {
34
- commitsAmountToGetThisTime := commitsLeft
35
- if commitsLeft > 100 {
36
- commitsAmountToGetThisTime = 100
37
- }
38
-
39
- commits , _ , err := client .Repositories .ListCommits (ctx , owner , repo , & github.CommitsListOptions {ListOptions : github.ListOptions {Page : pageNumber , PerPage : commitsAmountToGetThisTime }})
40
- if nil != err {
41
- return nil , err
42
- }
25
+ func (gm * githubManager ) GetCommits (owner , repo , branch string , lastCommitsNumber int ) ([]Commit , error ) {
26
+ if lastCommitsNumber > 100 || lastCommitsNumber < 1 {
27
+ return nil , & Error {Message : "lastCommitsNumber must be a number between 1 and 100" } // TODO maybe in future implement pagination
28
+ }
43
29
44
- for _ , element := range commits {
45
- fullCommitsList = append (fullCommitsList , element )
46
- }
30
+ q := & githubQuery {}
47
31
48
- pageNumber ++
49
- commitsLeft -= commitsAmountToGetThisTime
32
+ client := gm .Client
33
+ err := client .Query (gm .Context , & q , map [string ]interface {}{
34
+ "owner" : githubql .String (owner ),
35
+ "name" : githubql .String (repo ),
36
+ "branch" : githubql .String (branch ),
37
+ "commitsNumber" : githubql .Int (lastCommitsNumber ),
38
+ "parentsNumber" : githubql .Int (1 ),
39
+ })
40
+ if nil != err {
41
+ return nil , err
50
42
}
51
43
52
- return fullCommitsList , nil
44
+ return hydrateCommits ( q ) , nil
53
45
}
54
46
55
- func PickFirstParentCommits (fullCommitsList []* github. RepositoryCommit ) ([]* github. RepositoryCommit ) {
56
- var firstParentCommits []* github. RepositoryCommit
47
+ func PickFirstParentCommits (fullCommitsList []Commit ) ([]Commit ) {
48
+ var firstParentCommits []Commit
57
49
if 0 == len (fullCommitsList ) {
58
50
return firstParentCommits
59
51
}
60
52
61
- fullCommitsMap := make (map [string ]* github. RepositoryCommit )
53
+ fullCommitsMap := make (map [string ]Commit )
62
54
for _ , c := range fullCommitsList {
63
- fullCommitsMap [c .GetSHA () ] = c
55
+ fullCommitsMap [c .SHA ] = c
64
56
}
65
57
66
- sha := fullCommitsList [0 ].GetSHA () // HEAD
58
+ sha := fullCommitsList [0 ].SHA // HEAD
67
59
for {
68
60
c , exists := fullCommitsMap [sha ]
69
61
if ! exists {
70
- break // last commit received from repo has a parent but parent doesnt exist in map
62
+ break // last commit received from repo has a parent but parent doesn't exist in map
71
63
}
72
64
73
65
firstParentCommits = append (firstParentCommits , c )
74
66
if 0 == len (c .Parents ) {
75
67
break // initial commit
76
68
}
77
- sha = c .Parents [0 ].GetSHA ()
69
+ sha = c .Parents [0 ].SHA
78
70
}
79
71
80
72
return firstParentCommits
81
- }
73
+ }
74
+
75
+ func hydrateCommits (q * githubQuery ) ([]Commit ) {
76
+ var fullCommitsList []Commit
77
+ for _ , edge := range q .Repository .Ref .Target .Commit .History .Edges {
78
+ var parents []Commit
79
+ for _ , parent := range edge .Node .Parents .Edges {
80
+ parents = append (parents , Commit {
81
+ SHA : string (parent .Node .Oid ),
82
+ Message : string (parent .Node .Message ),
83
+ })
84
+ }
85
+ fullCommitsList = append (fullCommitsList , Commit {
86
+ SHA : string (edge .Node .Oid ),
87
+ Message : string (edge .Node .Message ),
88
+ Parents : parents ,
89
+ StatusSuccess : bool (edge .Node .Status .State == githubql .String (githubql .StatusStateSuccess )),
90
+ })
91
+ }
92
+
93
+ return fullCommitsList
94
+ }
0 commit comments