-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit addresses a couple of different issues with this package. Previously, everything was lumped in to `client.go`. This was fine initially but it became harder to manage as the codebase grew. Domains are now separated in by file. Future work is probably needed to subdivide each domain in to it's own package to prevent leakage. Secondly, the methods for Tags and Pull were a bit scrappy and not very efficient. This was a left over from the "get it done" phase of the project. These instances have now been resolved and I think the implementation is closed to O(n) (maybe.. either way it's less garbage than it was before).
- Loading branch information
Showing
5 changed files
with
187 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package githubclient | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/shurcooL/githubv4" | ||
) | ||
|
||
type PullRequestLabel struct { | ||
Name string | ||
} | ||
|
||
type PullRequestEdge struct { | ||
Node struct { | ||
PullRequest struct { | ||
Number int | ||
Title string | ||
Author struct { | ||
Login string | ||
} | ||
Labels struct { | ||
Nodes []PullRequestLabel | ||
} `graphql:"labels(first: 100)"` | ||
} `graphql:"... on PullRequest"` | ||
} | ||
} | ||
|
||
type PullRequestSearchQuery struct { | ||
Search struct { | ||
Edges []PullRequestEdge | ||
PageInfo struct { | ||
EndCursor githubv4.String | ||
HasNextPage bool | ||
} | ||
} `graphql:"search(query: $query, type: ISSUE, first: 100, after: $cursor)"` | ||
} | ||
|
||
type PullRequest struct { | ||
Number int | ||
Title string | ||
User string | ||
Labels []PullRequestLabel | ||
} | ||
|
||
func (client *githubClient) GetPullRequestsBetweenDates(fromDate, toDate time.Time) ([]PullRequest, error) { | ||
variables := map[string]interface{}{ | ||
"query": githubv4.String( | ||
fmt.Sprintf( | ||
`repo:%s/%s is:pr is:merged merged:%s..%s`, | ||
client.repoContext.owner, | ||
client.repoContext.name, | ||
fromDate.Local().Format(time.RFC3339), | ||
toDate.Local().Format(time.RFC3339), | ||
), | ||
), | ||
"cursor": (*githubv4.String)(nil), | ||
} | ||
|
||
var pullRequestSearchQuery PullRequestSearchQuery | ||
var pullRequests []PullRequest | ||
var edges []PullRequestEdge | ||
|
||
for { | ||
err := client.base.Query(client.httpContext, &pullRequestSearchQuery, variables) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
edges = append(edges, pullRequestSearchQuery.Search.Edges...) | ||
|
||
if !pullRequestSearchQuery.Search.PageInfo.HasNextPage { | ||
break | ||
} | ||
variables["cursor"] = pullRequestSearchQuery.Search.PageInfo.EndCursor | ||
} | ||
|
||
for _, edge := range edges { | ||
pullRequests = append(pullRequests, PullRequest{ | ||
Number: edge.Node.PullRequest.Number, | ||
Title: edge.Node.PullRequest.Title, | ||
User: edge.Node.PullRequest.Author.Login, | ||
Labels: edge.Node.PullRequest.Labels.Nodes, | ||
}) | ||
} | ||
|
||
return pullRequests, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package githubclient | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"time" | ||
|
||
"github.com/shurcooL/githubv4" | ||
) | ||
|
||
type RefNode struct { | ||
Name string | ||
Target struct { | ||
TypeName string `graphql:"__typename"` | ||
Tag struct { | ||
Oid string | ||
Tagger struct { | ||
Date time.Time | ||
} | ||
} `graphql:"... on Tag"` | ||
Commit struct { | ||
Oid string | ||
Committer struct { | ||
Date time.Time | ||
} | ||
} `graphql:"... on Commit"` | ||
} | ||
} | ||
|
||
type TagQuery struct { | ||
Repository struct { | ||
Refs struct { | ||
Nodes []RefNode | ||
PageInfo struct { | ||
EndCursor githubv4.String | ||
HasNextPage bool | ||
} | ||
} `graphql:"refs(refPrefix: \"refs/tags/\", last: 100, after: $cursor)"` | ||
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"` | ||
} | ||
|
||
type Tag struct { | ||
Name string | ||
Sha string | ||
Date time.Time | ||
} | ||
|
||
func (client *githubClient) GetTags() ([]Tag, error) { | ||
variables := map[string]interface{}{ | ||
"repositoryOwner": githubv4.String(client.repoContext.owner), | ||
"repositoryName": githubv4.String(client.repoContext.name), | ||
"cursor": (*githubv4.String)(nil), | ||
} | ||
|
||
var tags []Tag | ||
var tagQuery TagQuery | ||
var nodes []RefNode | ||
|
||
for { | ||
err := client.base.Query(client.httpContext, &tagQuery, variables) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting tags: %w", err) | ||
} | ||
|
||
nodes = append(nodes, tagQuery.Repository.Refs.Nodes...) | ||
|
||
if !tagQuery.Repository.Refs.PageInfo.HasNextPage { | ||
break | ||
} | ||
|
||
variables["cursor"] = tagQuery.Repository.Refs.PageInfo.EndCursor | ||
} | ||
|
||
for _, node := range nodes { | ||
switch node.Target.TypeName { | ||
case "Tag": | ||
tags = append(tags, Tag{ | ||
Name: node.Name, | ||
Sha: node.Target.Tag.Oid, | ||
Date: node.Target.Tag.Tagger.Date, | ||
}) | ||
case "Commit": | ||
tags = append(tags, Tag{ | ||
Name: node.Name, | ||
Sha: node.Target.Commit.Oid, | ||
Date: node.Target.Commit.Committer.Date, | ||
}) | ||
} | ||
} | ||
|
||
sort.Slice(tags, func(i, j int) bool { | ||
return tags[i].Date.After(tags[j].Date) | ||
}) | ||
|
||
return tags, nil | ||
} |