Skip to content

Commit

Permalink
Merge pull request #17 from kyoshidajp/refactor_params
Browse files Browse the repository at this point in the history
Refactor params
  • Loading branch information
kyoshidajp authored Oct 28, 2023
2 parents 37e98f3 + ee7d052 commit aad5a18
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 101 deletions.
52 changes: 26 additions & 26 deletions cmd/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ func NewBundlerDoctor() *BundlerDoctor {
return &BundlerDoctor{}
}

func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
func (d *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
nameWithOwners := b.NameWithOwners(r)
sliceSize := len(nameWithOwners)
slicedParams := [][]github.FetchRepositoryParam{}
fetchRepositoryParams := d.FetchRepositoryParams(r)
sliceSize := len(fetchRepositoryParams)

for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
end := i + github.SEARCH_REPOS_PER_ONCE
if sliceSize < end {
end = sliceSize
}
slicedNameWithOwners = append(slicedNameWithOwners, nameWithOwners[i:end])
slicedParams = append(slicedParams, fetchRepositoryParams[i:end])
}

for _, nameWithOwners := range slicedNameWithOwners {
repos := github.FetchFromGitHub(nameWithOwners)
for _, param := range slicedParams {
repos := github.FetchFromGitHub(param)
for _, r := range repos {
isIgnore := slices.Contains(ignores, r.Name)
diagnosis := Diagnosis{
Expand All @@ -46,22 +46,22 @@ func (b *BundlerDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []s
}
}

for _, nameWithOwner := range nameWithOwners {
if nameWithOwner.CanSearch {
for _, fetchRepositoryParam := range fetchRepositoryParams {
if fetchRepositoryParam.CanSearch {
continue
}

diagnosis := Diagnosis{
Name: nameWithOwner.PackageName,
Name: fetchRepositoryParam.PackageName,
Diagnosed: false,
}
diagnoses[nameWithOwner.PackageName] = diagnosis
diagnoses[fetchRepositoryParam.PackageName] = diagnosis
}
return diagnoses
}

func (d *BundlerDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner {
var nameWithOwners []github.NameWithOwner
func (d *BundlerDoctor) FetchRepositoryParams(r parser_io.ReadSeekerAt) []github.FetchRepositoryParam {
var params []github.FetchRepositoryParam
p := &bundler.Parser{}
libs, _, _ := p.Parse(r)

Expand All @@ -75,25 +75,25 @@ func (d *BundlerDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWi
}

repo, err := github.ParseGitHubUrl(githubUrl)

if err != nil {
nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
params = append(params,
github.FetchRepositoryParam{
PackageName: lib.Name,
CanSearch: false,
},
)
} else {
nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
Repo: repo.Repo,
Owner: repo.Owner,
PackageName: lib.Name,
CanSearch: true,
},
)
continue
}

params = append(params,
github.FetchRepositoryParam{
Repo: repo.Repo,
Owner: repo.Owner,
PackageName: lib.Name,
CanSearch: true,
},
)
}

return nameWithOwners
return params
}
2 changes: 1 addition & 1 deletion cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const MAX_YEAR_TO_BE_BLANK = 5

type Doctor interface {
Diagnose(r io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis
NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner
FetchRepositoryParams(r parser_io.ReadSeekerAt) []github.FetchRepositoryParam
}

type Diagnosis struct {
Expand Down
30 changes: 17 additions & 13 deletions cmd/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const QUERY_SEPARATOR = " "
const SEARCH_REPOS_PER_ONCE = 20
const TOKEN_NAME = "GITHUB_TOKEN"

type GitHubRepository struct {
Name string
Expand All @@ -33,14 +34,14 @@ func (r GitHubRepository) IsActive(year int) bool {
return targetDate.After(now)
}

type NameWithOwner struct {
type FetchRepositoryParam struct {
PackageName string
Repo string
Owner string
CanSearch bool
}

func (n NameWithOwner) GetName() string {
func (n FetchRepositoryParam) QueryWord() string {
return fmt.Sprintf("repo:%s/%s", n.Owner, n.Repo)
}

Expand Down Expand Up @@ -96,10 +97,11 @@ func ParseGitHubUrl(url string) (GitHubRepository, error) {
}, nil
}

func FetchFromGitHub(nameWithOwners []NameWithOwner) []GitHubRepository {
token := os.Getenv("GITHUB_TOKEN")
func FetchFromGitHub(params []FetchRepositoryParam) []GitHubRepository {
token := os.Getenv(TOKEN_NAME)
if len(token) == 0 {
log.Fatal("env var `GITHUB_TOKEN` is not found")
m := fmt.Sprintf("Environment variable `%s` is not found.", TOKEN_NAME)
log.Fatal(m)
}

src := oauth2.StaticTokenSource(
Expand Down Expand Up @@ -136,9 +138,9 @@ func FetchFromGitHub(nameWithOwners []NameWithOwner) []GitHubRepository {
} `graphql:"search(query:$query, first:$count, type:REPOSITORY)"`
}

names := make([]string, len(nameWithOwners))
for i, n := range nameWithOwners {
names[i] = n.GetName()
names := make([]string, len(params))
for i, param := range params {
names[i] = param.QueryWord()
}
q := strings.Join(names, QUERY_SEPARATOR)
variables := map[string]interface{}{
Expand All @@ -153,12 +155,14 @@ func FetchFromGitHub(nameWithOwners []NameWithOwner) []GitHubRepository {
}

for _, node := range query.Search.Nodes {
nodeRepo := node.Repository
lastCommit := nodeRepo.DefaultBranchRef.Target.Commit.History.Edges[0].Node
repos = append(repos, GitHubRepository{
Repo: string(node.Repository.NameWithOwner),
Archived: bool(node.Repository.IsArchived),
Url: string(node.Repository.Url),
Name: string(node.Repository.Name),
LastCommittedAt: time.Time(node.Repository.DefaultBranchRef.Target.Commit.History.Edges[0].Node.CommittedDate.Time),
Repo: string(nodeRepo.NameWithOwner),
Archived: bool(nodeRepo.IsArchived),
Url: string(nodeRepo.Url),
Name: string(nodeRepo.Name),
LastCommittedAt: time.Time(lastCommit.CommittedDate.Time),
})
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func TestParseGitHubUrl(t *testing.T) {

func TestFetchFromGitHub(t *testing.T) {
tests := []struct {
name string
nameWithOwners []NameWithOwner
name string
params []FetchRepositoryParam
}{
{
name: "active repository",
nameWithOwners: []NameWithOwner{
params: []FetchRepositoryParam{
{
Owner: "rails",
Repo: "rails",
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestFetchFromGitHub(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FetchFromGitHub(tt.nameWithOwners)
got := FetchFromGitHub(tt.params)
if d := cmp.Diff(got, expect, cmpopts.IgnoreFields(GitHubRepository{}, "LastCommittedAt")); len(d) != 0 {
t.Errorf("differs: (-got +want)\n%s", d)
}
Expand Down
38 changes: 19 additions & 19 deletions cmd/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ func NewNPMDoctor() *NPMDoctor {

func (d *NPMDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
nameWithOwners := d.NameWithOwners(r)
sliceSize := len(nameWithOwners)
slicedParams := [][]github.FetchRepositoryParam{}
params := d.FetchRepositoryParams(r)
sliceSize := len(params)

for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
end := i + github.SEARCH_REPOS_PER_ONCE
if sliceSize < end {
end = sliceSize
}
slicedNameWithOwners = append(slicedNameWithOwners, nameWithOwners[i:end])
slicedParams = append(slicedParams, params[i:end])
}

for _, nameWithOwners := range slicedNameWithOwners {
repos := github.FetchFromGitHub(nameWithOwners)
for _, param := range slicedParams {
repos := github.FetchFromGitHub(param)
for _, r := range repos {
diagnosis := Diagnosis{
Name: r.Name,
Expand All @@ -43,22 +43,22 @@ func (d *NPMDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []strin
}
}

for _, nameWithOwner := range nameWithOwners {
if nameWithOwner.CanSearch {
for _, param := range params {
if param.CanSearch {
continue
}

diagnosis := Diagnosis{
Name: nameWithOwner.PackageName,
Name: param.PackageName,
Diagnosed: false,
}
diagnoses[nameWithOwner.PackageName] = diagnosis
diagnoses[param.PackageName] = diagnosis
}
return diagnoses
}

func (d *NPMDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner {
var nameWithOwners []github.NameWithOwner
func (d *NPMDoctor) FetchRepositoryParams(r parser_io.ReadSeekerAt) []github.FetchRepositoryParam {
var params []github.FetchRepositoryParam
libs, _, _ := npm.NewParser().Parse(r)

nodejs := Nodejs{}
Expand All @@ -67,8 +67,8 @@ func (d *NPMDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw

githubUrl, err := nodejs.fetchURLFromRegistry(lib.Name)
if err != nil {
nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
params = append(params,
github.FetchRepositoryParam{
PackageName: lib.Name,
CanSearch: false,
},
Expand All @@ -78,17 +78,17 @@ func (d *NPMDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw

repo, err := github.ParseGitHubUrl(githubUrl)
if err != nil {
nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
params = append(params,
github.FetchRepositoryParam{
PackageName: lib.Name,
CanSearch: false,
},
)
continue
}

nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
params = append(params,
github.FetchRepositoryParam{
Repo: repo.Repo,
Owner: repo.Owner,
PackageName: lib.Name,
Expand All @@ -97,5 +97,5 @@ func (d *NPMDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw
)
}

return nameWithOwners
return params
}
38 changes: 19 additions & 19 deletions cmd/pip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ func NewPipDoctor() *PipDoctor {

func (d *PipDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []string) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
nameWithOwners := d.NameWithOwners(r)
sliceSize := len(nameWithOwners)
slicedParams := [][]github.FetchRepositoryParam{}
params := d.FetchRepositoryParams(r)
sliceSize := len(params)

for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
end := i + github.SEARCH_REPOS_PER_ONCE
if sliceSize < end {
end = sliceSize
}
slicedNameWithOwners = append(slicedNameWithOwners, nameWithOwners[i:end])
slicedParams = append(slicedParams, params[i:end])
}

for _, nameWithOwners := range slicedNameWithOwners {
repos := github.FetchFromGitHub(nameWithOwners)
for _, slicedParam := range slicedParams {
repos := github.FetchFromGitHub(slicedParam)
for _, r := range repos {
diagnosis := Diagnosis{
Name: r.Name,
Expand All @@ -43,22 +43,22 @@ func (d *PipDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []strin
}
}

for _, nameWithOwner := range nameWithOwners {
if nameWithOwner.CanSearch {
for _, param := range params {
if param.CanSearch {
continue
}

diagnosis := Diagnosis{
Name: nameWithOwner.PackageName,
Name: param.PackageName,
Diagnosed: false,
}
diagnoses[nameWithOwner.PackageName] = diagnosis
diagnoses[param.PackageName] = diagnosis
}
return diagnoses
}

func (d *PipDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner {
var nameWithOwners []github.NameWithOwner
func (d *PipDoctor) FetchRepositoryParams(r parser_io.ReadSeekerAt) []github.FetchRepositoryParam {
var params []github.FetchRepositoryParam
libs, _, _ := pip.NewParser().Parse(r)

pypi := Pypi{}
Expand All @@ -67,8 +67,8 @@ func (d *PipDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw

githubUrl, err := pypi.fetchURLFromRepository(lib.Name)
if err != nil {
nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
params = append(params,
github.FetchRepositoryParam{
PackageName: lib.Name,
CanSearch: false,
},
Expand All @@ -78,17 +78,17 @@ func (d *PipDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw

repo, err := github.ParseGitHubUrl(githubUrl)
if err != nil {
nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
params = append(params,
github.FetchRepositoryParam{
PackageName: lib.Name,
CanSearch: false,
},
)
continue
}

nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
params = append(params,
github.FetchRepositoryParam{
Repo: repo.Repo,
Owner: repo.Owner,
PackageName: lib.Name,
Expand All @@ -97,5 +97,5 @@ func (d *PipDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw
)
}

return nameWithOwners
return params
}
Loading

0 comments on commit aad5a18

Please sign in to comment.