From 74714783525f77d197f4a76ade58370e59915eec Mon Sep 17 00:00:00 2001 From: Brian Allred Date: Sat, 10 Feb 2018 17:11:24 -0600 Subject: [PATCH] Make devel operations respect default branch --- vcs.go | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 171 insertions(+), 18 deletions(-) diff --git a/vcs.go b/vcs.go index 6b1152ec..02a4ab70 100644 --- a/vcs.go +++ b/vcs.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "strings" + "time" ) // branch contains the information of a repository branch @@ -29,6 +30,125 @@ type Info struct { type infos []Info +// Repo contains information about the repository +type repo struct { + ID int `json:"id"` + Name string `json:"name"` + FullName string `json:"full_name"` + Owner struct { + Login string `json:"login"` + ID int `json:"id"` + AvatarURL string `json:"avatar_url"` + GravatarID string `json:"gravatar_id"` + URL string `json:"url"` + HTMLURL string `json:"html_url"` + FollowersURL string `json:"followers_url"` + FollowingURL string `json:"following_url"` + GistsURL string `json:"gists_url"` + StarredURL string `json:"starred_url"` + SubscriptionsURL string `json:"subscriptions_url"` + OrganizationsURL string `json:"organizations_url"` + ReposURL string `json:"repos_url"` + EventsURL string `json:"events_url"` + ReceivedEventsURL string `json:"received_events_url"` + Type string `json:"type"` + SiteAdmin bool `json:"site_admin"` + } `json:"owner"` + Private bool `json:"private"` + HTMLURL string `json:"html_url"` + Description string `json:"description"` + Fork bool `json:"fork"` + URL string `json:"url"` + ForksURL string `json:"forks_url"` + KeysURL string `json:"keys_url"` + CollaboratorsURL string `json:"collaborators_url"` + TeamsURL string `json:"teams_url"` + HooksURL string `json:"hooks_url"` + IssueEventsURL string `json:"issue_events_url"` + EventsURL string `json:"events_url"` + AssigneesURL string `json:"assignees_url"` + BranchesURL string `json:"branches_url"` + TagsURL string `json:"tags_url"` + BlobsURL string `json:"blobs_url"` + GitTagsURL string `json:"git_tags_url"` + GitRefsURL string `json:"git_refs_url"` + TreesURL string `json:"trees_url"` + StatusesURL string `json:"statuses_url"` + LanguagesURL string `json:"languages_url"` + StargazersURL string `json:"stargazers_url"` + ContributorsURL string `json:"contributors_url"` + SubscribersURL string `json:"subscribers_url"` + SubscriptionURL string `json:"subscription_url"` + CommitsURL string `json:"commits_url"` + GitCommitsURL string `json:"git_commits_url"` + CommentsURL string `json:"comments_url"` + IssueCommentURL string `json:"issue_comment_url"` + ContentsURL string `json:"contents_url"` + CompareURL string `json:"compare_url"` + MergesURL string `json:"merges_url"` + ArchiveURL string `json:"archive_url"` + DownloadsURL string `json:"downloads_url"` + IssuesURL string `json:"issues_url"` + PullsURL string `json:"pulls_url"` + MilestonesURL string `json:"milestones_url"` + NotificationsURL string `json:"notifications_url"` + LabelsURL string `json:"labels_url"` + ReleasesURL string `json:"releases_url"` + DeploymentsURL string `json:"deployments_url"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + PushedAt time.Time `json:"pushed_at"` + GitURL string `json:"git_url"` + SSHURL string `json:"ssh_url"` + CloneURL string `json:"clone_url"` + SvnURL string `json:"svn_url"` + Homepage string `json:"homepage"` + Size int `json:"size"` + StargazersCount int `json:"stargazers_count"` + WatchersCount int `json:"watchers_count"` + Language string `json:"language"` + HasIssues bool `json:"has_issues"` + HasProjects bool `json:"has_projects"` + HasDownloads bool `json:"has_downloads"` + HasWiki bool `json:"has_wiki"` + HasPages bool `json:"has_pages"` + ForksCount int `json:"forks_count"` + MirrorURL interface{} `json:"mirror_url"` + Archived bool `json:"archived"` + OpenIssuesCount int `json:"open_issues_count"` + License struct { + Key string `json:"key"` + Name string `json:"name"` + SpdxID interface{} `json:"spdx_id"` + URL interface{} `json:"url"` + } `json:"license"` + Forks int `json:"forks"` + OpenIssues int `json:"open_issues"` + Watchers int `json:"watchers"` + DefaultBranch string `json:"default_branch"` + Organization struct { + Login string `json:"login"` + ID int `json:"id"` + AvatarURL string `json:"avatar_url"` + GravatarID string `json:"gravatar_id"` + URL string `json:"url"` + HTMLURL string `json:"html_url"` + FollowersURL string `json:"followers_url"` + FollowingURL string `json:"following_url"` + GistsURL string `json:"gists_url"` + StarredURL string `json:"starred_url"` + SubscriptionsURL string `json:"subscriptions_url"` + OrganizationsURL string `json:"organizations_url"` + ReposURL string `json:"repos_url"` + EventsURL string `json:"events_url"` + ReceivedEventsURL string `json:"received_events_url"` + Type string `json:"type"` + SiteAdmin bool `json:"site_admin"` + } `json:"organization"` + NetworkCount int `json:"network_count"` + SubscribersCount int `json:"subscribers_count"` +} + // createDevelDB forces yay to create a DB of the existing development packages func createDevelDB() error { _, _, _, remoteNames, err := filterPackages() @@ -66,24 +186,46 @@ func parseSource(source string) (owner string, repo string) { } func (info *Info) needsUpdate() bool { - var newRepo branches - r, err := http.Get(info.URL) - if err != nil { - fmt.Println(err) + var newRepo repo + var newBranches branches + if strings.HasSuffix(info.URL, "/branches") { + info.URL = info.URL[:len(info.URL)-9] + } + infoResp, infoErr := http.Get(info.URL) + if infoErr != nil { + fmt.Println(infoErr) return false } - defer r.Body.Close() + defer infoResp.Body.Close() - body, _ := ioutil.ReadAll(r.Body) - err = json.Unmarshal(body, &newRepo) + infoBody, _ := ioutil.ReadAll(infoResp.Body) + var err = json.Unmarshal(infoBody, &newRepo) if err != nil { fmt.Printf("Cannot update '%v'\nError: %v\nStatus code: %v\nBody: %v\n", - info.Package, err, r.StatusCode, string(body)) + info.Package, err, infoResp.StatusCode, string(infoBody)) return false } - for _, e := range newRepo { - if e.Name == "master" { + defaultBranch := newRepo.DefaultBranch + branchesURL := info.URL + "/branches" + + branchResp, branchErr := http.Get(branchesURL) + if branchErr != nil { + fmt.Println(branchErr) + return false + } + defer branchResp.Body.Close() + + branchBody, _ := ioutil.ReadAll(branchResp.Body) + err = json.Unmarshal(branchBody, &newBranches) + if err != nil { + fmt.Printf("Cannot update '%v'\nError: %v\nStatus code: %v\nBody: %v\n", + info.Package, err, branchResp.StatusCode, string(branchBody)) + return false + } + + for _, e := range newBranches { + if e.Name == defaultBranch { return e.Commit.SHA != info.SHA } } @@ -100,22 +242,33 @@ func inStore(pkgName string) *Info { } // branchInfo updates saved information -func branchInfo(pkgName string, owner string, repo string) (err error) { +func branchInfo(pkgName string, owner string, repoName string) (err error) { updated = true - var newRepo branches - url := "https://api.github.com/repos/" + owner + "/" + repo + "/branches" - r, err := http.Get(url) + var newRepo repo + var newBranches branches + url := "https://api.github.com/repos/" + owner + "/" + repoName + repoResp, err := http.Get(url) if err != nil { return } - defer r.Body.Close() + defer repoResp.Body.Close() - _ = json.NewDecoder(r.Body).Decode(&newRepo) + _ = json.NewDecoder(repoResp.Body).Decode(&newRepo) + defaultBranch := newRepo.DefaultBranch + branchesUrl := url + "/branches" + + branchResp, err := http.Get(branchesUrl) + if err != nil { + return + } + defer branchResp.Body.Close() + + _ = json.NewDecoder(branchResp.Body).Decode(&newBranches) packinfo := inStore(pkgName) - for _, e := range newRepo { - if e.Name == "master" { + for _, e := range newBranches { + if e.Name == defaultBranch { if packinfo != nil { packinfo.Package = pkgName packinfo.URL = url