-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring back update-contributors but in go (#3512)
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
1 parent
58e9e8b
commit d08e108
Showing
3 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |