forked from segmentio/analytics.js-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonorepo.go
208 lines (168 loc) · 5.3 KB
/
monorepo.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package operations
import (
"fmt"
"path"
"path/filepath"
"regexp"
"text/template"
"gopkg.in/libgit2/git2go.v27"
)
const integrationsFolder = "integrations"
const monorepoURL = "https://github.com/segmentio/analytics.js-integrations"
const commitTemplate = `
Add integration {{ .Name }}
This commit copies the content of the integration repo into
the "integrations" folder.
Original repo: {{ .URL }}
Readme: {{ .Readme }}
`
var ignorePaths = map[string]bool{
".circleci": true,
".eslintrc": true,
".git": true,
".gitignore": true,
".github": true,
"circle.yml": true,
"karma.conf.ci.js": true,
"karma.conf.js": true,
"CONTRIBUTING.md": true,
"LICENSE": true,
"Makefile": true,
"package-lock.json": true,
"yarn.lock": true,
}
// Monorepo
type Monorepo struct {
Project
IntegrationsPath string
repo *git.Repository
path string
commitTmpl *template.Template
}
// OpenMonorepo returns the git repository of the monorepo
func OpenMonorepo(path string) (*Monorepo, error) {
Debug("Opening monorepo from %s", path)
repo, err := git.OpenRepository(path)
if err != nil {
LogError(err, "Error opening monorepo")
return nil, err
}
return &Monorepo{
IntegrationsPath: integrationsFolder,
repo: repo,
path: path,
}, nil
}
// ConnectToGitHub updates the struct containing GitHub metadata. This was separated from
// `OpenMonorepo` because in some ocasions we don't need all GitHub information.
func (m *Monorepo) ConnectToGitHub(github *GitHub, organization, name string) error {
project, err := github.GetProject(organization, name)
if err != nil {
return err
}
tmpl, err := template.New("monorepo-commit").Parse(commitTemplate)
if err != nil {
LogError(err, "Error parsing template")
return err
}
m.Project = project
m.commitTmpl = tmpl
return nil
}
// AddIntegrationRepo adds the integration files into the monorepo as a folder, and commits
// the results. It returns the new commit's link.
func (m *Monorepo) AddIntegrationRepo(integration IntegrationRepo) (string, error) {
Debug("Adding %s to the monorepo", integration.Name)
// Copy files into /integrations/<integration>
src := path.Join(integration.Repo.Path(), "..")
dst := path.Join(m.repo.Path(), "..", m.IntegrationsPath, integration.Name)
exists, err := fileExists(dst)
if err != nil {
return "", err
}
if exists {
// already added
return "monorepoURL", nil
}
if err := copyFiles(src, dst, ignorePaths); err != nil {
return "", err
}
if err := m.updatePackageURLs(integration.Name, dst); err != nil {
return "", err
}
oid, err := commitFiles(m.repo, []string{m.IntegrationsPath}, m.commitMigrationMessage(integration))
if err != nil {
return "", err
}
return monorepoURL + "/commit/" + oid.String(), nil
}
func (m *Monorepo) commitMigrationMessage(integration IntegrationRepo) string {
info := struct {
Name, URL, Readme string
}{
Name: integration.Name,
URL: integration.URL,
Readme: integration.URL + "/blob/master/README.md",
}
return executeTemplate(m.commitTmpl, info)
}
// ListUpdatedIntegrationsSinceCommit compare the current state of the integration with a commit (oid or ref)
// and return the integrations that have change.
func (m *Monorepo) ListUpdatedIntegrationsSinceCommit(commitID string) (map[string]*Integration, error) {
diffRegexp := regexp.MustCompilePOSIX(fmt.Sprintf(`^%s/([a-z_-]+)/.*$`, m.IntegrationsPath))
integrations, err := m.OpenAllIntegrations()
if err != nil {
return nil, err
}
commit, err := resolveCommit(commitID, m.repo)
if err != nil {
LogError(err, "Can not get commit for %s", commitID)
return nil, err
}
treeToCompare, err := commit.Tree()
if err != nil {
LogError(err, "Can not retrieve tree for commit %s", commitID)
return nil, err
}
options := &git.DiffOptions{}
diff, err := m.repo.DiffTreeToWorkdirWithIndex(treeToCompare, options)
if err != nil {
LogError(err, "Error getting diff")
return nil, err
}
updatedIntegrations := make(map[string]*Integration)
p := func(delta git.DiffDelta, _ float64) (git.DiffForEachHunkCallback, error) {
for _, file := range []string{delta.NewFile.Path, delta.OldFile.Path} {
unixFile := filepath.ToSlash(file)
matches := diffRegexp.FindAllStringSubmatch(unixFile, -1)
if len(matches) > 0 && len(matches[0]) == 2 {
name := matches[0][1]
if i, found := integrations[name]; found {
updatedIntegrations[name] = i
}
}
}
return doNothingForEachHunk, nil
}
if err := diff.ForEach(p, git.DiffDetailFiles); err != nil {
LogError(err, "Error getting diff for files")
return nil, err
}
return updatedIntegrations, nil
}
// updatePackageURLs change the repo, homepage and bugs urls for a migrated
// integration
func (m *Monorepo) updatePackageURLs(integrationName, folder string) error {
file := path.Join(folder, "package.json")
pack, err := DecodePackage(file)
if err != nil {
return err
}
pack.Repository.URL = fmt.Sprintf("git+%s.git", m.Project.URL)
pack.Bugs.URL = fmt.Sprintf("%s/issues", m.Project.URL)
pack.Homepage = fmt.Sprintf("%s/blob/master/integrations/%s#readme", m.Project.URL, integrationName)
return EncodePackage(pack, file)
}
func (m *Monorepo) getIntegrationsFolder() string {
return path.Join(m.repo.Path(), "..", m.IntegrationsPath)
}