Skip to content

Commit 9f449dc

Browse files
authored
Merge pull request #1094 from 20minutes/feature/artifacts-function
Add artifacts function
2 parents e0a822e + 664b8d2 commit 9f449dc

File tree

6 files changed

+514
-2
lines changed

6 files changed

+514
-2
lines changed

functions/artifacts.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ArtifactsHandler } from './classes/ArtifactsHandler'
2+
3+
const artifacts = new ArtifactsHandler(
4+
process.env.GITHUB_TOKEN,
5+
process.env.NAMESPACE,
6+
process.env.ARTIFACTS_REGEX
7+
)
8+
9+
export async function handler(event, context, callback) {
10+
if (event.headers?.['content-type'] === 'application/x-www-form-urlencoded') {
11+
return callback(null, {
12+
statusCode: 500,
13+
body: 'Please choose "application/json" as Content type in the webhook definition (you should re-create it)',
14+
})
15+
}
16+
17+
return artifacts.handle(JSON.parse(event.body), callback)
18+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { Handler } from './Handler'
2+
3+
export class ArtifactsHandler extends Handler {
4+
constructor(githubToken, namespace = '', artifactsRegex = '') {
5+
super(githubToken)
6+
7+
this.namespace = namespace
8+
this.artifactsRegex = artifactsRegex
9+
}
10+
11+
async handle(body, callback) {
12+
let response = this.validateEvent(body)
13+
14+
if (response !== true) {
15+
return callback(null, response)
16+
}
17+
18+
console.log(`Working on repo ${body.repository.full_name} for PR #${body.pull_request.number}`)
19+
20+
const payload = {
21+
success: {
22+
state: 'success',
23+
description: 'No artifacts found in deps',
24+
context: `${this.namespace} - Artifacts check`,
25+
},
26+
failure: {
27+
state: 'failure',
28+
description: 'Artifacts found in deps, remove them to merge',
29+
context: `${this.namespace} - Artifacts check`,
30+
},
31+
}
32+
33+
const files = await this.githubClient.rest.pulls.listFiles({
34+
owner: body.repository.owner.login,
35+
repo: body.repository.name,
36+
pull_number: body.pull_request.number,
37+
})
38+
39+
// loop through all files to see if package.json has been updated
40+
const file = files.data.find(({ filename }) => filename.includes('package.json'))
41+
42+
if (!file) {
43+
response = await this.updateStatus(body, payload.success)
44+
45+
return callback(null, response)
46+
}
47+
48+
const refMatch = file.contents_url.match(/ref=([a-z0-9]+)/)
49+
if (!refMatch?.[1]) {
50+
response = await this.updateStatus(body, payload.success)
51+
52+
return callback(null, response)
53+
}
54+
55+
const content = await this.githubClient.rest.repos.getContent({
56+
owner: body.repository.owner.login,
57+
repo: body.repository.name,
58+
path: file.filename,
59+
ref: refMatch[1],
60+
})
61+
62+
const match = Buffer.from(content.data.content.toString('utf8'), 'base64')
63+
.toString('ascii')
64+
.match(this.artifactsRegex)
65+
66+
if (match === null) {
67+
response = await this.updateStatus(body, payload.success)
68+
} else {
69+
response = await this.updateStatus(body, payload.failure)
70+
}
71+
72+
return callback(null, response)
73+
}
74+
}

functions/classes/FixupHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class FixupHandler extends Handler {
2424
},
2525
failure: {
2626
state: 'failure',
27-
description: 'Fixup commits in history, please squash them!',
27+
description: 'Fixup commits in history, squash them to merge',
2828
context: `${this.namespace} - Fixup check`,
2929
},
3030
}

serverless.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ functions:
8383
path: webhook/auto-merge
8484
method: post
8585
cors: true
86+
87+
artifacts:
88+
handler: functions/artifacts.handler
89+
description: Check if an artifact in deps to block merge
90+
environment:
91+
ARTIFACTS_REGEX: '(fixtures\.20mn\.it|localhost:)'
92+
events:
93+
-
94+
http:
95+
path: webhook/artifacts
96+
method: post
97+
cors: true

0 commit comments

Comments
 (0)