/
repository_git.go
100 lines (75 loc) · 2.19 KB
/
repository_git.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package pipelines
import (
"fmt"
"github.com/datatok/tintin/pkg/utils/links"
"github.com/go-git/go-git/v5"
"github.com/sirupsen/logrus"
"io/fs"
"os"
"path/filepath"
"strings"
)
type RepositoryGit struct {
URL, path, workingDir string
linksRepository links.Repository
}
func (s *RepositoryGit) getDefinitions() ([]Definition, error) {
var (
ret []Definition
candidatesPath []string
)
if len(s.URL) > 0 {
logrus.Debugf("Cloning repo into %s", s.workingDir)
_, err := git.PlainClone(s.workingDir, false, &git.CloneOptions{
URL: s.URL,
InsecureSkipTLS: os.Getenv("GIT_SSL_NO_VERIFY") == "true",
Depth: 1,
SingleBranch: true,
})
if err != nil {
logrus.Fatal(err)
}
}
searchPath := filepath.Join(s.workingDir, s.path)
workingDirSanitized := sanitizePath(searchPath)
logrus.Debug("Searching pipeline.yaml files")
filepath.Walk(searchPath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return nil
}
if !info.IsDir() && (filepath.Base(path) == "pipeline.yml" || filepath.Base(path) == "pipeline.yaml") {
candidatesPath = append(candidatesPath, path)
}
return nil
})
for _, candidatePath := range candidatesPath {
candidatePathSanitized := sanitizePath(candidatePath)
pp := strings.Replace(candidatePathSanitized, workingDirSanitized, "", 1)
fullName := strings.Trim(filepath.Dir(strings.Replace(candidatePathSanitized, workingDirSanitized, "", 1)), "/")
team := "steam"
name := fullName
if strings.Contains(fullName, "/") {
team = strings.Split(fullName, "/")[0]
name = strings.Trim(strings.TrimLeft(fullName, team), "/")
}
def := defaultPipelineDefinition(candidatePath, fullName, name, team, s.linksRepository.Generate(GitlabURL, map[string]string{"uri": pp}))
dat, err := os.ReadFile(candidatePath)
if err != nil {
logrus.Fatal(err)
}
def.parsePipeline(dat)
ret = append(ret, def)
}
logrus.Debugf("removing %s", s.workingDir)
err := os.RemoveAll(s.workingDir)
if err != nil {
logrus.Fatal(err)
}
return ret, nil
}
func sanitizePath(p string) string {
p = strings.TrimLeft(p, "./")
p = strings.TrimLeft(p, "../")
return p
}