diff --git a/cmd/bundler.go b/cmd/bundler.go index 4ea67df..5834097 100644 --- a/cmd/bundler.go +++ b/cmd/bundler.go @@ -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{ @@ -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) @@ -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 } diff --git a/cmd/diagnose.go b/cmd/diagnose.go index 1f53e06..2c45019 100644 --- a/cmd/diagnose.go +++ b/cmd/diagnose.go @@ -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 { diff --git a/cmd/github/github.go b/cmd/github/github.go index 4622135..e55e375 100644 --- a/cmd/github/github.go +++ b/cmd/github/github.go @@ -17,6 +17,7 @@ import ( const QUERY_SEPARATOR = " " const SEARCH_REPOS_PER_ONCE = 20 +const TOKEN_NAME = "GITHUB_TOKEN" type GitHubRepository struct { Name string @@ -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) } @@ -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( @@ -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{}{ @@ -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), }) } diff --git a/cmd/github/github_test.go b/cmd/github/github_test.go index e1e9a80..1c09645 100644 --- a/cmd/github/github_test.go +++ b/cmd/github/github_test.go @@ -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", @@ -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) } diff --git a/cmd/npm.go b/cmd/npm.go index 3316f42..906a4f4 100644 --- a/cmd/npm.go +++ b/cmd/npm.go @@ -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, @@ -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{} @@ -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, }, @@ -78,8 +78,8 @@ 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, }, @@ -87,8 +87,8 @@ func (d *NPMDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw continue } - nameWithOwners = append(nameWithOwners, - github.NameWithOwner{ + params = append(params, + github.FetchRepositoryParam{ Repo: repo.Repo, Owner: repo.Owner, PackageName: lib.Name, @@ -97,5 +97,5 @@ func (d *NPMDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw ) } - return nameWithOwners + return params } diff --git a/cmd/pip.go b/cmd/pip.go index 8124dc0..0dc8fc1 100644 --- a/cmd/pip.go +++ b/cmd/pip.go @@ -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, @@ -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{} @@ -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, }, @@ -78,8 +78,8 @@ 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, }, @@ -87,8 +87,8 @@ func (d *PipDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw continue } - nameWithOwners = append(nameWithOwners, - github.NameWithOwner{ + params = append(params, + github.FetchRepositoryParam{ Repo: repo.Repo, Owner: repo.Owner, PackageName: lib.Name, @@ -97,5 +97,5 @@ func (d *PipDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOw ) } - return nameWithOwners + return params } diff --git a/cmd/yarn.go b/cmd/yarn.go index a04021a..49e050a 100644 --- a/cmd/yarn.go +++ b/cmd/yarn.go @@ -17,20 +17,20 @@ func NewYarnDoctor() *YarnDoctor { func (d *YarnDoctor) 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(slicedParams) 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, @@ -43,22 +43,22 @@ func (d *YarnDoctor) Diagnose(r parser_io.ReadSeekerAt, year int, ignores []stri } } - 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 *YarnDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner { - var nameWithOwners []github.NameWithOwner +func (d *YarnDoctor) FetchRepositoryParams(r parser_io.ReadSeekerAt) []github.FetchRepositoryParam { + var params []github.FetchRepositoryParam libs, _, _ := yarn.NewParser().Parse(r) nodejs := Nodejs{} @@ -67,8 +67,8 @@ func (d *YarnDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithO githubUrl, err := nodejs.fetchURLFromRegistry(lib.Name) if err != nil { - nameWithOwners = append(nameWithOwners, - github.NameWithOwner{ + params = append(params, + github.FetchRepositoryParam{ PackageName: lib.Name, CanSearch: false, }, @@ -78,8 +78,8 @@ func (d *YarnDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithO repo, err := github.ParseGitHubUrl(githubUrl) if err != nil { - nameWithOwners = append(nameWithOwners, - github.NameWithOwner{ + params = append(params, + github.FetchRepositoryParam{ PackageName: lib.Name, CanSearch: false, }, @@ -87,8 +87,8 @@ func (d *YarnDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithO continue } - nameWithOwners = append(nameWithOwners, - github.NameWithOwner{ + params = append(params, + github.FetchRepositoryParam{ Repo: repo.Repo, Owner: repo.Owner, PackageName: lib.Name, @@ -97,5 +97,5 @@ func (d *YarnDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithO ) } - return nameWithOwners + return params }