Skip to content

Commit

Permalink
Merge pull request #44 from juev/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
juev committed Dec 31, 2023
2 parents 200dfee + 0d111ea commit 8974a47
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 97 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: '>=1.17.0'
go-version: '>=1.20.0'
check-latest: true
-
name: Install dependencies
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: '>=1.17.0'
check-latest: true
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
Expand All @@ -31,10 +31,10 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v3
-
name: Log in to the Container registry
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
88 changes: 43 additions & 45 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,95 @@ import (
"context"
"fmt"
"log"
"net/http"
"os"

"github.com/google/go-github/github"
"golang.org/x/oauth2"
"github.com/google/go-github/v57/github"
"github.com/gregjones/httpcache"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

const (
// perPage is how many links we get by ine shoot
// perPage is how many links we get by one shoot
perPage int = 100
// repositoriesCount is const for allocation memory to store repositories
repositoriesCount = 1000
// langReposCount is const for allocation memory to store langRepo
langReposCount = 100
)

// Github struct for requests
type Github struct {
// GitHub struct for requests
type GitHub struct {
client *github.Client
}

// Repository struct for storing parameters from Repository
type Repository struct {
FullName string
HTMLURL string
URL string
Language string
Description string
}

// NewGithub creates new github client
func NewGithub(ctx context.Context, token string) (client *Github) {
var tc *http.Client
// New creates new GitHub client
func New(token string) (client *GitHub) {
gh := github.NewClient(
httpcache.NewMemoryCacheTransport().Client(),
)
if token != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc = oauth2.NewClient(ctx, ts)
gh = gh.WithAuthToken(token)
}
return &Github{client: github.NewClient(tc)}
return &GitHub{client: gh}
}

// GetRepositories getting repositories from Github
func (g *Github) GetRepositories(ctx context.Context) (langRepoMap map[string][]Repository, repositories []Repository) {
opt := &github.ActivityListStarredOptions{}
opt.ListOptions.PerPage = perPage
// GetRepositories getting repositories from GitHub
func (g *GitHub) GetRepositories(ctx context.Context) (langRepoMap map[string][]Repository, repositories []Repository) {
repositories = make([]Repository, 0, repositoriesCount)
langRepoMap = make(map[string][]Repository, langReposCount)

pageIdx := 1
for {
opt.ListOptions.Page = pageIdx
opt := &github.ActivityListStarredOptions{
ListOptions: github.ListOptions{PerPage: perPage},
}

reps, _, err := g.client.Activity.ListStarred(ctx, username, opt)
for {
repos, resp, err := g.client.Activity.ListStarred(ctx, username, opt)
if err != nil {
log.Fatalln("Error: cannot fetch starred:", err)
}
for _, r := range reps {
repositories = append(repositories, Repository{
for _, r := range repos {
repo := Repository{
FullName: r.Repository.GetFullName(),
HTMLURL: r.Repository.GetHTMLURL(),
URL: r.Repository.GetHTMLURL(),
Language: r.Repository.GetLanguage(),
Description: r.Repository.GetDescription(),
})
}
repositories = append(repositories, repo)
lang := "Others"
if repo.Language != "" {
lang = capitalize(repo.Language)
}

if _, ok := langRepoMap[lang]; !ok {
langRepoMap[lang] = make([]Repository, 0, langReposCount)
}
langRepoMap[lang] = append(langRepoMap[lang], repo)
}

if len(reps) != perPage {
if resp.NextPage == 0 {
break
}

pageIdx++
opt.Page = resp.NextPage
}

if len(repositories) == 0 {
return nil, repositories
}

langRepoMap = make(map[string][]Repository)
for _, r := range repositories {
lang := "Others"
if r.Language != "" {
lang = capitalize(r.Language)
}

langList, ok := langRepoMap[lang]
if !ok {
langList = []Repository{}
}
langList = append(langList, r)
langRepoMap[lang] = langList
}
return langRepoMap, repositories
}

// UpdateReadmeFile updates README file
func (g *Github) UpdateReadmeFile(ctx context.Context) {
func (g *GitHub) UpdateReadmeFile(ctx context.Context) {
if _, resp, err := g.client.Repositories.Get(ctx, username, repository); err != nil || resp.StatusCode != 200 {
fmt.Printf("Error: check repository (%s) is exist : %v\n", repository, err)
os.Exit(2)
Expand Down
14 changes: 4 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
module github.com/juev/starred

go 1.18
go 1.20

require (
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-github/v57 v57.0.0
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79
github.com/spf13/pflag v1.0.5
golang.org/x/oauth2 v0.15.0
golang.org/x/text v0.14.0
)

require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
golang.org/x/net v0.19.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
require github.com/google/go-querystring v1.1.0 // indirect
29 changes: 5 additions & 24 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-github/v57 v57.0.0 h1:L+Y3UPTY8ALM8x+TV0lg+IEBI+upibemtBD8Q9u7zHs=
github.com/google/go-github/v57 v57.0.0/go.mod h1:s0omdnye0hvK/ecLvpsGfJMiRt85PimQh4oygmLIxHw=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA=
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/oauth2 v0.15.0 h1:s8pnnxNVzjWyrvYdFUQq5llS1PX2zhPXmccZv99h7uQ=
golang.org/x/oauth2 v0.15.0/go.mod h1:q48ptWNTY5XWf+JNten23lcvHpLJ0ZSxF5ttTHKVCAM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func init() {
if version != "" {
versionStr = fmt.Sprintf("starred version: %s (%s) / builded %s\n", version, commit[:6], date)
}
fmt.Printf(versionStr)
fmt.Println(versionStr)
os.Exit(0)
}

Expand All @@ -67,12 +67,12 @@ func init() {
func main() {
ctx := context.Background()

client := NewGithub(ctx, token)
client := New(token)

langRepoMap, repositories := client.GetRepositories(ctx)

var funcMap = template.FuncMap{
"toLink": func(lang string) string { return strings.ToLower(strings.Replace(lang, " ", "-", -1)) },
"toLink": func(lang string) string { return strings.ToLower(strings.ReplaceAll(lang, " ", "-")) },
}

temp := template.Must(template.New("starred").Funcs(funcMap).Parse(content))
Expand Down
10 changes: 4 additions & 6 deletions templates/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@

{{ range $lang, $langMap := .LangRepoMap }}
<div id="{{ toLink $lang }}"></div>

## {{ $lang }}

{{ range $langMap -}}
- [{{ .FullName }}]({{ .HTMLURL }}){{ if ne .Description "" }} – {{ .Description }}{{- end }}
{{ end }}
{{- end }}
- [{{ .FullName }}]({{ .URL }}){{ if ne .Description "" }} – {{ .Description }}{{- end }}
{{ end }}{{- end }}
{{- else }}
## Repositories

{{ range .Repositories -}}
- [{{ .FullName }}]({{ .HTMLURL }})
- [{{ .FullName }}]({{ .URL }})
{{ end }}
{{ end -}}
{{- end }}

## License

Expand Down

0 comments on commit 8974a47

Please sign in to comment.