Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add repoRecentReleases #4

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
} `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 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beware though, the api might not actually reply with that many repos...

probably better to move this line a couple of lines down and do

releases := make([]Release, len(repoRecentReleasesQuery.Repository.Releases.Nodes))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beware though, the api might not actually reply with that many repos...
You mean if they choose to ignore that value on their end? so still worth checking 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, if the user ask for 10 releases, but the repo only have 3, the array will still have length=10

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah of course! Thanks Carlos :)


variables := map[string]interface{}{
"owner": githubv4.String(owner),
"name": githubv4.String(name),
aymanbagabas marked this conversation as resolved.
Show resolved Hide resolved
"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
Loading