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 render a GitHub users to be a link #23

Merged
merged 2 commits into from
Jun 18, 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
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ Flags:

### Available variables:

| Name | Usage |
|---|---|
| `filename` | The filename of a particular item file. For example, `items/good.yaml`, the filename is `good`. |
| `parentname` | The parent directory name. For example, `items/good.yaml`, the parent name is `items`. |
| `fullpath` | The related file path of each items. |
| Name | Usage |
|--------------|-------------------------------------------------------------------------------------------------|
| `filename` | The filename of a particular item file. For example, `items/good.yaml`, the filename is `good`. |
| `parentname` | The parent directory name. For example, `items/good.yaml`, the parent name is `items`. |
| `fullpath` | The related file path of each items. |

### Available functions

Expand All @@ -40,7 +40,9 @@ 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 |
| `render` | `{{render true}}` | Make the value be readable, turn `true` to `:white_check_mark:` |
| `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 |

### Ignore particular items

Expand Down
46 changes: 46 additions & 0 deletions function/data/linuxsuren.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"login": "LinuxSuRen",
"id": 1450685,
"node_id": "MDQ6VXNlcjE0NTA2ODU=",
"avatar_url": "https://avatars.githubusercontent.com/u/1450685?v=4",
"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,
"name": "Rick",
"company": "@opensource-f2f @kubesphere",
"blog": "https://linuxsuren.github.io/open-source-best-practice/",
"location": "China",
"email": null,
"hireable": true,
"bio": "程序员,业余开源布道者",
"twitter_username": "linuxsuren",
"public_repos": 706,
"public_gists": 2,
"followers": 595,
"following": 2,
"created_at": "2012-02-19T06:28:06Z",
"updated_at": "2022-05-23T04:17:31Z",
"private_gists": 16,
"total_private_repos": 16,
"owned_private_repos": 16,
"disk_usage": 424624,
"collaborators": 1,
"two_factor_authentication": true,
"plan": {
"name": "free",
"space": 976562499,
"collaborators": 0,
"private_repos": 10000
}
}
File renamed without changes.
143 changes: 143 additions & 0 deletions function/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package function

import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"regexp"
"strings"
)

// 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)

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

if contributors, err = ghRequestAsSlice(api); err == nil {
var text string
group := 6
for i := 0; i < len(contributors); {
next := i + group
if next > len(contributors) {
next = len(contributors)
}
text = text + "<tr>" + generateContributor(contributors[i:next]) + "</tr>"
i = next
}

output = fmt.Sprintf(`<table>%s</table>
`, text)
}
return
}

func ghRequest(api string) (data []byte, err error) {
var (
resp *http.Response
)

if resp, err = http.Get(api); err == nil && resp.StatusCode == http.StatusOK {
data, err = ioutil.ReadAll(resp.Body)
}
return
}

func ghRequestAsSlice(api string) (data []map[string]interface{}, err error) {
var byteData []byte
if byteData, err = ghRequest(api); err == nil {
err = json.Unmarshal(byteData, &data)
}
return
}

func ghRequestAsMap(api string) (data map[string]interface{}, err error) {
var byteData []byte
if byteData, err = ghRequest(api); err == nil {
err = json.Unmarshal(byteData, &data)
}
return
}

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

var contributorsTpl = `{{- range $i, $val := .}}
<td align="center">
<a href="{{$val.html_url}}">
<img src="{{$val.avatar_url}}" width="100;" alt="{{$val.login}}"/>
<br />
<sub><b>{{$val.login}}</b></sub>
</a>
</td>
{{- end}}
`

// GitHubUsersLink parses a text and try to make the potential GitHub IDs be links
func GitHubUsersLink(ids, sep string) (links string) {
if sep == "" {
sep = " "
}

splits := strings.Split(ids, sep)
var items []string
for _, item := range splits {
items = append(items, GithubUserLink(strings.TrimSpace(item), false))
}

// having additional whitespace it's an ASCII character
if sep == "," {
sep = sep + " "
}
links = strings.Join(items, sep)
return
}

// GithubUserLink makes a GitHub user link
func GithubUserLink(id string, bio bool) (link string) {
link = id
if strings.Contains(id, " ") { // only handle the valid GitHub ID
return
}

// return the original text if there are Markdown style link exist
if hasLink(id) {
return
}

api := fmt.Sprintf("https://api.github.com/users/%s", id)

var (
err error
data map[string]interface{}
)
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"])
}
}
return
}

// hasLink determines if there are Markdown style links
func hasLink(text string) (ok bool) {
reg, _ := regexp.Compile(".*\\[.*\\]\\(.*\\)")
ok = reg.MatchString(text)
return
}
Loading