-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
332 lines (287 loc) · 10 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/google/go-github/v69/github"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"gopkg.in/yaml.v3"
)
func main() {
repos, err := listAvailableAddons(false)
checkErr(err)
err = os.Chdir("..")
checkErr(err)
for _, repo := range repos {
err := createRepoMarkdown(repo)
if err != nil {
log.Errorf("Failed to create markdown file for %s: %v", repo.GetFullName(), err)
}
err = createIndexFile(repo)
if err != nil {
log.Errorf("Failed to create index file for %s: %v", repo.GetFullName(), err)
}
}
}
// listAvailableAddons lists the add-ons that are listed on GitHub
func listAvailableAddons(officialOnly bool) ([]*github.Repository, error) {
client := GetGithubClient(context.Background())
q := "topic:ddev-get fork:true"
if officialOnly {
q = q + " org:" + "ddev"
}
opts := &github.SearchOptions{Sort: "updated", Order: "desc", ListOptions: github.ListOptions{PerPage: 200}}
var allRepos []*github.Repository
for {
repos, resp, err := client.Search.Repositories(context.Background(), q, opts)
if err != nil {
msg := fmt.Sprintf("Unable to get list of available services: %v", err)
if resp != nil {
msg = msg + fmt.Sprintf(" rateinfo=%v", resp.Rate)
}
return nil, fmt.Errorf("%s", msg)
}
allRepos = append(allRepos, repos.Repositories...)
if resp.NextPage == 0 {
break
}
// Set the next page number for the next request
opts.ListOptions.Page = resp.NextPage
}
out := ""
for _, r := range allRepos {
out = out + fmt.Sprintf("%s: %s\n", r.GetFullName(), r.GetDescription())
}
if len(allRepos) == 0 {
return nil, fmt.Errorf("no add-ons found")
}
return allRepos, nil
}
// GetGithubClient creates the required GitHub client
func GetGithubClient(ctx context.Context) github.Client {
// Use authenticated client for higher rate limit, normally only needed for tests
githubToken := os.Getenv("DDEV_ADDON_REGISTRY_TOKEN")
if githubToken != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: githubToken},
)
tc := oauth2.NewClient(ctx, ts)
return *github.NewClient(tc)
}
return *github.NewClient(nil)
}
func checkErr(err error) {
if err != nil {
log.Panic("CheckErr(): ERROR:", err)
}
}
// createRepoMarkdown creates a markdown file for each repository in the structure _addons/<org>/<repo>.md
func createRepoMarkdown(repo *github.Repository) error {
// Define the directory and filename
org := repo.Owner.GetLogin() // Get organization/user name
repoName := repo.GetName() // Get repository name
dir := filepath.Join("_addons", org) // Create path _addons/<org>
err := os.MkdirAll(dir, os.ModePerm) // Ensure the directory exists
if err != nil {
return fmt.Errorf("failed to create directory: %v", err)
}
// Define the markdown file path
filePath := filepath.Join(dir, fmt.Sprintf("%s.md", repoName))
// Get README content from the repository
readmeContent, err := getRepoReadme(repo)
if err != nil {
log.Warnf("Could not retrieve README for repo %s: %v", repo.GetFullName(), err)
readmeContent = "README not available."
}
installYaml, err := getRepoInstallYaml(repo)
if err != nil {
return fmt.Errorf("could not retrieve install.yaml for repo %s: %v", repo.GetFullName(), err)
}
dependencies := "[]"
if len(installYaml.Dependencies) > 0 {
dependencies = fmt.Sprintf(`["%s"]`, strings.Join(installYaml.Dependencies, `", "`))
}
// Create the front matter (YAML-like header)
addonType := "contrib"
if org == "ddev" {
addonType = "official"
}
newContent := fmt.Sprintf(`---
title: %s
github_url: %s
description: "%s"
user: %s
repo: %s
repo_id: %d
ddev_version_constraint: "%s"
dependencies: %s
type: %s
created_at: %s
updated_at: %s
stars: %d
---
%s
`,
repo.GetFullName(),
repo.GetHTMLURL(),
strings.ReplaceAll(repo.GetDescription(), `"`, `\"`),
org,
repoName,
repo.GetID(),
installYaml.DdevVersionConstraint,
dependencies,
addonType,
repo.GetCreatedAt().Format(time.DateOnly),
repo.GetUpdatedAt().Format(time.DateOnly),
repo.GetStargazersCount(),
strings.TrimSpace(readmeContent),
)
if !isFileChanged(filePath, newContent) {
log.Infof("No changes for repo: %s", repo.GetFullName())
return nil
}
// Write content to the markdown file
err = os.WriteFile(filePath, []byte(newContent), 0644)
if err != nil {
return fmt.Errorf("failed to write file: %v", err)
}
log.Infof("Updated repo: %s", repo.GetFullName())
return nil
}
// createIndexFile creates a markdown file for each repository in the structure _addons/<org>/<repo>.md
func createIndexFile(repo *github.Repository) error {
// Define the directory and filename
org := repo.Owner.GetLogin() // Get organization/user name
dir := filepath.Join("_addons", org) // Create path _addons/<org>
err := os.MkdirAll(dir, os.ModePerm) // Ensure the directory exists
if err != nil {
return fmt.Errorf("failed to create directory: %v", err)
}
// Define the markdown file path
filePath := filepath.Join(dir, "index.html")
// Generate new content for the file
newContent := fmt.Sprintf(`---
layout: page
title: DDEV Add-on Registry / %s
group: %s
---
{%% include addon_table.html filter_by_user="%s" %%}
`, org, org, org)
if !isFileChanged(filePath, newContent) {
return nil
}
// Write the new content to the file
err = os.WriteFile(filePath, []byte(newContent), 0644)
if err != nil {
return fmt.Errorf("failed to write file: %v", err)
}
log.Infof("index file created or updated for org: %s", org)
return nil
}
// getRepoReadme retrieves the README.md content from the given repository
func getRepoReadme(repo *github.Repository) (string, error) {
client := GetGithubClient(context.Background())
readme, _, err := client.Repositories.GetReadme(context.Background(), repo.Owner.GetLogin(), repo.GetName(), nil)
if err != nil {
return "", fmt.Errorf("could not retrieve README: %v", err)
}
// Decode README content (GitHub API returns it as Base64-encoded)
content, err := readme.GetContent()
if err != nil {
return "", fmt.Errorf("could not decode README content: %v", err)
}
// Replace relative links with full links
updatedContent := replaceRelativeLinks(content, repo)
return updatedContent, nil
}
// replaceRelativeLinks replaces relative links with full links in the README content,
// handling both regular links and images. It ignores anchor links (e.g., "#introduction").
func replaceRelativeLinks(content string, repo *github.Repository) string {
blobURL := fmt.Sprintf("https://github.com/%s/%s/blob/%s", repo.Owner.GetLogin(), repo.GetName(), repo.GetDefaultBranch())
rawURL := fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s", repo.Owner.GetLogin(), repo.GetName(), repo.GetDefaultBranch())
// Match all Markdown links
linkRegex := regexp.MustCompile(`\[(.*?)\]\((.*?)\)`)
imageRegex := regexp.MustCompile(`!\[(.*?)\]\((.*?)\)`)
// Replace relative image links with raw.githubusercontent URL
updatedContent := imageRegex.ReplaceAllStringFunc(content, func(link string) string {
matches := imageRegex.FindStringSubmatch(link)
if len(matches) > 2 {
altText := matches[1]
relativeLink := matches[2]
// Ignore if the link starts with http or #
if strings.HasPrefix(relativeLink, "http") || strings.HasPrefix(relativeLink, "#") {
return link
}
fullLink := fmt.Sprintf("%s/%s", rawURL, strings.TrimPrefix(relativeLink, "/"))
return fmt.Sprintf("", altText, fullLink)
}
return link
})
// Replace relative links (non-image) with blob URL, excluding anchors and external links
updatedContent = linkRegex.ReplaceAllStringFunc(updatedContent, func(link string) string {
matches := linkRegex.FindStringSubmatch(link)
if len(matches) > 2 {
text := matches[1]
relativeLink := matches[2]
// Ignore if the link starts with http or #
if strings.HasPrefix(relativeLink, "http") || strings.HasPrefix(relativeLink, "#") {
return link
}
fullLink := fmt.Sprintf("%s/%s", blobURL, strings.TrimPrefix(relativeLink, "/"))
return fmt.Sprintf("[%s](%s)", text, fullLink)
}
return link
})
return updatedContent
}
// isFileChanged checks if a file has changed
func isFileChanged(filePath string, newContent string) bool {
if _, err := os.Stat(filePath); err == nil {
existingContent, err := os.ReadFile(filePath)
if err != nil {
return true
}
if string(existingContent) == newContent {
return false
}
}
return true
}
type InstallDesc struct {
// Name must be unique in a project; it will overwrite any existing add-on with the same name.
Name string `yaml:"name"`
ProjectFiles []string `yaml:"project_files"`
GlobalFiles []string `yaml:"global_files,omitempty"`
DdevVersionConstraint string `yaml:"ddev_version_constraint,omitempty"`
Dependencies []string `yaml:"dependencies,omitempty"`
PreInstallActions []string `yaml:"pre_install_actions,omitempty"`
PostInstallActions []string `yaml:"post_install_actions,omitempty"`
RemovalActions []string `yaml:"removal_actions,omitempty"`
YamlReadFiles map[string]string `yaml:"yaml_read_files"`
}
// getRepoInstallYaml retrieves and parses the install.yaml content from the given repository
func getRepoInstallYaml(repo *github.Repository) (*InstallDesc, error) {
client := GetGithubClient(context.Background())
// Fetch install.yaml from the repo (adjust the path if necessary)
fileContent, _, _, err := client.Repositories.GetContents(context.Background(), repo.Owner.GetLogin(), repo.GetName(), "install.yaml", nil)
if err != nil {
return nil, fmt.Errorf("could not retrieve install.yaml: %v", err)
}
// Decode the file content (GitHub API returns Base64-encoded content)
content, err := fileContent.GetContent()
if err != nil {
return nil, fmt.Errorf("could not decode install.yaml content: %v", err)
}
// Parse the YAML content
var parsedYaml InstallDesc
err = yaml.Unmarshal([]byte(content), &parsedYaml)
if err != nil {
return nil, fmt.Errorf("could not parse install.yaml: %v", err)
}
return &parsedYaml, nil
}