Skip to content

Commit fadffa2

Browse files
feat: add repoRecentReleases (#4)
* feat: add repoRecentReleases Adds a `repoRecentReleases` command that fetches the latest releases for a given repository. Reference: muesli#84 * Update repos.go Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * Update repos.go Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * Update repos.go Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> --------- Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
1 parent 2949194 commit fadffa2

4 files changed

Lines changed: 64 additions & 4 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ Last Release: {{humanize .LastRelease.PublishedAt}}
134134
{{end}}
135135
```
136136

137+
### Recent releases to a given repository
138+
139+
```
140+
{{range recentRepoReleases "charmbracelet" "markscribe" 10}}
141+
Name: {{.Name}}
142+
Git Tag: {{.TagName}}
143+
URL: {{.URL}}
144+
Published: {{humanize .PublishedAt}}
145+
CreatedAt: {{humanize .CreatedAt}}
146+
IsPreRelease: {{.IsPreRelease}}
147+
IsDraft: {{.IsDraft}}
148+
IsLatest: {{.IsLatest}}
149+
{{end}}
150+
```
151+
137152
This function requires GitHub authentication with the following API scopes:
138153
`repo:status`, `public_repo`, `read:user`.
139154

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func main() {
4949
funcMap["gists"] = gists
5050
funcMap["sponsors"] = sponsors
5151
funcMap["repo"] = repo
52+
funcMap["repoRecentReleases"] = repoRecentReleases
5253
/* RSS */
5354
funcMap["rss"] = rssFeed
5455
/* GoodReads */

repos.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ var repoQuery struct {
8686
} `graphql:"repository(owner:$owner, name:$name)"`
8787
}
8888

89+
var repoRecentReleasesQuery struct {
90+
Repository struct {
91+
Releases qlRelease `graphql:"releases(first: $count, orderBy: {field: CREATED_AT, direction: DESC})"`
92+
} `graphql:"repository(name: $name, owner: $owner)"`
93+
}
94+
8995
func recentContributions(count int) []Contribution {
9096
// fmt.Printf("Finding recent contributions...\n")
9197

@@ -298,6 +304,38 @@ func repo(owner, name string) Repo {
298304
}
299305
}
300306

307+
func repoRecentReleases(owner, name string, count int) []Release {
308+
var releases []Release
309+
310+
variables := map[string]interface{}{
311+
"owner": githubv4.String(owner),
312+
"name": githubv4.String(name),
313+
"count": githubv4.Int(count),
314+
}
315+
err := gitHubClient.Query(context.Background(), &repoRecentReleasesQuery, variables)
316+
if err != nil {
317+
panic(err)
318+
}
319+
320+
for _, rel := range repoRecentReleasesQuery.Repository.Releases.Nodes {
321+
if bool(rel.IsPrerelease) {
322+
continue
323+
}
324+
releases = append(releases, Release{
325+
Name: string(rel.Name),
326+
TagName: string(rel.TagName),
327+
PublishedAt: rel.PublishedAt.Time,
328+
CreatedAt: rel.CreatedAt.Time,
329+
URL: string(rel.URL),
330+
IsLatest: bool(rel.IsLatest),
331+
IsPreRelease: bool(rel.IsPrerelease),
332+
IsDraft: bool(rel.IsDraft),
333+
})
334+
}
335+
336+
return releases
337+
}
338+
301339
/*
302340
{
303341
user(login: "muesli") {

types.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ type PullRequest struct {
3737

3838
// Release represents a release.
3939
type Release struct {
40-
Name string
41-
TagName string
42-
PublishedAt time.Time
43-
URL string
40+
Name string
41+
TagName string
42+
PublishedAt time.Time
43+
CreatedAt time.Time
44+
URL string
45+
IsLatest bool
46+
IsPreRelease bool
47+
IsDraft bool
4448
}
4549

4650
// Repo represents a git repo.
@@ -89,8 +93,10 @@ type qlRelease struct {
8993
Name githubv4.String
9094
TagName githubv4.String
9195
PublishedAt githubv4.DateTime
96+
CreatedAt githubv4.DateTime
9297
URL githubv4.String
9398
IsPrerelease githubv4.Boolean
99+
IsLatest githubv4.Boolean
94100
IsDraft githubv4.Boolean
95101
}
96102
}

0 commit comments

Comments
 (0)