-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
103 lines (87 loc) · 2.7 KB
/
helpers.js
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
const AWS = require('aws-sdk')
const config = require('./config')
const ddb = new AWS.DynamoDB({
apiVersion: '2012-08-10'
})
const sqs = new AWS.SQS({
apiVersion: '2012-11-05'
})
function reformat (ddbResultItem) {
const item = {}
for (let key in ddbResultItem) {
const val = ddbResultItem[key]
if ('S' in val) {
item[key] = val['S']
} else if ('N' in val) {
item[key] = +val['N']
}
}
return item
}
async function createModuleVersion (username, reponame, fullname, userId,
moduleDescription, ref, versionDescription, event, archiveUrl) {
const moduleItem = {
username: { S: username },
ID: { S: reponame },
fullname: { S: fullname },
createdBy: { S: userId },
createdAt: { N: Date.now().toString() }
}
if (moduleDescription) {
moduleItem.description = { S: moduleDescription }
}
// Put ddb module entry if it doesn't already exist
let moduleResult
try {
moduleResult = await ddb.putItem({
TableName: 'modules',
Item: moduleItem,
ConditionExpression: 'username <> :username AND ID <> :reponame',
ExpressionAttributeValues: {
':username': { 'S': username },
':reponame': { 'S': reponame }
}
}).promise()
} catch (err) {
if (err.code !== 'ConditionalCheckFailedException') {
throw err
}
}
const versionItem = {
ID: { S: ref },
moduleId: { S: fullname },
moduleName: { S: reponame },
status: { S: 'pending' },
version: { S: ref },
createdBy: { S: userId },
createdAt: { N: Date.now().toString() }
}
if (versionDescription) {
versionItem.description = { S: versionDescription }
}
// if (changesetUrl) item.changesetUrl = { S: changesetUrl }
// Save new version item - will throw if the module/version exists already
const versionResult = await ddb.putItem({
TableName: 'versions',
Item: versionItem,
ConditionExpression: 'moduleId <> :moduleId AND ID <> :version',
ExpressionAttributeValues: {
':moduleId': { 'S': fullname },
':version': { 'S': ref }
}
}).promise()
// Pop message onto the queue to download & sync the repo
const queueResult = await sqs.sendMessage({
MessageBody: event.body,
QueueUrl: config.queueUrl,
MessageAttributes: {
'type': { DataType: 'String', StringValue: 'webhook' },
'archive_url': { DataType: 'String', StringValue: archiveUrl },
'module_id': { DataType: 'String', StringValue: fullname },
'version': { DataType: 'String', StringValue: ref },
'destination_path': { DataType: 'String', StringValue: `${fullname}/${ref}` }
}
}).promise()
return { moduleResult, versionResult, queueResult }
}
module.exports = { reformat, createModuleVersion }