2017-08-02 17:24:03 +00:00
|
|
|
package main
|
2017-05-01 01:23:03 +00:00
|
|
|
|
2017-05-01 01:34:40 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-05-02 00:06:16 +00:00
|
|
|
"fmt"
|
2017-11-21 00:27:01 +00:00
|
|
|
"io/ioutil"
|
2017-05-01 01:34:40 +00:00
|
|
|
"net/http"
|
2017-05-02 00:06:16 +00:00
|
|
|
"os"
|
2017-05-01 01:34:40 +00:00
|
|
|
"strings"
|
|
|
|
)
|
2017-05-01 01:23:03 +00:00
|
|
|
|
2017-05-02 00:06:16 +00:00
|
|
|
// branch contains the information of a repository branch
|
|
|
|
type branch struct {
|
2017-05-09 01:09:43 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Commit struct {
|
|
|
|
SHA string `json:"sha"`
|
|
|
|
} `json:"commit"`
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type branches []branch
|
|
|
|
|
|
|
|
// Info contains the last commit sha of a repo
|
|
|
|
type Info struct {
|
2017-05-02 10:50:11 +00:00
|
|
|
Package string `json:"pkgname"`
|
|
|
|
URL string `json:"url"`
|
2017-05-02 00:06:16 +00:00
|
|
|
SHA string `json:"sha"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type infos []Info
|
|
|
|
|
2018-02-10 23:11:24 +00:00
|
|
|
// Repo contains information about the repository
|
|
|
|
type repo struct {
|
2018-02-13 17:52:33 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
FullName string `json:"full_name"`
|
2018-02-10 23:11:24 +00:00
|
|
|
DefaultBranch string `json:"default_branch"`
|
|
|
|
}
|
|
|
|
|
2017-10-14 16:11:47 +00:00
|
|
|
// createDevelDB forces yay to create a DB of the existing development packages
|
2017-08-02 17:24:03 +00:00
|
|
|
func createDevelDB() error {
|
2017-08-07 13:43:25 +00:00
|
|
|
_, _, _, remoteNames, err := filterPackages()
|
2017-05-02 00:06:16 +00:00
|
|
|
if err != nil {
|
2017-08-02 17:24:03 +00:00
|
|
|
return err
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
2017-08-02 17:24:03 +00:00
|
|
|
|
|
|
|
config.NoConfirm = true
|
2018-01-17 21:48:23 +00:00
|
|
|
arguments := makeArguments()
|
2018-02-08 22:51:43 +00:00
|
|
|
arguments.addArg("gendb")
|
2018-01-17 21:48:23 +00:00
|
|
|
arguments.addTarget(remoteNames...)
|
|
|
|
err = install(arguments)
|
2017-08-02 17:24:03 +00:00
|
|
|
return err
|
2017-05-01 01:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-14 16:11:47 +00:00
|
|
|
// parseSource returns owner and repo from source
|
2017-08-02 17:24:03 +00:00
|
|
|
func parseSource(source string) (owner string, repo string) {
|
2017-05-09 01:09:43 +00:00
|
|
|
if !(strings.Contains(source, "git://") ||
|
|
|
|
strings.Contains(source, ".git") ||
|
|
|
|
strings.Contains(source, "git+https://")) {
|
|
|
|
return
|
|
|
|
}
|
2017-05-01 01:23:03 +00:00
|
|
|
split := strings.Split(source, "github.com/")
|
|
|
|
if len(split) > 1 {
|
|
|
|
secondSplit := strings.Split(split[1], "/")
|
|
|
|
if len(secondSplit) > 1 {
|
|
|
|
owner = secondSplit[0]
|
|
|
|
thirdSplit := strings.Split(secondSplit[1], ".git")
|
|
|
|
if len(thirdSplit) > 0 {
|
|
|
|
repo = thirdSplit[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-05-01 01:34:40 +00:00
|
|
|
|
2017-05-02 10:50:11 +00:00
|
|
|
func (info *Info) needsUpdate() bool {
|
2018-02-10 23:11:24 +00:00
|
|
|
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 infoResp.Body.Close()
|
|
|
|
|
|
|
|
infoBody, _ := ioutil.ReadAll(infoResp.Body)
|
|
|
|
var err = json.Unmarshal(infoBody, &newRepo)
|
2017-05-02 10:50:11 +00:00
|
|
|
if err != nil {
|
2018-02-10 23:11:24 +00:00
|
|
|
fmt.Printf("Cannot update '%v'\nError: %v\nStatus code: %v\nBody: %v\n",
|
|
|
|
info.Package, err, infoResp.StatusCode, string(infoBody))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultBranch := newRepo.DefaultBranch
|
|
|
|
branchesURL := info.URL + "/branches"
|
|
|
|
|
|
|
|
branchResp, branchErr := http.Get(branchesURL)
|
|
|
|
if branchErr != nil {
|
|
|
|
fmt.Println(branchErr)
|
2017-05-02 10:50:11 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-02-10 23:11:24 +00:00
|
|
|
defer branchResp.Body.Close()
|
2017-05-02 00:06:16 +00:00
|
|
|
|
2018-02-10 23:11:24 +00:00
|
|
|
branchBody, _ := ioutil.ReadAll(branchResp.Body)
|
|
|
|
err = json.Unmarshal(branchBody, &newBranches)
|
2017-05-02 10:50:11 +00:00
|
|
|
if err != nil {
|
2017-11-21 00:27:01 +00:00
|
|
|
fmt.Printf("Cannot update '%v'\nError: %v\nStatus code: %v\nBody: %v\n",
|
2018-02-10 23:11:24 +00:00
|
|
|
info.Package, err, branchResp.StatusCode, string(branchBody))
|
2017-05-02 10:50:11 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-10 23:11:24 +00:00
|
|
|
for _, e := range newBranches {
|
|
|
|
if e.Name == defaultBranch {
|
2017-10-19 02:30:37 +00:00
|
|
|
return e.Commit.SHA != info.SHA
|
2017-05-02 10:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-06-11 20:50:39 +00:00
|
|
|
func inStore(pkgName string) *Info {
|
2017-05-02 00:06:16 +00:00
|
|
|
for i, e := range savedInfo {
|
2017-06-11 20:50:39 +00:00
|
|
|
if pkgName == e.Package {
|
2017-05-02 00:06:16 +00:00
|
|
|
return &savedInfo[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-14 16:11:47 +00:00
|
|
|
// branchInfo updates saved information
|
2018-02-10 23:11:24 +00:00
|
|
|
func branchInfo(pkgName string, owner string, repoName string) (err error) {
|
2018-02-27 12:06:30 +00:00
|
|
|
updated := false
|
2018-02-10 23:11:24 +00:00
|
|
|
var newRepo repo
|
|
|
|
var newBranches branches
|
|
|
|
url := "https://api.github.com/repos/" + owner + "/" + repoName
|
|
|
|
repoResp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer repoResp.Body.Close()
|
|
|
|
|
|
|
|
_ = json.NewDecoder(repoResp.Body).Decode(&newRepo)
|
|
|
|
defaultBranch := newRepo.DefaultBranch
|
2018-02-13 17:52:33 +00:00
|
|
|
branchesURL := url + "/branches"
|
2018-02-10 23:11:24 +00:00
|
|
|
|
2018-02-13 17:52:33 +00:00
|
|
|
branchResp, err := http.Get(branchesURL)
|
2017-05-01 01:34:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-02-10 23:11:24 +00:00
|
|
|
defer branchResp.Body.Close()
|
2017-05-01 01:34:40 +00:00
|
|
|
|
2018-02-10 23:11:24 +00:00
|
|
|
_ = json.NewDecoder(branchResp.Body).Decode(&newBranches)
|
2017-05-01 01:34:40 +00:00
|
|
|
|
2017-06-11 20:50:39 +00:00
|
|
|
packinfo := inStore(pkgName)
|
2017-05-02 00:06:16 +00:00
|
|
|
|
2018-02-10 23:11:24 +00:00
|
|
|
for _, e := range newBranches {
|
|
|
|
if e.Name == defaultBranch {
|
2018-02-27 12:06:30 +00:00
|
|
|
updated = true
|
|
|
|
|
2017-05-02 00:06:16 +00:00
|
|
|
if packinfo != nil {
|
2017-05-16 15:53:15 +00:00
|
|
|
packinfo.Package = pkgName
|
2017-05-02 10:50:11 +00:00
|
|
|
packinfo.URL = url
|
2017-05-09 01:09:43 +00:00
|
|
|
packinfo.SHA = e.Commit.SHA
|
2017-05-02 00:06:16 +00:00
|
|
|
} else {
|
2017-05-16 15:53:15 +00:00
|
|
|
savedInfo = append(savedInfo, Info{Package: pkgName, URL: url, SHA: e.Commit.SHA})
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 12:06:30 +00:00
|
|
|
if updated {
|
|
|
|
saveVCSInfo()
|
|
|
|
}
|
|
|
|
|
2017-05-01 01:34:40 +00:00
|
|
|
return
|
|
|
|
}
|
2017-05-02 10:50:11 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
func saveVCSInfo() error {
|
2017-08-04 09:26:53 +00:00
|
|
|
marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
|
2017-05-09 13:44:34 +00:00
|
|
|
if err != nil || string(marshalledinfo) == "null" {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-20 23:41:55 +00:00
|
|
|
in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
2017-05-02 10:50:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
_, err = in.Write(marshalledinfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = in.Sync()
|
|
|
|
return err
|
|
|
|
}
|