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

Support to print a repo of contributors #13

Merged
merged 3 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v3.0.0
- name: Test
run: |
go test ./...
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2.9.1
with:
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ Flags:

### Available functions

| Name | Usage | Description |
|--------------------|----------------------|----------------------------------------|
| `printHelp` | `{{printHelp 'hd'}}` | Print the help text of a command |
| Name | Usage | Description |
|--------------------|----------------------|-------------------------------------------------------------------------|
| `printHelp` | `{{printHelp 'hd'}}` | Print the help text of a command |
| `printToc` | `{{printToc}}` | Print the [TOC](https://en.wikipedia.org/wiki/TOC) of the template file |
| `printContributors` | `{{printContributors "linuxsuren" "yaml-readme"}}` | Print all the contributors of an repository |

### Ignore particular items

Expand Down
9 changes: 9 additions & 0 deletions data/yaml-readme-contributors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<table><tr>
<td align="center">
<a href="https://github.com/LinuxSuRen">
<img src="https://avatars.githubusercontent.com/u/1450685?v=4" width="100;" alt="LinuxSuRen"/>
<br />
<sub><b>LinuxSuRen</b></sub>
</a>
</td>
</tr></table>
23 changes: 23 additions & 0 deletions data/yaml-readme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"login": "LinuxSuRen",
"id": 1450685,
"node_id": "MDQ6VXNlcjE0NTA2ODU=",
"avatar_url": "https://avatars.githubusercontent.com/u/1450685?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/LinuxSuRen",
"html_url": "https://github.com/LinuxSuRen",
"followers_url": "https://api.github.com/users/LinuxSuRen/followers",
"following_url": "https://api.github.com/users/LinuxSuRen/following{/other_user}",
"gists_url": "https://api.github.com/users/LinuxSuRen/gists{/gist_id}",
"starred_url": "https://api.github.com/users/LinuxSuRen/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/LinuxSuRen/subscriptions",
"organizations_url": "https://api.github.com/users/LinuxSuRen/orgs",
"repos_url": "https://api.github.com/users/LinuxSuRen/repos",
"events_url": "https://api.github.com/users/LinuxSuRen/events{/privacy}",
"received_events_url": "https://api.github.com/users/LinuxSuRen/received_events",
"type": "User",
"site_admin": false,
"contributions": 33
}
]
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/h2non/gock v1.0.9 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=
github.com/h2non/gock v1.0.9/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
70 changes: 70 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"html/template"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -123,6 +126,9 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
"printToc": func() string {
return generateTOC(readmeTpl)
},
"printContributors": func(owner, repo string) template.HTML {
return template.HTML(printContributors(owner, repo))
},
}).Parse(readmeTpl); err != nil {
return
}
Expand Down Expand Up @@ -183,6 +189,70 @@ func generateTOC(txt string) (toc string) {
return
}

func printContributors(owner, repo string) (output string) {
api := fmt.Sprintf("https://api.github.com/repos/%s/%s/contributors", owner, repo)

var (
resp *http.Response
err error
)

if resp, err = http.Get(api); err != nil || resp.StatusCode != http.StatusOK {
return
}

var data []byte
if data, err = ioutil.ReadAll(resp.Body); err != nil {
return
}

var contributors []map[string]interface{}
if err = json.Unmarshal(data, &contributors); err != nil {
return
}

var text string
group := 6
for i := 0; i < len(contributors); {
next := i + group
if next > len(contributors) {
next = len(contributors)
}
text = text + "<tr>" + generateContributor(contributors[i:next]) + "</tr>"
i = next
}

output = fmt.Sprintf(`<table>%s</table>
`, text)
return
}

func generateContributor(contributors []map[string]interface{}) (output string) {
var tpl *template.Template
var err error
if tpl, err = template.New("contributors").Parse(contributorsTpl); err != nil {
return
}

buf := bytes.NewBuffer([]byte{})
if err = tpl.Execute(buf, contributors); err != nil {
return
}
output = buf.String()
return
}

var contributorsTpl = `{{- range $i, $val := .}}
<td align="center">
<a href="{{$val.html_url}}">
<img src="{{$val.avatar_url}}" width="100;" alt="{{$val.login}}"/>
<br />
<sub><b>{{$val.login}}</b></sub>
</a>
</td>
{{- end}}
`

func main() {
opt := &option{}
cmd := cobra.Command{
Expand Down
43 changes: 42 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import "testing"
import (
"github.com/h2non/gock"
"io/ioutil"
"net/http"
"testing"
)
import "github.com/stretchr/testify/assert"

func Test_sortBy(t *testing.T) {
Expand Down Expand Up @@ -112,3 +117,39 @@ content
})
}
}

func Test_printContributor(t *testing.T) {
type args struct {
owner string
repo string
}
tests := []struct {
name string
args args
prepare func()
wantOutput func() string
}{{
name: "normal case",
args: args{
owner: "linuxsuren",
repo: "yaml-readme",
},
prepare: func() {
gock.New("https://api.github.com").
Get("/repos/linuxsuren/yaml-readme/contributors").
Reply(http.StatusOK).
File("data/yaml-readme.json")
},
wantOutput: func() string {
data, _ := ioutil.ReadFile("data/yaml-readme-contributors.txt")
return string(data)
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer gock.Off()
tt.prepare()
assert.Equalf(t, tt.wantOutput(), printContributors(tt.args.owner, tt.args.repo), "printContributors(%v, %v)", tt.args.owner, tt.args.repo)
})
}
}