From e5530b815ddd535651148c53bd0e2e67ce5e9b51 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Tue, 16 Jul 2024 11:39:28 -0700 Subject: [PATCH] feat: fetch recently pushed repos for an org --- README.md | 17 +++++++++++++++++ main.go | 1 + repos.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/README.md b/README.md index e078542..8e88a37 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,23 @@ Published: {{humanize .LastRelease.PublishedAt}} This function requires GitHub authentication with the following API scopes: `repo:status`, `public_repo`, `read:user`. +### Recent pushes in an organization + +``` +{{range orgRecentPushes "charmbracelet" 10}} +Name: {{.Name}} +URL: {{.URL}} +Description: {{.Description}} +Stars: {{.Stargazers}} +{{end}} +``` + +This function requires GitHub authentication with the following API scopes: +`public_repo`, `read:org`. + +> [!TIP] +> Use `{{with repo "charmbracelet .Name"}}` to create a pipeline that grabs additional information about the repo including releases. + ### Your published gists ``` diff --git a/main.go b/main.go index 0995fed..a165b7a 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,7 @@ func main() { funcMap["recentRepos"] = recentRepos funcMap["recentForks"] = recentForks funcMap["recentReleases"] = recentReleases + funcMap["orgRecentPushes"] = orgRecentPushes funcMap["followers"] = recentFollowers funcMap["recentStars"] = recentStars funcMap["gists"] = gists diff --git a/repos.go b/repos.go index 6a0472d..ef11440 100644 --- a/repos.go +++ b/repos.go @@ -282,6 +282,62 @@ func recentReleases(count int) []Repo { return repos } +/*{ +* organization(login: "charmbracelet") { +* login +* repositories( +* first: 5 +* privacy: PUBLIC +* orderBy: {field: PUSHED_AT, direction: DESC} +* ) { +* totalCount +* edges { +* cursor +* node { +* nameWithOwner +* pushedAt +* latestRelease { +* tagName +* createdAt +* } +* } +* } +* } +* } +*} + * */ +func orgRecentPushes(owner string, count int) []Repo { + var query struct { + Organization struct { + Repositories struct { + Edges []struct { + Node qlRepository + } + } `graphql:"repositories(first: $count, privacy: PUBLIC, orderBy: {field: STARGAZERS, direction: DESC})"` + } `graphql:"organization(login: $owner)"` + } + fmt.Printf("Finding repos with recent pushes in the %s org\n", owner) + var repos []Repo + variables := map[string]interface{}{ + "count": githubv4.Int(count), + "owner": githubv4.String(owner), + } + err := gitHubClient.Query(context.Background(), &query, variables) + if err != nil { + panic(err) + } + + for _, v := range query.Organization.Repositories.Edges { + // ignore meta-repo + repos = append(repos, repoFromQL(v.Node)) + if len(repos) == count { + break + } + } + fmt.Printf("Found %d repos!\n", len(repos)) + return repos +} + func repo(owner, name string) Repo { variables := map[string]interface{}{ "owner": githubv4.String(owner),