Skip to content

Commit 10aa28a

Browse files
authored
Do not show bio if it is null (#33)
Co-authored-by: rick <[email protected]>
1 parent c5274e6 commit 10aa28a

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

function/data/linuxsuren-bot.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"login": "linuxsuren-bot",
3+
"id": 39147110,
4+
"node_id": "MDQ6VXNlcjM5MTQ3MTEw",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/39147110?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/linuxsuren-bot",
8+
"html_url": "https://github.com/linuxsuren-bot",
9+
"followers_url": "https://api.github.com/users/linuxsuren-bot/followers",
10+
"following_url": "https://api.github.com/users/linuxsuren-bot/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/linuxsuren-bot/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/linuxsuren-bot/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/linuxsuren-bot/subscriptions",
14+
"organizations_url": "https://api.github.com/users/linuxsuren-bot/orgs",
15+
"repos_url": "https://api.github.com/users/linuxsuren-bot/repos",
16+
"events_url": "https://api.github.com/users/linuxsuren-bot/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/linuxsuren-bot/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "LinuxSuRen-bot",
21+
"company": null,
22+
"blog": "",
23+
"location": null,
24+
"email": null,
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": null,
28+
"public_repos": 35,
29+
"public_gists": 0,
30+
"followers": 1,
31+
"following": 2,
32+
"created_at": "2018-05-10T04:38:59Z",
33+
"updated_at": "2022-06-15T02:15:00Z"
34+
}

function/github.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ func GithubUserLink(id string, bio bool) (link string) {
139139
)
140140
if data, err = ghRequestAsMap(api); err == nil {
141141
link = fmt.Sprintf("[%s](%s)", data["name"], data["html_url"])
142-
if bio {
143-
link = fmt.Sprintf("%s (%s)", link, data["bio"])
142+
if bioText, ok := data["bio"]; ok && bio && bioText != nil {
143+
link = fmt.Sprintf("%s (%s)", link, bioText)
144144
}
145145
}
146146
return

function/github_test.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,49 +53,63 @@ func TestGithubUserLink(t *testing.T) {
5353
bio bool
5454
}
5555
tests := []struct {
56-
name string
57-
args args
58-
want string
56+
name string
57+
mockUser string
58+
args args
59+
want string
5960
}{{
60-
name: "normal case without bio",
61+
name: "normal case without bio",
62+
mockUser: "linuxsuren",
6163
args: args{
6264
id: "linuxsuren",
6365
bio: false,
6466
},
6567
want: `[Rick](https://github.com/LinuxSuRen)`,
6668
}, {
67-
name: "normal case with bio",
69+
name: "normal case with bio",
70+
mockUser: "linuxsuren",
6871
args: args{
6972
id: "linuxsuren",
7073
bio: true,
7174
},
7275
want: `[Rick](https://github.com/LinuxSuRen) (程序员,业余开源布道者)`,
7376
}, {
74-
name: "with whitespace",
77+
name: "with whitespace",
78+
mockUser: "linuxsuren",
7579
args: args{
7680
id: "this is not id",
7781
bio: false,
7882
},
7983
want: "this is not id",
8084
}, {
81-
name: "has Markdown style link",
85+
name: "has Markdown style link",
86+
mockUser: "linuxsuren",
8287
args: args{
8388
id: "[name](link)",
8489
bio: false,
8590
},
8691
want: "[name](link)",
8792
}, {
88-
name: "has Markdown style link, want bio",
93+
name: "has Markdown style link, want bio",
94+
mockUser: "linuxsuren",
8995
args: args{
9096
id: "[Rick](https://github.com/linuxsuren)",
9197
bio: true,
9298
},
9399
want: `[Rick](https://github.com/LinuxSuRen) (程序员,业余开源布道者)`,
100+
}, {
101+
name: "do not have bio",
102+
mockUser: "linuxsuren-bot",
103+
args: args{
104+
id: "linuxsuren-bot",
105+
bio: true,
106+
},
107+
want: `[LinuxSuRen-bot](https://github.com/linuxsuren-bot)`,
94108
}}
95109
for _, tt := range tests {
96110
t.Run(tt.name, func(t *testing.T) {
97111
defer gock.Off()
98-
mockGitHubUser("linuxsuren")
112+
mockGitHubUser(tt.mockUser)
99113
assert.Equalf(t, tt.want, GithubUserLink(tt.args.id, tt.args.bio), "GithubUserLink(%v, %v)", tt.args.id, tt.args.bio)
100114
})
101115
}

0 commit comments

Comments
 (0)