-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (134 loc) · 4.01 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
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"regexp"
"strings"
"github.com/caarlos0/env/v11"
"github.com/google/go-github/v67/github"
"golang.org/x/oauth2"
)
type Configuration struct {
Token string `env:"INPUT_TOKEN"`
Owner string `env:"INPUT_OWNER"`
Repository string `env:"INPUT_REPOSITORY"`
Size int `env:"INPUT_SIZE" envDefault:"50"`
File string `env:"INPUT_FILE" envDefault:"README.md"`
Limit int `env:"INPUT_LIMIT" envDefault:"70"`
LogLevel string `env:"INPUT_LOG_LEVEL" envDefault:"info"`
}
type Contributor struct {
Username string
Avatar string
Profile string
}
const guard string = "[//]: kontrolplane/generate-contributors-list"
func main() {
cfg := Configuration{}
ctx := context.Background()
err := env.Parse(&cfg)
if err != nil {
fmt.Println("unable to parse the environment variables", err)
os.Exit(1)
}
var ll slog.Level
if cfg.LogLevel == "debug" {
ll = slog.LevelDebug
} else {
ll = slog.LevelInfo
}
logHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: false,
Level: ll,
})
logger := slog.New(logHandler)
var client *github.Client
if cfg.Token != "" {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: cfg.Token})
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
} else {
client = github.NewClient(nil)
}
contributors, err := fetchContributors(cfg, ctx, client)
if err != nil {
logger.Error("error fetching contributors", slog.Any("error", err))
os.Exit(1)
}
file, err := os.ReadFile(cfg.File)
if err != nil {
logger.Error("error reading file", slog.Any("error", err))
os.Exit(1)
}
updatedContent, err := updateContributors(cfg, contributors, string(file))
if err != nil {
logger.Error("updating contributors failed", slog.Any("error", err))
os.Exit(1)
}
err = os.WriteFile(cfg.File, []byte(updatedContent), 0644)
if err != nil {
logger.Error("error writing file", slog.Any("error", err))
os.Exit(1)
}
logger.Debug(updatedContent)
logger.Info("contributors section updated successfully")
}
func fetchContributors(cfg Configuration, ctx context.Context, client *github.Client) ([]Contributor, error) {
clientOptions := &github.ListContributorsOptions{
ListOptions: github.ListOptions{PerPage: cfg.Limit},
}
var contributors []Contributor
for {
contributorList, resp, err := client.Repositories.ListContributors(ctx, cfg.Owner, cfg.Repository, clientOptions)
if err != nil {
return nil, err
}
for _, contributor := range contributorList {
contributors = append(contributors, Contributor{
Username: contributor.GetLogin(),
Avatar: contributor.GetAvatarURL(),
Profile: fmt.Sprintf("https://github.com/%s", contributor.GetLogin()),
})
}
if resp.NextPage == 0 {
break
}
clientOptions.Page = resp.NextPage
}
return contributors, nil
}
func updateContributors(cfg Configuration, contributors []Contributor, content string) (string, error) {
r := regexp.MustCompile(fmt.Sprintf(`(?s)%s\n*.*?\n*%s`, regexp.QuoteMeta(guard), regexp.QuoteMeta(guard)))
contributorsHTML := generateContributors(cfg, contributors)
if r.MatchString(content) {
content = r.ReplaceAllString(content, fmt.Sprintf("%s\n\n%s\n\n%s", guard, contributorsHTML, guard))
} else {
// todo: possible option to add it to the content regardless of guards
// content += fmt.Sprintf("\n\n%s\n%s\n%s", guard, contributorsHTML, guard)
return "", errors.New("no guards found used for protecting the markdown file")
}
return content, nil
}
func generateContributors(cfg Configuration, contributors []Contributor) string {
var htmlBuilder strings.Builder
for i, contributor := range contributors {
htmlBuilder.WriteString(fmt.Sprintf(
`<a href="%s"><img src="%s" title="%s" width="%d" height="%d"></a>%s`,
contributor.Profile,
contributor.Avatar,
contributor.Username,
cfg.Size,
cfg.Size,
func() string {
if i < len(contributors)-1 {
return "\n"
}
return ""
}(),
))
}
return htmlBuilder.String()
}