Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: team activity #7

Merged
merged 10 commits into from
Jul 31, 2024
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ This function requires GitHub authentication with the following API scopes:
### Repositories you recently created

```
{{range recentRepos 10}}
{{range recentCreatedRepos "charmbracelet" 10}}
Name: {{.Name}}
Description: {{.Description}}
URL: {{.URL}})
Expand All @@ -118,7 +118,7 @@ Stars: {{.Stargazers}}
```

This function requires GitHub authentication with the following API scopes:
`repo:status`, `public_repo`, `read:user`.
`repo:status`, `public_repo`, `read:user` or `read:org` if you provide an organization name.

### Repositories with the most stars

Expand Down Expand Up @@ -173,7 +173,7 @@ This function requires GitHub authentication with the following API scopes:
### Forks you recently created

```
{{range recentForks 10}}
{{range recentForkedRepos "charmbracelet" 10}}
Name: {{.Name}}
Description: {{.Description}}
URL: {{.URL}})
Expand All @@ -182,7 +182,7 @@ Stars: {{.Stargazers}}
```

This function requires GitHub authentication with the following API scopes:
`repo:status`, `public_repo`, `read:user`.
`repo:status`, `public_repo`, `read:user` or `read:org` if you provide an organization name.

### Recent releases you contributed to

Expand All @@ -198,6 +198,23 @@ Published: {{humanize .LastRelease.PublishedAt}}
This function requires GitHub authentication with the following API scopes:
`repo:status`, `public_repo`, `read:user`.

### Recent pushes

```
{{range recentPushedRepos "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

```
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ func main() {
/* Github */
funcMap["recentContributions"] = recentContributions
funcMap["recentPullRequests"] = recentPullRequests
funcMap["recentForks"] = recentForks
funcMap["recentReleases"] = recentReleases
funcMap["recentRepos"] = recentRepos
funcMap["popularRepos"] = popularRepos
funcMap["recentCreatedRepos"] = recentCreatedRepos
funcMap["recentPushedRepos"] = recentPushedRepos
funcMap["recentForkedRepos"] = recentForkedRepos
funcMap["recentReleases"] = recentReleases
funcMap["followers"] = recentFollowers
funcMap["recentStars"] = recentStars
funcMap["gists"] = gists
Expand Down
89 changes: 61 additions & 28 deletions repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
Node qlRepository
}
} `graphql:"repositories(first: $count, privacy: PUBLIC, isFork: $isFork, ownerAffiliations: OWNER, orderBy: {field: CREATED_AT, direction: DESC})"`
} `graphql:"user(login:$username)"`
} `graphql:"repositoryOwner(login: $owner)"`
}

var recentReleasesQuery struct {
Expand Down Expand Up @@ -155,8 +155,6 @@
}

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

var contributions []Contribution
variables := map[string]interface{}{
"username": githubv4.String(username),
Expand Down Expand Up @@ -187,16 +185,13 @@
return contributions[i].OccurredAt.After(contributions[j].OccurredAt)
})

// fmt.Printf("Found %d contributions!\n", len(repos))
if len(contributions) > count {
return contributions[:count]
}
return contributions
}

func recentPullRequests(count int) []PullRequest {
// fmt.Printf("Finding recently created pullRequests...\n")

var pullRequests []PullRequest
variables := map[string]interface{}{
"username": githubv4.String(username),
Expand All @@ -222,18 +217,15 @@
}
}

// fmt.Printf("Found %d pullRequests!\n", len(pullRequests))
return pullRequests
}

func recentRepos(count int) []Repo {
// fmt.Printf("Finding recently created repos...\n")

func recentCreatedRepos(owner string, count int) []Repo {
var repos []Repo
variables := map[string]interface{}{
"username": githubv4.String(username),
"count": githubv4.Int(count + 1), // +1 in case we encounter the meta-repo itself
"isFork": githubv4.Boolean(false),
"owner": githubv4.String(owner),
"count": githubv4.Int(count + 1), // +1 in case we encounter the meta-repo itself
"isFork": githubv4.Boolean(false),
}
err := gitHubClient.Query(context.Background(), &recentReposQuery, variables)
if err != nil {
Expand All @@ -242,7 +234,7 @@

for _, v := range recentReposQuery.User.Repositories.Edges {
// ignore meta-repo
if string(v.Node.NameWithOwner) == fmt.Sprintf("%s/%s", username, username) {
if string(v.Node.NameWithOwner) == fmt.Sprintf("%s/%s", owner, owner) {
continue
}

Expand All @@ -252,18 +244,15 @@
}
}

// fmt.Printf("Found %d repos!\n", len(repos))
return repos
}

func recentForks(count int) []Repo {
// fmt.Printf("Finding recently created repos...\n")

func recentForkedRepos(owner string, count int) []Repo {
var repos []Repo
variables := map[string]interface{}{
"username": githubv4.String(username),
"count": githubv4.Int(count + 1), // +1 in case we encounter the meta-repo itself
"isFork": githubv4.Boolean(true),
"owner": githubv4.String(owner),
"count": githubv4.Int(count + 1), // +1 in case we encounter the meta-repo itself
"isFork": githubv4.Boolean(true),
}
err := gitHubClient.Query(context.Background(), &recentReposQuery, variables)
if err != nil {
Expand All @@ -272,7 +261,7 @@

for _, v := range recentReposQuery.User.Repositories.Edges {
// ignore meta-repo
if string(v.Node.NameWithOwner) == fmt.Sprintf("%s/%s", username, username) {
if string(v.Node.NameWithOwner) == fmt.Sprintf("%s/%s", owner, owner) {
continue
}

Expand All @@ -281,14 +270,10 @@
break
}
}

// fmt.Printf("Found %d repos!\n", len(repos))
return repos
}

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

var after *githubv4.String
var repos []Repo

Expand All @@ -302,7 +287,6 @@
panic(err)
}

// fmt.Printf("%+v\n", query)
if len(recentReleasesQuery.User.RepositoriesContributedTo.Edges) == 0 {
break
}
Expand Down Expand Up @@ -337,13 +321,62 @@
return repos[i].LastRelease.PublishedAt.After(repos[j].LastRelease.PublishedAt)
})

// fmt.Printf("Found %d repos!\n", len(repos))
if len(repos) > count {
return repos[:count]
}
return repos
}

/*
{
repositoryOwner(login: "charmbracelet") {
id
login
repositories(
first: 5
privacy: PUBLIC
orderBy: {field: PUSHED_AT, direction: DESC}
) {
edges {
node {
name
description
url
}
}
}
}
}
*/
func recentPushedRepos(owner string, count int) []Repo {
var query struct {
Owner struct {
Repositories struct {
Edges []struct {
Node qlRepository
}
} `graphql:"repositories(first: $count, privacy: PUBLIC, orderBy: {field: PUSHED_AT, direction: DESC})"`
} `graphql:"repositoryOwner(login: $owner)"`
}
var repos []Repo

Check failure on line 361 in repos.go

View workflow job for this annotation

GitHub Actions / lint-soft

Consider pre-allocating `repos` (prealloc)
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.Owner.Repositories.Edges {
repos = append(repos, repoFromQL(v.Node))
bashbunni marked this conversation as resolved.
Show resolved Hide resolved
if len(repos) == count {
break
}
}
return repos
}

func repo(owner, name string) Repo {
variables := map[string]interface{}{
"owner": githubv4.String(owner),
Expand Down
4 changes: 2 additions & 2 deletions templates/github-profile.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
{{- end}}

#### 🌱 My latest projects
{{range recentRepos 10}}
{{range recentCreatedRepos "charmbracelet" 10}}
- [{{.Name}}]({{.URL}}) - {{.Description}}
{{- end}}

#### 🍴 My recent forks
{{range recentForks 10}}
{{range recentForkedRepos "charmbracelet" 10}}
- [{{.Name}}]({{.URL}}) - {{.Description}}
{{- end}}

Expand Down
Loading