Skip to content

Commit

Permalink
feat: add recent pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal authored and muesli committed May 17, 2021
1 parent 2b0d2b8 commit 49652d6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ Occurred: {{humanize .OccurredAt}}

This function requires GitHub authentication!

### Your recent pull requests

```
{{range recentPullRequests 10}}
Title: {{.Title}}
URL: {{.URL}}
State: {{.State}}
CreatedAt: {{humanize .CreatedAt}}
Repository name: {{.Repo.Name}}
Repository description: {{.Repo.Description}}
Repository URL: {{.Repo.URL}}
{{end}}
```

This function requires GitHub authentication!

### Repositories you recently created

```
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ github.com/shurcooL/githubv4 v0.0.0-20191127044304-8f68eb5628d0/go.mod h1:hAF0iL
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f h1:tygelZueB1EtXkPI6mQ4o9DQ0+FKW41hTbunoXZCTqk=
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func main() {
tpl, err := template.New("tpl").Funcs(template.FuncMap{
/* GitHub */
"recentContributions": recentContributions,
"recentPullRequests": recentPullRequests,
"recentRepos": recentRepos,
"recentReleases": recentReleases,
"followers": recentFollowers,
Expand Down
45 changes: 45 additions & 0 deletions repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ var recentContributionsQuery struct {
} `graphql:"user(login:$username)"`
}

var recentPullRequestsQuery struct {
User struct {
Login githubv4.String
PullRequests struct {
TotalCount githubv4.Int
Edges []struct {
Cursor githubv4.String
Node QLPullRequest
}
} `graphql:"pullRequests(first: $count, orderBy: {field: CREATED_AT, direction: DESC})"`
} `graphql:"user(login:$username)"`
}

var recentReposQuery struct {
User struct {
Login githubv4.String
Expand Down Expand Up @@ -96,6 +109,38 @@ func recentContributions(count int) []Contribution {
return contributions
}

func recentPullRequests(count int) []PullRequest {
// fmt.Printf("Finding recently created pullRequests...\n")

var pullRequests []PullRequest
variables := map[string]interface{}{
"username": githubv4.String(username),
"count": githubv4.Int(count + 1), // +1 in case we encounter the meta-repo itself
}
err := gitHubClient.Query(context.Background(), &recentPullRequestsQuery, variables)
if err != nil {
panic(err)
}

for _, v := range recentPullRequestsQuery.User.PullRequests.Edges {
// ignore meta-repo
if string(v.Node.Repository.NameWithOwner) == fmt.Sprintf("%s/%s", username, username) {
continue
}
if v.Node.Repository.IsPrivate {
continue
}

pullRequests = append(pullRequests, PullRequestFromQL(v.Node))
if len(pullRequests) == count {
break
}
}

// fmt.Printf("Found %d pullRequests!\n", len(pullRequests))
return pullRequests
}

func recentRepos(count int) []Repo {
// fmt.Printf("Finding recently created repos...\n")

Expand Down
26 changes: 26 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ type Gist struct {
CreatedAt time.Time
}

type PullRequest struct {
Title string
URL string
State string
CreatedAt time.Time
Repo Repo
}

type Release struct {
Name string
TagName string
Expand Down Expand Up @@ -52,6 +60,14 @@ type QLGist struct {
CreatedAt githubv4.DateTime
}

type QLPullRequest struct {
URL githubv4.String
Title githubv4.String
State githubv4.PullRequestState
CreatedAt githubv4.DateTime
Repository QLRepository
}

type QLRelease struct {
Nodes []struct {
Name githubv4.String
Expand Down Expand Up @@ -89,6 +105,16 @@ func GistFromQL(gist QLGist) Gist {
}
}

func PullRequestFromQL(pullRequest QLPullRequest) PullRequest {
return PullRequest{
Title: string(pullRequest.Title),
URL: string(pullRequest.URL),
State: string(pullRequest.State),
CreatedAt: pullRequest.CreatedAt.Time,
Repo: RepoFromQL(pullRequest.Repository),
}
}

func ReleaseFromQL(release QLRelease) Release {
return Release{
Name: string(release.Nodes[0].Name),
Expand Down

0 comments on commit 49652d6

Please sign in to comment.