Skip to content

Commit

Permalink
feat: add repoRecentReleases (#4)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
aymanbagabas and caarlos0 authored Jul 16, 2024
1 parent 2949194 commit fadffa2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ Last Release: {{humanize .LastRelease.PublishedAt}}
{{end}}
```

### Recent releases to a given repository

```
{{range recentRepoReleases "charmbracelet" "markscribe" 10}}
Name: {{.Name}}
Git Tag: {{.TagName}}
URL: {{.URL}}
Published: {{humanize .PublishedAt}}
CreatedAt: {{humanize .CreatedAt}}
IsPreRelease: {{.IsPreRelease}}
IsDraft: {{.IsDraft}}
IsLatest: {{.IsLatest}}
{{end}}
```

This function requires GitHub authentication with the following API scopes:
`repo:status`, `public_repo`, `read:user`.

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func main() {
funcMap["gists"] = gists
funcMap["sponsors"] = sponsors
funcMap["repo"] = repo
funcMap["repoRecentReleases"] = repoRecentReleases
/* RSS */
funcMap["rss"] = rssFeed
/* GoodReads */
Expand Down
38 changes: 38 additions & 0 deletions repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ var repoQuery struct {
} `graphql:"repository(owner:$owner, name:$name)"`
}

var repoRecentReleasesQuery struct {
Repository struct {
Releases qlRelease `graphql:"releases(first: $count, orderBy: {field: CREATED_AT, direction: DESC})"`
} `graphql:"repository(name: $name, owner: $owner)"`
}

func recentContributions(count int) []Contribution {
// fmt.Printf("Finding recent contributions...\n")

Expand Down Expand Up @@ -298,6 +304,38 @@ func repo(owner, name string) Repo {
}
}

func repoRecentReleases(owner, name string, count int) []Release {
var releases []Release

Check failure on line 308 in repos.go

View workflow job for this annotation

GitHub Actions / lint-soft

Consider pre-allocating `releases` (prealloc)

variables := map[string]interface{}{
"owner": githubv4.String(owner),
"name": githubv4.String(name),
"count": githubv4.Int(count),
}
err := gitHubClient.Query(context.Background(), &repoRecentReleasesQuery, variables)
if err != nil {
panic(err)
}

for _, rel := range repoRecentReleasesQuery.Repository.Releases.Nodes {
if bool(rel.IsPrerelease) {
continue
}
releases = append(releases, Release{
Name: string(rel.Name),
TagName: string(rel.TagName),
PublishedAt: rel.PublishedAt.Time,
CreatedAt: rel.CreatedAt.Time,
URL: string(rel.URL),
IsLatest: bool(rel.IsLatest),
IsPreRelease: bool(rel.IsPrerelease),
IsDraft: bool(rel.IsDraft),
})
}

return releases
}

/*
{
user(login: "muesli") {
Expand Down
14 changes: 10 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ type PullRequest struct {

// Release represents a release.
type Release struct {
Name string
TagName string
PublishedAt time.Time
URL string
Name string
TagName string
PublishedAt time.Time
CreatedAt time.Time
URL string
IsLatest bool
IsPreRelease bool
IsDraft bool
}

// Repo represents a git repo.
Expand Down Expand Up @@ -89,8 +93,10 @@ type qlRelease struct {
Name githubv4.String
TagName githubv4.String
PublishedAt githubv4.DateTime
CreatedAt githubv4.DateTime
URL githubv4.String
IsPrerelease githubv4.Boolean
IsLatest githubv4.Boolean
IsDraft githubv4.Boolean
}
}
Expand Down

0 comments on commit fadffa2

Please # to comment.