Skip to content

Commit

Permalink
Bring back update-contributors but in go (#3512)
Browse files Browse the repository at this point in the history
I think it is a nice to have this appreciation, but we haven't updated
in a while, as the nodejs of the old tools no longer maintained.

Just threw some go together quickly.
  • Loading branch information
simonswine authored Aug 23, 2024
1 parent 58e9e8b commit d08e108
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/update-contributors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Update Contributors in README

on:
push:
branches: [main]

jobs:
update-contributors:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Update contributors
run: make update-contributors

- uses: stefanzweifel/git-auto-commit-action@v5
with:
# these are credentials for https://github.com/pyroscopebot
token: ${{ secrets.BOT_GITHUB_TOKEN }}
commit_user_name: Pyroscope Bot
commit_user_email: dmitry+bot@pyroscope.io
commit_author: 'Pyroscope Bot <dmitry+bot@pyroscope.io>'
commit_message: 'docs: updates the list of contributors in README'
file_pattern: README.md
env:
GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ go/lint: $(BIN)/golangci-lint
$(BIN)/golangci-lint run
$(GO) vet ./...

.PHONY: update-contributors
update-contributors: ## Update the contributors in README.md
go run ./tools/update-contributors

.PHONY: go/mod
go/mod: $(foreach P,$(GO_MOD_PATHS),go/mod_tidy/$P)

Expand Down
136 changes: 136 additions & 0 deletions tools/update-contributors/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
)

type contributor struct {
Login string `json:"login"`
AvatarURL string `json:"avatar_url"`
HTMLURL string `json:"html_url"`
}

func fetchContributors(ctx context.Context, owner, repo string) ([]contributor, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/contributors?per_page=200", owner, repo)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode)
}

var contributors []contributor
if err := json.NewDecoder(resp.Body).Decode(&contributors); err != nil {
return nil, err
}

return contributors, nil
}

func generateContributors() (string, error) {
ctx := context.Background()
contributors, err := fetchContributors(ctx, "grafana", "pyroscope")
if err != nil {
return "", err
}

sb := strings.Builder{}

limit := 9 * 7

for _, c := range contributors {
// filter bots
if strings.HasSuffix(c.Login, "[bot]") || c.Login == "pyroscopebot" {
continue
}
sb.WriteString(`<a href="`)
sb.WriteString(c.HTMLURL)
sb.WriteString(`"><img src="`)
sb.WriteString(c.AvatarURL)
sb.WriteString(`" title="`)
sb.WriteString(c.Login)
sb.WriteString(`" width="80" height="80"></a>`)
sb.WriteByte('\n')
limit--
if limit == 0 {
break
}
}

return sb.String(), nil
}

const (
marker = "[//]: contributor-faces"
readmeFile = "README.md"
)

func replaceReadme() error {
b, err := os.ReadFile(readmeFile)
if err != nil {
return err
}
s := string(b)

start := strings.Index(s, marker)
if start == -1 {
return fmt.Errorf("could not find marker %q", marker)
}
start += len(marker) + 1

end := strings.Index(s[start:], marker)
if end == -1 {
return fmt.Errorf("could not find end marker %q", marker)
}
end += start

contributor, err := generateContributors()
if err != nil {
return err
}

f, err := os.Create(readmeFile)
if err != nil {
return err
}

defer f.Close()

_, err = f.WriteString(s[:start])
if err != nil {
return err
}

_, err = f.WriteString(contributor)
if err != nil {
return err
}

_, err = f.WriteString(s[end:])
if err != nil {
return err
}

return nil
}

func main() {
if err := replaceReadme(); err != nil {
log.Fatal(err)
}
}

0 comments on commit d08e108

Please sign in to comment.