Skip to content

Commit

Permalink
feat: repoRecentReleases and repoRecentPreReleases
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Aug 9, 2023
1 parent d838b6e commit b9dc303
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 16 deletions.
27 changes: 25 additions & 2 deletions README.md
Expand Up @@ -165,6 +165,29 @@ Published: {{humanize .LastRelease.PublishedAt}}
This function requires GitHub authentication with the following API scopes:
`repo:status`, `public_repo`, `read:user`.

### Recent releases to a given repository

```
{{range recentRepoReleases "muesli" "markscribe" 10}}
Name: {{.Name}}
Git Tag: {{.TagName}}
URL: {{.URL}}
Published: {{humanize .PublishedAt}}
{{end}}
```

### Recent pre-releases to a given repository

```
{{range recentRepoPreReleases "muesli" "markscribe" 10}}
Name: {{.Name}}
Git Tag: {{.TagName}}
URL: {{.URL}}
Published: {{humanize .CreatedAt}}
{{end}}
```

### Your published gists

```
Expand Down Expand Up @@ -278,8 +301,8 @@ You also need to set your GoodReads user ID in your secrets as `GOODREADS_USER_I

## FAQ

Q: That's awesome, but can you expose more APIs and data?
Q: That's awesome, but can you expose more APIs and data?
A: Of course, just open a new issue and let me know what you'd like to do with markscribe!

Q: That's awesome, but I don't have my own server to run this on. Can you help?
Q: That's awesome, but I don't have my own server to run this on. Can you help?
A: Check out [readme-scribe](https://github.com/muesli/readme-scribe/), a GitHub Action that runs markscribe for you!
22 changes: 12 additions & 10 deletions main.go
Expand Up @@ -41,16 +41,18 @@ func main() {

tpl, err := template.New("tpl").Funcs(template.FuncMap{
/* GitHub */
"recentContributions": recentContributions,
"recentPullRequests": recentPullRequests,
"recentRepos": recentRepos,
"recentForks": recentForks,
"recentReleases": recentReleases,
"followers": recentFollowers,
"recentStars": recentStars,
"gists": gists,
"sponsors": sponsors,
"repo": repo,
"recentContributions": recentContributions,
"recentPullRequests": recentPullRequests,
"recentRepos": recentRepos,
"recentForks": recentForks,
"recentReleases": recentReleases,
"repoRecentReleases": repoRecentReleases,
"repoRecentPreReleases": repoRecentPreReleases,
"followers": recentFollowers,
"recentStars": recentStars,
"gists": gists,
"sponsors": sponsors,
"repo": repo,
/* RSS */
"rss": rssFeed,
/* GoodReads */
Expand Down
72 changes: 72 additions & 0 deletions repos.go
Expand Up @@ -69,6 +69,12 @@ var recentReleasesQuery struct {
} `graphql:"user(login:$username)"`
}

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

var repoQuery struct {
Repository struct {
Description githubv4.String
Expand Down Expand Up @@ -214,6 +220,72 @@ func recentForks(count int) []Repo {
return repos
}

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

Check failure on line 224 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),
}
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),
})
}

if len(releases) > count {
return releases[:count]
}
return releases
}

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

Check failure on line 257 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),
}
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),
})
}

if len(releases) > count {
return releases[:count]
}
return releases
}

func recentReleases(count int) []Repo {
// fmt.Printf("Finding recent releases...\n")

Expand Down
13 changes: 9 additions & 4 deletions types.go
Expand Up @@ -37,10 +37,13 @@ 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
}

// Repo represents a git repo.
Expand Down Expand Up @@ -87,7 +90,9 @@ type qlRelease struct {
Name githubv4.String
TagName githubv4.String
PublishedAt githubv4.DateTime
CreatedAt githubv4.DateTime
URL githubv4.String
IsLatest githubv4.Boolean
IsPrerelease githubv4.Boolean
IsDraft githubv4.Boolean
}
Expand Down

0 comments on commit b9dc303

Please sign in to comment.