-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
291 lines (255 loc) · 6.75 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
package main
import (
"bufio"
"context"
"database/sql"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/sns"
_ "github.com/go-sql-driver/mysql"
"github.com/google/go-github/v33/github"
"github.com/pkg/errors"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type App struct {
DynamoDB *dynamodb.DynamoDB
CursorTable string
SQLDriver *sql.DB
SNSClient *sns.SNS
Region string
AwsAccountId string
}
type Category struct {
GithubFile string // input
SQLTable string // output
SNSTopic string // output
DynamoCursor string
IDField string
NameField string
}
const (
region = "eu-west-1"
)
var (
categories = map[string]Category{
"youtube-channels.csv": {
"youtube-channels.csv",
"yt_channels",
"arn:aws:sns:%s:%s:mirrorfm_incoming_youtube_channel",
"from_github_last_successful_channel",
"channel_id",
"channel_name",
},
"discogs-labels.csv": {
"discogs-labels.csv",
"dg_labels",
"arn:aws:sns:%s:%s:mirrorfm_incoming_discogs_label",
"from_github_last_successful_label",
"label_id",
"label_name",
},
}
)
func getApp(ctx context.Context) (App, error) {
// MySQL
dbHost := os.Getenv("DB_HOST")
dbUser := os.Getenv("DB_USERNAME")
dbPass := os.Getenv("DB_PASSWORD")
dbName := os.Getenv("DB_NAME")
sqlDriver, err := sql.Open("mysql", dbUser+":"+dbPass+"@tcp("+dbHost+")/"+dbName+"?parseTime=true")
if err != nil {
return App{}, errors.Wrap(err, "failed to set up DB client")
}
// AWS
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
dynamoClient := dynamodb.New(sess, &aws.Config{
Region: aws.String(region),
})
snsClient := sns.New(sess, &aws.Config{
Region: aws.String(region),
})
AwsAccountId, exists := os.LookupEnv("AWS_ACCOUNT_ID")
if !exists {
lc, ok := lambdacontext.FromContext(ctx)
if !ok {
return App{}, errors.Errorf("missing environment variable AWS_ACCOUNT_ID")
}
AwsAccountId = strings.Split(lc.InvokedFunctionArn, ":")[4]
}
return App{
DynamoDB: dynamoClient,
CursorTable: "mirrorfm_cursors",
SQLDriver: sqlDriver,
SNSClient: snsClient,
Region: region,
AwsAccountId: AwsAccountId,
}, nil
}
func Handler(ctx context.Context, evt github.PushEvent) error {
fmt.Printf("%+v\n", evt)
if evt.Repo == nil || evt.Repo.FullName == nil || evt.HeadCommit == nil || evt.HeadCommit.Modified == nil {
// Github webhook sends multiple events for a single push
fmt.Println("ignored incorrect event: some fields missing")
return nil
}
app, err := getApp(ctx)
if err != nil {
return errors.Wrap(err, "could not set up app")
}
for _, file := range evt.HeadCommit.Modified {
current, err := app.ProcessFile(*evt.Repo.FullName, file)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("could not process file %s", file))
}
err = app.SaveCursor(categories[file].DynamoCursor, current)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("could not save cursor %d for file %s", current, categories[file].DynamoCursor))
}
}
return nil
}
func (client *App) ProcessFile(repo, file string) (int, error) {
s := []string{
"https://raw.githubusercontent.com",
repo,
"master",
file,
}
url := strings.Join(s, "/")
resp, err := http.Get(url)
if err != nil {
return 0, errors.Wrap(err, fmt.Sprintf("failed to get %s", url))
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return 0, errors.New(fmt.Sprintf("status %d for %s", resp.StatusCode, url))
}
var lines []string
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if len(lines) == 0 {
return 0, errors.New("nothing in file")
}
cat := categories[file]
cat.SNSTopic = fmt.Sprintf(cat.SNSTopic, client.Region, client.AwsAccountId)
current, err := client.GetCursor(cat.DynamoCursor)
if err != nil {
return 0, errors.Wrap(err, "could not get cursor")
}
current, err = client.processLines(lines, current, cat)
if err != nil {
return 0, errors.Wrap(err, "failed to process lines")
}
return current, nil
}
func (client *App) processLines(lines []string, current int, cat Category) (int, error) {
total := len(lines) - 1
for current < total {
current += 1
currentLine := lines[current]
parts := strings.Split(currentLine, ",")
id := parts[0]
name := parts[1]
if id == "" {
fmt.Printf("line %s is empty", id)
break
}
err := client.InsertIntoTable(id, name, cat)
if err != nil {
fmt.Printf("skip duplicate #%d: %s\n", current, err.Error())
continue
}
_, err = client.SNSClient.Publish(&sns.PublishInput{
TopicArn: aws.String(cat.SNSTopic),
Message: aws.String(id),
})
if err != nil {
return current, errors.Wrap(err, fmt.Sprintf("failed to publish %s on %s\n", id, cat.SNSTopic))
}
fmt.Printf("published %s on %s\n", id, cat.SNSTopic)
}
return current, nil
}
func (client *App) InsertIntoTable(id, name string, cat Category) error {
_, err := client.SQLDriver.Exec(fmt.Sprintf(`
INSERT INTO %s (%s, %s, added_datetime)
VALUES (?, ?, ?)
`, cat.SQLTable, cat.IDField, cat.NameField), id, strings.TrimSpace(name), time.Now())
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to insert into %s", cat.SQLTable))
}
return nil
}
func (client *App) GetCursor(cursor string) (int, error) {
resp, err := client.DynamoDB.GetItem(&dynamodb.GetItemInput{
TableName: &client.CursorTable,
Key: map[string]*dynamodb.AttributeValue{
"name": {
S: aws.String(cursor),
},
},
AttributesToGet: []*string{
aws.String("value"),
},
})
if err != nil {
return 0, err
}
val, ok := resp.Item["value"]
if !ok {
return 0, nil
}
return strconv.Atoi(*val.N)
}
func (client *App) SaveCursor(cursor string, value int) error {
if _, err := client.DynamoDB.PutItem(&dynamodb.PutItemInput{
TableName: &client.CursorTable,
Item: map[string]*dynamodb.AttributeValue{
"name": {
S: aws.String(cursor),
},
"value": {
N: aws.String(strconv.Itoa(value)),
},
},
}); err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to save %s cursor", cursor))
}
fmt.Printf("successfully set cursor to %d\n", value)
return nil
}
func main() {
if os.Getenv("AWS_LAMBDA_FUNCTION_NAME") != "" {
lambda.Start(Handler)
} else {
// Local run
name := "mirrorfm/data"
err := Handler(context.TODO(),
github.PushEvent{
Repo: &github.PushEventRepository{
FullName: &name,
},
HeadCommit: &github.HeadCommit{
Modified: []string{
"youtube-channels.csv",
"discogs-labels.csv",
},
},
})
if err != nil {
fmt.Println(err.Error())
}
}
}