Skip to content

Commit c818d86

Browse files
committed
refactor, use newer gh api thus previous one generates too many requests
1 parent 06145fd commit c818d86

File tree

7 files changed

+133
-43
lines changed

7 files changed

+133
-43
lines changed

Gopkg.lock

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
unused-packages = true
3131

3232
[[constraint]]
33-
name = "github.com/google/go-github"
34-
version = "15.0.0"
33+
branch = "master"
34+
name = "golang.org/x/oauth2"
3535

3636
[[constraint]]
3737
branch = "master"
38-
name = "golang.org/x/oauth2"
38+
name = "github.com/shurcooL/githubql"

github-flow-manager.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import (
88

99
func main() {
1010
gm := github.New(os.Getenv("GITHUB_TOKEN"))
11-
commits, _ := gm.GetCommits("DocPlanner", "github-flow-manager-test-repo", 10)
11+
commits, err := gm.GetCommits("DocPlanner", "github-flow-manager-test-repo", "master", 10)
12+
if nil != err {
13+
fmt.Println(err)
14+
panic("")
15+
}
1216
firstParentCommits := github.PickFirstParentCommits(commits)
1317

1418
for _, c := range firstParentCommits {
15-
fmt.Println(c.GetSHA(), c.GetCommit().GetMessage())
19+
fmt.Println(c.SHA, c.Message)
1620
}
1721
}

github/commit.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package github
2+
3+
type Commit struct {
4+
SHA string
5+
Message string
6+
Parents []Commit
7+
StatusSuccess bool
8+
}

github/error.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package github
2+
3+
type Error struct {
4+
Message string
5+
}
6+
7+
func (e Error) Error() (string) {
8+
return e.Message
9+
}

github/github-query.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package github
2+
3+
import "github.com/shurcooL/githubql"
4+
5+
type githubQuery struct {
6+
Repository struct {
7+
Ref struct {
8+
Target struct {
9+
Commit struct {
10+
History struct {
11+
Edges []struct {
12+
Node struct {
13+
Parents struct {
14+
Edges []struct {
15+
Node struct {
16+
Oid githubql.String
17+
Message githubql.String
18+
}
19+
}
20+
} `graphql:"parents(first: $parentsNumber)"`
21+
Oid githubql.String
22+
Message githubql.String
23+
Status struct {
24+
Id githubql.String
25+
State githubql.String
26+
}
27+
}
28+
}
29+
} `graphql:"history(first: $commitsNumber)"`
30+
} `graphql:"... on Commit"`
31+
}
32+
} `graphql:"ref(qualifiedName: $branch)"`
33+
} `graphql:"repository(owner: $owner, name: $name)"`
34+
}

github/manager.go

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,93 @@ package github
22

33
import (
44
"golang.org/x/oauth2"
5-
"github.com/google/go-github/github"
65
"golang.org/x/net/context"
6+
"github.com/shurcooL/githubql"
77
)
88

99
type githubManager struct {
1010
Context context.Context
11-
Client *github.Client
11+
Client *githubql.Client
1212
}
1313

1414
func New(githubAccessToken string) (*githubManager) {
1515
ctx := context.Background()
16-
ts := oauth2.StaticTokenSource(
16+
src := oauth2.StaticTokenSource(
1717
&oauth2.Token{AccessToken: githubAccessToken},
1818
)
19-
tc := oauth2.NewClient(ctx, ts)
20-
21-
client := github.NewClient(tc)
19+
httpClient := oauth2.NewClient(ctx, src)
20+
client := githubql.NewClient(httpClient)
2221

2322
return &githubManager{Context: ctx, Client: client}
2423
}
2524

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+
}
4329

44-
for _, element := range commits {
45-
fullCommitsList = append(fullCommitsList, element)
46-
}
30+
q := &githubQuery{}
4731

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
5042
}
5143

52-
return fullCommitsList, nil
44+
return hydrateCommits(q), nil
5345
}
5446

55-
func PickFirstParentCommits(fullCommitsList []*github.RepositoryCommit) ([]*github.RepositoryCommit) {
56-
var firstParentCommits []*github.RepositoryCommit
47+
func PickFirstParentCommits(fullCommitsList []Commit) ([]Commit) {
48+
var firstParentCommits []Commit
5749
if 0 == len(fullCommitsList) {
5850
return firstParentCommits
5951
}
6052

61-
fullCommitsMap := make(map[string]*github.RepositoryCommit)
53+
fullCommitsMap := make(map[string]Commit)
6254
for _, c := range fullCommitsList {
63-
fullCommitsMap[c.GetSHA()] = c
55+
fullCommitsMap[c.SHA] = c
6456
}
6557

66-
sha := fullCommitsList[0].GetSHA() // HEAD
58+
sha := fullCommitsList[0].SHA // HEAD
6759
for {
6860
c, exists := fullCommitsMap[sha]
6961
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
7163
}
7264

7365
firstParentCommits = append(firstParentCommits, c)
7466
if 0 == len(c.Parents) {
7567
break // initial commit
7668
}
77-
sha = c.Parents[0].GetSHA()
69+
sha = c.Parents[0].SHA
7870
}
7971

8072
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

Comments
 (0)