Skip to content
Open
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ build:
go build -o bin/yaml-readme .
copy: build
cp bin/yaml-readme /usr/local/bin

test:
go test ./...
benchmark:
go test -benchmem -run=^$$ -bench ^*$$ github.com/linuxsuren/yaml-readme/function
65 changes: 65 additions & 0 deletions function/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,74 @@ import (
"net/http"
"os"
"regexp"
"sort"
"strings"
)

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

var (
users []map[string]interface{}
companies map[string]int = make(map[string]int)
err error
)

if users, err = ghRequestAsSlice(api); err == nil {
for _, user := range users {
api := fmt.Sprintf("https://api.github.com/users/%s", user["login"])

var (
data map[string]interface{}
)
if data, err = ghRequestAsMap(api); err == nil {
name := data["company"]
if name == nil {
continue
}
if count, ok := companies[name.(string)]; ok {
companies[name.(string)] = count + 1
} else {
companies[name.(string)] = 1
}
}
}
}

companies = getTopN(companies, 5)
for name, count := range companies {
output = output + fmt.Sprintf("<tr><td>%s</td><td>%d</td><tr>", name, count)
}
return fmt.Sprintf("<table>%s</table>", output)
}

func getTopN(values map[string]int, count int) (result map[string]int) {
tops := []int{}
for _, v := range values {
tops = append(tops, v)
}

sort.Slice(tops, func(i, j int) bool {
return tops[i] > tops[j]
})

if len(tops) < count {
count = len(tops)
} else {
tops = tops[:count]
}

result = make(map[string]int, count)
for _, v := range tops {
for k, vul := range values {
if vul == v {
result[k] = vul
}
}
}
return result
}

// PrintContributors from a GitHub repository
func PrintContributors(owner, repo string) (output string) {
api := fmt.Sprintf("https://api.github.com/repos/%s/%s/contributors", owner, repo)
Expand Down
64 changes: 62 additions & 2 deletions function/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package function
import (
"bytes"
"fmt"
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"os"
"testing"

"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
)

func Test_printContributor(t *testing.T) {
Expand Down Expand Up @@ -364,3 +365,62 @@ func TestPrintPages(t *testing.T) {
})
}
}

func TestGetTopN(t *testing.T) {
tests := []struct {
name string
values map[string]int
count int
expect map[string]int
}{{
name: "normal",
values: items,
count: 3,
expect: map[string]int{
"seven": 11,
"six": 10,
"five": 9,
},
}, {
name: "less data",
values: map[string]int{
"linuxsuren": 2,
},
count: 2,
expect: map[string]int{
"linuxsuren": 2,
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
results := getTopN(tt.values, tt.count)
assert.Equal(t, tt.expect, results)
})
}
}

var items = map[string]int{
"linuxsuren": 1,
"fake": 2,
"test": 3,
"rick": 4,
"one": 5,
"two": 6,
"three": 7,
"four": 8,
"five": 9,
"six": 10,
"seven": 11,
}

func BenchmarkGetTopN(b *testing.B) {
for i := 0; i < b.N; i++ {
getTopN(items, 3)
}
}

func BenchmarkGitHubEmojiLink(b *testing.B) {
for i := 0; i < b.N; i++ {
GitHubEmojiLink("linuxsuren")
}
}
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package main
import (
"bytes"
"fmt"
"github.com/Masterminds/sprig"
"github.com/linuxsuren/yaml-readme/function"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"html/template"
"io"
"io/ioutil"
Expand All @@ -18,6 +14,11 @@ import (
"sort"
"strconv"
"strings"

"github.com/Masterminds/sprig"
"github.com/linuxsuren/yaml-readme/function"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

var logger *log.Logger
Expand Down Expand Up @@ -218,6 +219,9 @@ func getFuncMap(readmeTpl string) template.FuncMap {
"printContributors": func(owner, repo string) template.HTML {
return template.HTML(function.PrintContributors(owner, repo))
},
"printStargazers": func(owner, repo string) template.HTML {
return template.HTML(function.PrintStargazers(owner, repo))
},
"printStarHistory": func(owner, repo string) string {
return printStarHistory(owner, repo)
},
Expand Down
4 changes: 3 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"io/ioutil"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)
import "github.com/stretchr/testify/assert"

func Test_sortBy(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -302,6 +303,7 @@ printGHTable
printHelp
printPages
printStarHistory
printStargazers
printToc
printVisitorCount
render
Expand Down
1 change: 1 addition & 0 deletions samples/stargazers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{printStargazers "linuxsuren" "remote-jobs-in-china"}}