diff --git a/Makefile b/Makefile
index 98c83c9..07d46b6 100644
--- a/Makefile
+++ b/Makefile
@@ -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
\ No newline at end of file
diff --git a/function/github.go b/function/github.go
index 43a0674..2a6602d 100644
--- a/function/github.go
+++ b/function/github.go
@@ -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("
| %s | %d |
", name, count)
+ }
+ return fmt.Sprintf("", 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)
diff --git a/function/github_test.go b/function/github_test.go
index 8ba239a..7095048 100644
--- a/function/github_test.go
+++ b/function/github_test.go
@@ -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) {
@@ -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")
+ }
+}
diff --git a/main.go b/main.go
index 83e754a..5eff504 100644
--- a/main.go
+++ b/main.go
@@ -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"
@@ -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
@@ -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)
},
diff --git a/main_test.go b/main_test.go
index 2b6208e..a192a8a 100644
--- a/main_test.go
+++ b/main_test.go
@@ -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 {
@@ -302,6 +303,7 @@ printGHTable
printHelp
printPages
printStarHistory
+printStargazers
printToc
printVisitorCount
render
diff --git a/samples/stargazers.tpl b/samples/stargazers.tpl
new file mode 100644
index 0000000..88c4283
--- /dev/null
+++ b/samples/stargazers.tpl
@@ -0,0 +1 @@
+{{printStargazers "linuxsuren" "remote-jobs-in-china"}}
\ No newline at end of file