Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit c63ca56

Browse files
authored
DA-3256: (#12)
adding fixtures related common methods
1 parent 805c804 commit c63ca56

File tree

3 files changed

+236
-0
lines changed

3 files changed

+236
-0
lines changed

fixtures/models.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package fixtures
2+
3+
import "regexp"
4+
5+
// Fixture struct represents a fixture model
6+
type Fixture struct {
7+
Native struct {
8+
Slug string `yaml:"slug"`
9+
} `yaml:"native"`
10+
DataSources []struct {
11+
Slug string `yaml:"slug"`
12+
Projects []struct {
13+
Name string `yaml:"name"`
14+
Endpoints []struct {
15+
Name string `yaml:"name"`
16+
Flags struct {
17+
Type string `yaml:"type"`
18+
} `yaml:"flags"`
19+
Skip []string `yaml:"skip"`
20+
SkipREs []*regexp.Regexp `yaml:"-"`
21+
} `yaml:"endpoints"`
22+
Flags interface{} `yaml:"flags"`
23+
} `yaml:"projects,omitempty"`
24+
Config []struct {
25+
Name string `yaml:"name"`
26+
Value string `yaml:"value"`
27+
} `yaml:"config,omitempty"`
28+
MaxFrequency string `yaml:"max_frequency,omitempty"`
29+
} `yaml:"data_sources"`
30+
}
31+
32+
// Repo struct represents a single repo model
33+
type Repo struct {
34+
Htmlurl string `json:"html_url"`
35+
}
36+
37+
// RepoList represents list of Repos
38+
type RepoList []Repo

fixtures/util.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
}

fixtures/util_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package fixtures
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// Check the format date util method returns expected computed value
10+
func TestFilterUniqueList(t *testing.T) {
11+
data := []string{"A1", "A2", "A3", "A4", "A2", "A5", "A3"}
12+
expected := []string{"A1", "A2", "A3", "A4", "A5"}
13+
result := FilterUniqueList(data)
14+
assert.Equal(t, result, expected)
15+
}
16+
17+
func TestParseOrg(t *testing.T) {
18+
endPoint := "https://github.com/stretchr"
19+
expected := "stretchr"
20+
result := ParseOrg(endPoint)
21+
assert.Equal(t, result, expected)
22+
}

0 commit comments

Comments
 (0)