Skip to content

Commit efcbc74

Browse files
authored
feat: add push activity for repos (#7)
* feat: fetch recently pushed repos for an org * fix: fix orgRecentPushes query * feat: support users and organizations for recently pushed repos query * chore: clean up example graphql query * docs: update README * feat: add recentCreatedRepos * feat: support users and orgs for recent forks + created repos * fix: declare owner variable * refactor: rename recentForks to recentForkedRepos to match others * chore: remove commented code
1 parent 50643a9 commit efcbc74

4 files changed

Lines changed: 88 additions & 37 deletions

File tree

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ This function requires GitHub authentication with the following API scopes:
109109
### Repositories you recently created
110110

111111
```
112-
{{range recentRepos 10}}
112+
{{range recentCreatedRepos "charmbracelet" 10}}
113113
Name: {{.Name}}
114114
Description: {{.Description}}
115115
URL: {{.URL}})
@@ -118,7 +118,7 @@ Stars: {{.Stargazers}}
118118
```
119119

120120
This function requires GitHub authentication with the following API scopes:
121-
`repo:status`, `public_repo`, `read:user`.
121+
`repo:status`, `public_repo`, `read:user` or `read:org` if you provide an organization name.
122122

123123
### Repositories with the most stars
124124

@@ -173,7 +173,7 @@ This function requires GitHub authentication with the following API scopes:
173173
### Forks you recently created
174174

175175
```
176-
{{range recentForks 10}}
176+
{{range recentForkedRepos "charmbracelet" 10}}
177177
Name: {{.Name}}
178178
Description: {{.Description}}
179179
URL: {{.URL}})
@@ -182,7 +182,7 @@ Stars: {{.Stargazers}}
182182
```
183183

184184
This function requires GitHub authentication with the following API scopes:
185-
`repo:status`, `public_repo`, `read:user`.
185+
`repo:status`, `public_repo`, `read:user` or `read:org` if you provide an organization name.
186186

187187
### Recent releases you contributed to
188188

@@ -198,6 +198,23 @@ Published: {{humanize .LastRelease.PublishedAt}}
198198
This function requires GitHub authentication with the following API scopes:
199199
`repo:status`, `public_repo`, `read:user`.
200200

201+
### Recent pushes
202+
203+
```
204+
{{range recentPushedRepos "charmbracelet" 10}}
205+
Name: {{.Name}}
206+
URL: {{.URL}}
207+
Description: {{.Description}}
208+
Stars: {{.Stargazers}}
209+
{{end}}
210+
```
211+
212+
This function requires GitHub authentication with the following API scopes:
213+
`public_repo`, `read:org`.
214+
215+
> [!TIP]
216+
> Use `{{with repo "charmbracelet .Name"}}` to create a pipeline that grabs additional information about the repo including releases.
217+
201218
### Your published gists
202219

203220
```

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ func main() {
4141
/* Github */
4242
funcMap["recentContributions"] = recentContributions
4343
funcMap["recentPullRequests"] = recentPullRequests
44-
funcMap["recentForks"] = recentForks
45-
funcMap["recentReleases"] = recentReleases
46-
funcMap["recentRepos"] = recentRepos
4744
funcMap["popularRepos"] = popularRepos
45+
funcMap["recentCreatedRepos"] = recentCreatedRepos
46+
funcMap["recentPushedRepos"] = recentPushedRepos
47+
funcMap["recentForkedRepos"] = recentForkedRepos
48+
funcMap["recentReleases"] = recentReleases
4849
funcMap["followers"] = recentFollowers
4950
funcMap["recentStars"] = recentStars
5051
funcMap["gists"] = gists

repos.go

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var recentReposQuery struct {
5050
Node qlRepository
5151
}
5252
} `graphql:"repositories(first: $count, privacy: PUBLIC, isFork: $isFork, ownerAffiliations: OWNER, orderBy: {field: CREATED_AT, direction: DESC})"`
53-
} `graphql:"user(login:$username)"`
53+
} `graphql:"repositoryOwner(login: $owner)"`
5454
}
5555

5656
var recentReleasesQuery struct {
@@ -155,8 +155,6 @@ var repoRecentReleasesQuery struct {
155155
}
156156

157157
func recentContributions(count int) []Contribution {
158-
// fmt.Printf("Finding recent contributions...\n")
159-
160158
var contributions []Contribution
161159
variables := map[string]interface{}{
162160
"username": githubv4.String(username),
@@ -187,16 +185,13 @@ func recentContributions(count int) []Contribution {
187185
return contributions[i].OccurredAt.After(contributions[j].OccurredAt)
188186
})
189187

190-
// fmt.Printf("Found %d contributions!\n", len(repos))
191188
if len(contributions) > count {
192189
return contributions[:count]
193190
}
194191
return contributions
195192
}
196193

197194
func recentPullRequests(count int) []PullRequest {
198-
// fmt.Printf("Finding recently created pullRequests...\n")
199-
200195
var pullRequests []PullRequest
201196
variables := map[string]interface{}{
202197
"username": githubv4.String(username),
@@ -222,18 +217,15 @@ func recentPullRequests(count int) []PullRequest {
222217
}
223218
}
224219

225-
// fmt.Printf("Found %d pullRequests!\n", len(pullRequests))
226220
return pullRequests
227221
}
228222

229-
func recentRepos(count int) []Repo {
230-
// fmt.Printf("Finding recently created repos...\n")
231-
223+
func recentCreatedRepos(owner string, count int) []Repo {
232224
var repos []Repo
233225
variables := map[string]interface{}{
234-
"username": githubv4.String(username),
235-
"count": githubv4.Int(count + 1), // +1 in case we encounter the meta-repo itself
236-
"isFork": githubv4.Boolean(false),
226+
"owner": githubv4.String(owner),
227+
"count": githubv4.Int(count + 1), // +1 in case we encounter the meta-repo itself
228+
"isFork": githubv4.Boolean(false),
237229
}
238230
err := gitHubClient.Query(context.Background(), &recentReposQuery, variables)
239231
if err != nil {
@@ -242,7 +234,7 @@ func recentRepos(count int) []Repo {
242234

243235
for _, v := range recentReposQuery.User.Repositories.Edges {
244236
// ignore meta-repo
245-
if string(v.Node.NameWithOwner) == fmt.Sprintf("%s/%s", username, username) {
237+
if string(v.Node.NameWithOwner) == fmt.Sprintf("%s/%s", owner, owner) {
246238
continue
247239
}
248240

@@ -252,18 +244,15 @@ func recentRepos(count int) []Repo {
252244
}
253245
}
254246

255-
// fmt.Printf("Found %d repos!\n", len(repos))
256247
return repos
257248
}
258249

259-
func recentForks(count int) []Repo {
260-
// fmt.Printf("Finding recently created repos...\n")
261-
250+
func recentForkedRepos(owner string, count int) []Repo {
262251
var repos []Repo
263252
variables := map[string]interface{}{
264-
"username": githubv4.String(username),
265-
"count": githubv4.Int(count + 1), // +1 in case we encounter the meta-repo itself
266-
"isFork": githubv4.Boolean(true),
253+
"owner": githubv4.String(owner),
254+
"count": githubv4.Int(count + 1), // +1 in case we encounter the meta-repo itself
255+
"isFork": githubv4.Boolean(true),
267256
}
268257
err := gitHubClient.Query(context.Background(), &recentReposQuery, variables)
269258
if err != nil {
@@ -272,7 +261,7 @@ func recentForks(count int) []Repo {
272261

273262
for _, v := range recentReposQuery.User.Repositories.Edges {
274263
// ignore meta-repo
275-
if string(v.Node.NameWithOwner) == fmt.Sprintf("%s/%s", username, username) {
264+
if string(v.Node.NameWithOwner) == fmt.Sprintf("%s/%s", owner, owner) {
276265
continue
277266
}
278267

@@ -281,14 +270,10 @@ func recentForks(count int) []Repo {
281270
break
282271
}
283272
}
284-
285-
// fmt.Printf("Found %d repos!\n", len(repos))
286273
return repos
287274
}
288275

289276
func recentReleases(count int) []Repo {
290-
// fmt.Printf("Finding recent releases...\n")
291-
292277
var after *githubv4.String
293278
var repos []Repo
294279

@@ -302,7 +287,6 @@ func recentReleases(count int) []Repo {
302287
panic(err)
303288
}
304289

305-
// fmt.Printf("%+v\n", query)
306290
if len(recentReleasesQuery.User.RepositoriesContributedTo.Edges) == 0 {
307291
break
308292
}
@@ -337,13 +321,62 @@ func recentReleases(count int) []Repo {
337321
return repos[i].LastRelease.PublishedAt.After(repos[j].LastRelease.PublishedAt)
338322
})
339323

340-
// fmt.Printf("Found %d repos!\n", len(repos))
341324
if len(repos) > count {
342325
return repos[:count]
343326
}
344327
return repos
345328
}
346329

330+
/*
331+
{
332+
repositoryOwner(login: "charmbracelet") {
333+
id
334+
login
335+
repositories(
336+
first: 5
337+
privacy: PUBLIC
338+
orderBy: {field: PUSHED_AT, direction: DESC}
339+
) {
340+
edges {
341+
node {
342+
name
343+
description
344+
url
345+
}
346+
}
347+
}
348+
}
349+
}
350+
*/
351+
func recentPushedRepos(owner string, count int) []Repo {
352+
var query struct {
353+
Owner struct {
354+
Repositories struct {
355+
Edges []struct {
356+
Node qlRepository
357+
}
358+
} `graphql:"repositories(first: $count, privacy: PUBLIC, orderBy: {field: PUSHED_AT, direction: DESC})"`
359+
} `graphql:"repositoryOwner(login: $owner)"`
360+
}
361+
var repos []Repo
362+
variables := map[string]interface{}{
363+
"count": githubv4.Int(count),
364+
"owner": githubv4.String(owner),
365+
}
366+
err := gitHubClient.Query(context.Background(), &query, variables)
367+
if err != nil {
368+
panic(err)
369+
}
370+
371+
for _, v := range query.Owner.Repositories.Edges {
372+
repos = append(repos, repoFromQL(v.Node))
373+
if len(repos) == count {
374+
break
375+
}
376+
}
377+
return repos
378+
}
379+
347380
func repo(owner, name string) Repo {
348381
variables := map[string]interface{}{
349382
"owner": githubv4.String(owner),

templates/github-profile.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{{- end}}
77

88
#### 🌱 My latest projects
9-
{{range recentRepos 10}}
9+
{{range recentCreatedRepos "charmbracelet" 10}}
1010
- [{{.Name}}]({{.URL}}) - {{.Description}}
1111
{{- end}}
1212

1313
#### 🍴 My recent forks
14-
{{range recentForks 10}}
14+
{{range recentForkedRepos "charmbracelet" 10}}
1515
- [{{.Name}}]({{.URL}}) - {{.Description}}
1616
{{- end}}
1717

0 commit comments

Comments
 (0)