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 github user as a table #34

Merged
merged 1 commit into from
Jul 8, 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
36 changes: 36 additions & 0 deletions function/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,42 @@ func GetIDFromGHLink(link string) string {
return reg.ReplaceAllString(link, "")
}

// PrintUserAsTable generates a table for a GitHub user
func PrintUserAsTable(id string) (result string) {
api := fmt.Sprintf("https://api.github.com/users/%s", id)

result = `|||
|---|---|
`

var (
err error
data map[string]interface{}
)
if data, err = ghRequestAsMap(api); err == nil {
result = result + addWithEmpty("Name", "name", data) +
addWithEmpty("Location", "location", data) +
addWithEmpty("Bio", "bio", data) +
addWithEmpty("Blog", "blog", data) +
addWithEmpty("Twitter", "twitter_username", data) +
addWithEmpty("Organization", "company", data)
}
return
}

func addWithEmpty(title, key string, data map[string]interface{}) (result string) {
if val, ok := data[key]; ok && val != "" {
desc := val
switch key {
case "twitter_username":
desc = fmt.Sprintf("[%s](https://twitter.com/%s)", val, val)
}
result = fmt.Sprintf(`| %s | %s |
`, title, desc)
}
return
}

// hasLink determines if there are Markdown style links
func hasLink(text string) (ok bool) {
reg, _ := regexp.Compile(".*\\[.*\\]\\(.*\\)")
Expand Down
32 changes: 32 additions & 0 deletions function/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,35 @@ func Test_getIDFromGHLink(t *testing.T) {
})
}
}

func TestPrintUserAsTable(t *testing.T) {
type args struct {
id string
}
tests := []struct {
name string
args args
wantResult string
}{{
name: "normal",
args: args{
id: "linuxsuren",
},
wantResult: `|||
|---|---|
| Name | Rick |
| Location | China |
| Bio | 程序员,业余开源布道者 |
| Blog | https://linuxsuren.github.io/open-source-best-practice/ |
| Twitter | [linuxsuren](https://twitter.com/linuxsuren) |
| Organization | @opensource-f2f @kubesphere |
`,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer gock.Off()
mockGitHubUser(tt.args.id)
assert.Equalf(t, tt.wantResult, PrintUserAsTable(tt.args.id), "PrintUserAsTable(%v)", tt.args.id)
})
}
}
21 changes: 11 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,17 @@ func getFuncMap(readmeTpl string) template.FuncMap {
"printVisitorCount": func(id string) string {
return fmt.Sprintf(`![Visitor Count](https://profile-counter.glitch.me/%s/count.svg)`, id)
},
"render": dataRender,
"gh": function.GithubUserLink,
"ghs": function.GitHubUsersLink,
"ghEmoji": function.GitHubEmojiLink,
"link": function.Link,
"linkOrEmpty": function.LinkOrEmpty,
"twitterLink": function.TwitterLink,
"youTubeLink": function.YouTubeLink,
"gstatic": function.GStatic,
"ghID": function.GetIDFromGHLink,
"render": dataRender,
"gh": function.GithubUserLink,
"ghs": function.GitHubUsersLink,
"ghEmoji": function.GitHubEmojiLink,
"link": function.Link,
"linkOrEmpty": function.LinkOrEmpty,
"twitterLink": function.TwitterLink,
"youTubeLink": function.YouTubeLink,
"gstatic": function.GStatic,
"ghID": function.GetIDFromGHLink,
"printGHTable": function.PrintUserAsTable,
}
}

Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ gstatic
link
linkOrEmpty
printContributors
printGHTable
printHelp
printStarHistory
printToc
Expand Down