Skip to content

Commit

Permalink
feat: support print the repos that pages enabled (#39)
Browse files Browse the repository at this point in the history
* feat: support print the repos that pages enabled

* add unit tests

Co-authored-by: Rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Nov 18, 2022
1 parent 8b79379 commit d746acf
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Flags:
| `printContributors` | `{{printContributors "linuxsuren" "yaml-readme"}}` | Print all the contributors of an repository |
| `printStarHistory` | `{{printStarHistory "linuxsuren" "yaml-readme"}}` | Print the star history of an repository |
| `printVisitorCount` | `{{printVisitorCount "repo-id"}}` | Print the visitor count chart of an repository |
| `printPages` | `{{printPages "linuxsuren"}}` | Print all the repositories that pages enabled |
| `render` | `{{render true}}` | Make the value be readable, turn `true` to `:white_check_mark:` |
| `gh` | `{{gh "linuxsuren" true}}` | Render a GitHub user to be a link |
| `ghs` | `{{ghs "linuxsuren, linuxsuren" ","}}` | Render multiple GitHub users to be links |
Expand Down
106 changes: 106 additions & 0 deletions function/data/repos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
[
{
"id": 1296269,
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
"name": "Hello-World",
"full_name": "linuxsuren/Hello-World",
"owner": {
"login": "linuxsuren",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/linuxsuren_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/linuxsuren",
"html_url": "https://github.com/linuxsuren",
"followers_url": "https://api.github.com/users/linuxsuren/followers",
"following_url": "https://api.github.com/users/linuxsuren/following{/other_user}",
"gists_url": "https://api.github.com/users/linuxsuren/gists{/gist_id}",
"starred_url": "https://api.github.com/users/linuxsuren/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/linuxsuren/subscriptions",
"organizations_url": "https://api.github.com/users/linuxsuren/orgs",
"repos_url": "https://api.github.com/users/linuxsuren/repos",
"events_url": "https://api.github.com/users/linuxsuren/events{/privacy}",
"received_events_url": "https://api.github.com/users/linuxsuren/received_events",
"type": "User",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/linuxsuren/Hello-World",
"description": "This your first repo!",
"fork": false,
"url": "https://api.github.com/repos/linuxsuren/Hello-World",
"archive_url": "https://api.github.com/repos/linuxsuren/Hello-World/{archive_format}{/ref}",
"assignees_url": "https://api.github.com/repos/linuxsuren/Hello-World/assignees{/user}",
"blobs_url": "https://api.github.com/repos/linuxsuren/Hello-World/git/blobs{/sha}",
"branches_url": "https://api.github.com/repos/linuxsuren/Hello-World/branches{/branch}",
"collaborators_url": "https://api.github.com/repos/linuxsuren/Hello-World/collaborators{/collaborator}",
"comments_url": "https://api.github.com/repos/linuxsuren/Hello-World/comments{/number}",
"commits_url": "https://api.github.com/repos/linuxsuren/Hello-World/commits{/sha}",
"compare_url": "https://api.github.com/repos/linuxsuren/Hello-World/compare/{base}...{head}",
"contents_url": "https://api.github.com/repos/linuxsuren/Hello-World/contents/{+path}",
"contributors_url": "https://api.github.com/repos/linuxsuren/Hello-World/contributors",
"deployments_url": "https://api.github.com/repos/linuxsuren/Hello-World/deployments",
"downloads_url": "https://api.github.com/repos/linuxsuren/Hello-World/downloads",
"events_url": "https://api.github.com/repos/linuxsuren/Hello-World/events",
"forks_url": "https://api.github.com/repos/linuxsuren/Hello-World/forks",
"git_commits_url": "https://api.github.com/repos/linuxsuren/Hello-World/git/commits{/sha}",
"git_refs_url": "https://api.github.com/repos/linuxsuren/Hello-World/git/refs{/sha}",
"git_tags_url": "https://api.github.com/repos/linuxsuren/Hello-World/git/tags{/sha}",
"git_url": "git:github.com/linuxsuren/Hello-World.git",
"issue_comment_url": "https://api.github.com/repos/linuxsuren/Hello-World/issues/comments{/number}",
"issue_events_url": "https://api.github.com/repos/linuxsuren/Hello-World/issues/events{/number}",
"issues_url": "https://api.github.com/repos/linuxsuren/Hello-World/issues{/number}",
"keys_url": "https://api.github.com/repos/linuxsuren/Hello-World/keys{/key_id}",
"labels_url": "https://api.github.com/repos/linuxsuren/Hello-World/labels{/name}",
"languages_url": "https://api.github.com/repos/linuxsuren/Hello-World/languages",
"merges_url": "https://api.github.com/repos/linuxsuren/Hello-World/merges",
"milestones_url": "https://api.github.com/repos/linuxsuren/Hello-World/milestones{/number}",
"notifications_url": "https://api.github.com/repos/linuxsuren/Hello-World/notifications{?since,all,participating}",
"pulls_url": "https://api.github.com/repos/linuxsuren/Hello-World/pulls{/number}",
"releases_url": "https://api.github.com/repos/linuxsuren/Hello-World/releases{/id}",
"ssh_url": "[email protected]:linuxsuren/Hello-World.git",
"stargazers_url": "https://api.github.com/repos/linuxsuren/Hello-World/stargazers",
"statuses_url": "https://api.github.com/repos/linuxsuren/Hello-World/statuses/{sha}",
"subscribers_url": "https://api.github.com/repos/linuxsuren/Hello-World/subscribers",
"subscription_url": "https://api.github.com/repos/linuxsuren/Hello-World/subscription",
"tags_url": "https://api.github.com/repos/linuxsuren/Hello-World/tags",
"teams_url": "https://api.github.com/repos/linuxsuren/Hello-World/teams",
"trees_url": "https://api.github.com/repos/linuxsuren/Hello-World/git/trees{/sha}",
"clone_url": "https://github.com/linuxsuren/Hello-World.git",
"mirror_url": "git:git.example.com/linuxsuren/Hello-World",
"hooks_url": "https://api.github.com/repos/linuxsuren/Hello-World/hooks",
"svn_url": "https://svn.github.com/linuxsuren/Hello-World",
"homepage": "https://github.com",
"language": null,
"forks_count": 9,
"stargazers_count": 80,
"watchers_count": 80,
"size": 108,
"default_branch": "master",
"open_issues_count": 0,
"is_template": false,
"topics": [
"linuxsuren",
"atom",
"electron",
"api"
],
"has_issues": true,
"has_projects": true,
"has_wiki": true,
"has_pages": true,
"has_downloads": true,
"has_discussions": false,
"archived": false,
"disabled": false,
"visibility": "public",
"pushed_at": "2011-01-26T19:06:43Z",
"created_at": "2011-01-26T19:01:12Z",
"updated_at": "2011-01-26T19:14:43Z",
"permissions": {
"admin": false,
"push": false,
"pull": true
}
}
]
43 changes: 43 additions & 0 deletions function/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ func PrintContributors(owner, repo string) (output string) {
return
}

// PrintPages prints the repositories which enabled pages
func PrintPages(owner string) (output string) {
api := fmt.Sprintf("https://api.github.com/users/%s/repos?type=owner&per_page=100&sort=updated&username=%s", owner, owner)

var (
repos []map[string]interface{}
err error
)

if repos, err = ghRequestAsSlice(api); err == nil {
var text string
for i := 0; i < len(repos); i++ {
repo := strings.TrimSpace(generateRepo(repos[i]))
if repo != "" {
text = text + repo + "\n"
}
}

output = fmt.Sprintf(`||||
|---|---|---|
%s`, strings.TrimSpace(text))
}
return
}

func ghRequest(api string) (data []byte, err error) {
var (
resp *http.Response
Expand Down Expand Up @@ -77,6 +102,24 @@ func ghRequestAsMap(api string) (data map[string]interface{}, err error) {
return
}

var pageRepoTemplate = `
{{if eq .has_pages true}}
|{{.name}}|![GitHub Repo stars](https://img.shields.io/github/stars/{{.owner.login}}/{{.name}}?style=social)|[view](https://{{.owner.login}}.github.io/{{.name}}/)|
{{end}}
`

func generateRepo(repo interface{}) (output string) {
var tpl *template.Template
var err error
if tpl, err = template.New("repo").Parse(pageRepoTemplate); err == nil {
buf := bytes.NewBuffer([]byte{})
if err = tpl.Execute(buf, repo); err == nil {
output = buf.String()
}
}
return
}

func generateContributor(contributors []map[string]interface{}) (output string) {
var tpl *template.Template
var err error
Expand Down
34 changes: 34 additions & 0 deletions function/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ func mockGitHubUser(id string) {
Get(fmt.Sprintf("/users/%s", id)).Reply(http.StatusOK).File(fmt.Sprintf("data/%s.json", id))
}

func mockUserRepos(owner string) {
gock.New("https://api.github.com").
Get(fmt.Sprintf("/users/%s/repos", owner)).
MatchParam("type", "owner").
MatchParam("per_page", "100").
MatchParam("sort", "updated").
MatchParam("username", owner).
Reply(http.StatusOK).File("data/repos.json")
}

func TestGitHubUsersLink(t *testing.T) {
type args struct {
ids string
Expand Down Expand Up @@ -330,3 +340,27 @@ func TestPrintUserAsTable(t *testing.T) {
})
}
}

func TestPrintPages(t *testing.T) {
type args struct {
owner string
}
tests := []struct {
name string
args args
wantOutput string
}{{
name: "normal",
args: args{owner: "linuxsuren"},
wantOutput: `||||
|---|---|---|
|Hello-World|![GitHub Repo stars](https://img.shields.io/github/stars/linuxsuren/Hello-World?style=social)|[view](https://linuxsuren.github.io/Hello-World/)|`,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer gock.Off()
mockUserRepos(tt.args.owner)
assert.Equalf(t, tt.wantOutput, PrintPages(tt.args.owner), "PrintPages(%v)", tt.args.owner)
})
}
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ 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)
},
"printPages": func(owner string) string {
return function.PrintPages(owner)
},
"render": dataRender,
"gh": function.GithubUserLink,
"ghs": function.GitHubUsersLink,
Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ linkOrEmpty
printContributors
printGHTable
printHelp
printPages
printStarHistory
printToc
printVisitorCount
Expand Down

0 comments on commit d746acf

Please sign in to comment.