diff --git a/function/data/linuxsuren-bot.json b/function/data/linuxsuren-bot.json new file mode 100644 index 0000000..765efff --- /dev/null +++ b/function/data/linuxsuren-bot.json @@ -0,0 +1,34 @@ +{ + "login": "linuxsuren-bot", + "id": 39147110, + "node_id": "MDQ6VXNlcjM5MTQ3MTEw", + "avatar_url": "https://avatars.githubusercontent.com/u/39147110?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/linuxsuren-bot", + "html_url": "https://github.com/linuxsuren-bot", + "followers_url": "https://api.github.com/users/linuxsuren-bot/followers", + "following_url": "https://api.github.com/users/linuxsuren-bot/following{/other_user}", + "gists_url": "https://api.github.com/users/linuxsuren-bot/gists{/gist_id}", + "starred_url": "https://api.github.com/users/linuxsuren-bot/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/linuxsuren-bot/subscriptions", + "organizations_url": "https://api.github.com/users/linuxsuren-bot/orgs", + "repos_url": "https://api.github.com/users/linuxsuren-bot/repos", + "events_url": "https://api.github.com/users/linuxsuren-bot/events{/privacy}", + "received_events_url": "https://api.github.com/users/linuxsuren-bot/received_events", + "type": "User", + "site_admin": false, + "name": "LinuxSuRen-bot", + "company": null, + "blog": "", + "location": null, + "email": null, + "hireable": null, + "bio": null, + "twitter_username": null, + "public_repos": 35, + "public_gists": 0, + "followers": 1, + "following": 2, + "created_at": "2018-05-10T04:38:59Z", + "updated_at": "2022-06-15T02:15:00Z" +} \ No newline at end of file diff --git a/function/github.go b/function/github.go index 864c1d4..7e30765 100644 --- a/function/github.go +++ b/function/github.go @@ -139,8 +139,8 @@ func GithubUserLink(id string, bio bool) (link string) { ) if data, err = ghRequestAsMap(api); err == nil { link = fmt.Sprintf("[%s](%s)", data["name"], data["html_url"]) - if bio { - link = fmt.Sprintf("%s (%s)", link, data["bio"]) + if bioText, ok := data["bio"]; ok && bio && bioText != nil { + link = fmt.Sprintf("%s (%s)", link, bioText) } } return diff --git a/function/github_test.go b/function/github_test.go index ae1e7f7..29ebfb6 100644 --- a/function/github_test.go +++ b/function/github_test.go @@ -53,49 +53,63 @@ func TestGithubUserLink(t *testing.T) { bio bool } tests := []struct { - name string - args args - want string + name string + mockUser string + args args + want string }{{ - name: "normal case without bio", + name: "normal case without bio", + mockUser: "linuxsuren", args: args{ id: "linuxsuren", bio: false, }, want: `[Rick](https://github.com/LinuxSuRen)`, }, { - name: "normal case with bio", + name: "normal case with bio", + mockUser: "linuxsuren", args: args{ id: "linuxsuren", bio: true, }, want: `[Rick](https://github.com/LinuxSuRen) (程序员,业余开源布道者)`, }, { - name: "with whitespace", + name: "with whitespace", + mockUser: "linuxsuren", args: args{ id: "this is not id", bio: false, }, want: "this is not id", }, { - name: "has Markdown style link", + name: "has Markdown style link", + mockUser: "linuxsuren", args: args{ id: "[name](link)", bio: false, }, want: "[name](link)", }, { - name: "has Markdown style link, want bio", + name: "has Markdown style link, want bio", + mockUser: "linuxsuren", args: args{ id: "[Rick](https://github.com/linuxsuren)", bio: true, }, want: `[Rick](https://github.com/LinuxSuRen) (程序员,业余开源布道者)`, + }, { + name: "do not have bio", + mockUser: "linuxsuren-bot", + args: args{ + id: "linuxsuren-bot", + bio: true, + }, + want: `[LinuxSuRen-bot](https://github.com/linuxsuren-bot)`, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { defer gock.Off() - mockGitHubUser("linuxsuren") + mockGitHubUser(tt.mockUser) assert.Equalf(t, tt.want, GithubUserLink(tt.args.id, tt.args.bio), "GithubUserLink(%v, %v)", tt.args.id, tt.args.bio) }) }