|
| 1 | +package fixtures |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "regexp" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/go-git/go-git/v5" |
| 16 | + "github.com/google/go-github/github" |
| 17 | + "golang.org/x/oauth2" |
| 18 | + "gopkg.in/yaml.v2" |
| 19 | +) |
| 20 | + |
| 21 | +// CloneRepo method to clone dev-analytics-api repo as it contains the fixtures files |
| 22 | +func CloneRepo() error { |
| 23 | + token := os.Getenv("GITHUB_OAUTH_TOKEN") |
| 24 | + stage := os.Getenv("STAGE") |
| 25 | + repo := "github.com/LF-Engineering/dev-analytics-api.git" |
| 26 | + url := fmt.Sprintf("https://%s@%s", token, repo) |
| 27 | + options := git.CloneOptions{ |
| 28 | + URL: url, |
| 29 | + Progress: os.Stdout, |
| 30 | + } |
| 31 | + _, err := git.PlainClone("./data", false, &options) |
| 32 | + if err != nil { |
| 33 | + log.Printf("Error cloning repo: %+v", err) |
| 34 | + } |
| 35 | + |
| 36 | + // checkout branch will be test or prod based on STAGE env var |
| 37 | + cmd := exec.Command("git", "checkout", stage) |
| 38 | + cmd.Dir = "./data" |
| 39 | + err = cmd.Run() |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + // if repo cloned already pull latest |
| 45 | + cmd = exec.Command("git", "pull", "origin", stage) |
| 46 | + cmd.Dir = "./data" |
| 47 | + err = cmd.Run() |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + return err |
| 53 | +} |
| 54 | + |
| 55 | +// GetYamlFiles returns all the fixture yaml files |
| 56 | +func GetYamlFiles() []string { |
| 57 | + var result = make([]string, 0) |
| 58 | + err := filepath.Walk("./data/app/services/lf/bootstrap/fixtures", |
| 59 | + func(path string, info os.FileInfo, err error) error { |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + if filepath.Ext(path) == ".yaml" { |
| 64 | + result = append(result, path) |
| 65 | + } |
| 66 | + return nil |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + log.Printf("Error listing yaml files: %+v", err) |
| 70 | + } |
| 71 | + return result |
| 72 | +} |
| 73 | + |
| 74 | +// ParseYamlFile reads the fixture yaml file and returns the model |
| 75 | +func ParseYamlFile(path string) Fixture { |
| 76 | + a, err := ioutil.ReadFile(path) |
| 77 | + var f Fixture |
| 78 | + if err != nil { |
| 79 | + log.Printf("Error parsing yaml file: %+v", err) |
| 80 | + return f |
| 81 | + } |
| 82 | + err = yaml.Unmarshal(a, &f) |
| 83 | + if err != nil { |
| 84 | + log.Printf("Error unmarshalling yaml file: %+v", err) |
| 85 | + } |
| 86 | + return f |
| 87 | +} |
| 88 | + |
| 89 | +// FilterUniqueList returns unique elements in the data array |
| 90 | +func FilterUniqueList(data []string) []string { |
| 91 | + occured := map[string]bool{} |
| 92 | + result := []string{} |
| 93 | + for element := range data { |
| 94 | + // check if already the mapped variable is set to true (exists) or not |
| 95 | + if occured[data[element]] != true { |
| 96 | + occured[data[element]] = true |
| 97 | + // Append to result slice. |
| 98 | + result = append(result, data[element]) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return result |
| 103 | +} |
| 104 | + |
| 105 | +// ParseOrg returns the org name by parsing the input param |
| 106 | +func ParseOrg(endPointName string) string { |
| 107 | + arr := strings.Split(endPointName, "/") |
| 108 | + ary := []string{} |
| 109 | + l := len(arr) - 1 |
| 110 | + for i, s := range arr { |
| 111 | + if i == l && s == "" { |
| 112 | + break |
| 113 | + } |
| 114 | + ary = append(ary, s) |
| 115 | + } |
| 116 | + lAry := len(ary) |
| 117 | + org := ary[lAry-1] |
| 118 | + return org |
| 119 | +} |
| 120 | + |
| 121 | +// GetGithubRepoList returns the list of repositories for a given endpoint |
| 122 | +func GetGithubRepoList(endPointName string, skipREs []*regexp.Regexp) ([]string, error) { |
| 123 | + token := os.Getenv("GITHUB_OAUTH_TOKEN") |
| 124 | + ctx := context.Background() |
| 125 | + ts := oauth2.StaticTokenSource( |
| 126 | + &oauth2.Token{AccessToken: token}, |
| 127 | + ) |
| 128 | + tc := oauth2.NewClient(ctx, ts) |
| 129 | + |
| 130 | + client := github.NewClient(tc) |
| 131 | + url, err := url.Parse(endPointName) |
| 132 | + if err != nil { |
| 133 | + log.Printf("Error parsing endpoint url: %+v", err) |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + repos := []string{} |
| 137 | + // list public repositories for org "github" |
| 138 | + opt := &github.RepositoryListByOrgOptions{Type: "public"} |
| 139 | + opt.PerPage = 100 |
| 140 | + for { |
| 141 | + repoList, response, err := client.Repositories.ListByOrg(ctx, strings.TrimLeft(url.Path, "/"), opt) |
| 142 | + if err != nil { |
| 143 | + log.Printf("Error fetching repo list: %+v", err) |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + if len(repoList) > 0 { |
| 147 | + org := ParseOrg(endPointName) |
| 148 | + for _, repo := range repoList { |
| 149 | + if repo.Name != nil { |
| 150 | + name := org + "/" + *(repo.Name) |
| 151 | + if CheckSkipped(endPointName, skipREs, name) { |
| 152 | + repos = append(repos, *(repo.HTMLURL)) |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + if response.NextPage == 0 { |
| 158 | + break |
| 159 | + } |
| 160 | + opt.Page = response.NextPage |
| 161 | + } |
| 162 | + return repos, nil |
| 163 | +} |
| 164 | + |
| 165 | +// CheckSkipped verifies whether a particular repo is in the skipped list or not. |
| 166 | +func CheckSkipped(endPointName string, skipREs []*regexp.Regexp, repo string) bool { |
| 167 | + included := true |
| 168 | + for _, skipRE := range skipREs { |
| 169 | + if skipRE.MatchString(repo) { |
| 170 | + included = false |
| 171 | + log.Printf("%s: skipped %s (%v)\n", endPointName, repo, skipRE) |
| 172 | + break |
| 173 | + } |
| 174 | + } |
| 175 | + return included |
| 176 | +} |
0 commit comments