-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_repository.go
75 lines (66 loc) · 2.21 KB
/
parse_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"encoding/json"
"fmt"
"time"
)
// RepositoryGithubDto have important element from repositories list
type RepositoryGithubDto struct {
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
Owner struct {
OwnerName string `json:"login"`
AvatarURL string `json:"avatar_url"`
Type string `json:"type"`
}
LanguageURL string `json:"languages_url"`
URL string `json:"html_url"`
}
type repositorySearchGithubDto struct {
Items []RepositoryGithubDto
}
// Repository structure of repository used and sent to the client
type Repository struct {
Name string
FullName string
Description string
OwnerName string
AvatarURL string
Type string
URL string
Languages map[string]float64
}
// ContainsLanguage check if the current repository contains a searched language
func (repo *Repository) ContainsLanguage(searchedLanguage string) bool {
for lang := range repo.Languages {
if lang == searchedLanguage {
return true
}
}
return false
}
// FetchRepositoriesList will fetch 100 last public repositories
func FetchRepositoriesList(querySearch ...string) (*[]RepositoryGithubDto, error) {
// Github API doesn't offer possibility to fetch last created repositories
// To get them we have to set 'q' parameter, we search by date (D-1) and sort by stars arbitrary
// to activate order desc and to be sure to load 100 last repositories
// https://developer.github.com/v3/search/#search-repositories
if len(querySearch) <= 1 && querySearch[0] == "" {
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
querySearch = append(querySearch, "q=created:"+yesterday)
}
querySearch = append(querySearch, "order=desc", "sort=stars")
body, err := FetchAPI("search/repositories", querySearch...)
if err != nil {
return nil, fmt.Errorf("Failed to fetch repositories", err)
}
repositoriesSearch := &repositorySearchGithubDto{}
err = json.Unmarshal(body, &repositoriesSearch)
if err != nil {
return nil, fmt.Errorf("Request returned bad JSON", err, "-", string(body))
}
repositories := &repositoriesSearch.Items
fmt.Println("Fetched", len(*repositories), "repositories")
return repositories, nil
}