-
Notifications
You must be signed in to change notification settings - Fork 4
/
github-events-to-s3.ts
41 lines (34 loc) · 1.61 KB
/
github-events-to-s3.ts
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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
// Name : githubEventsToS3
// Description : Stores GitHub Events in an S3 Bucket
import { Probot } from 'probot';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { Resource } from '../service/resource/resource';
import { validateResourceConfig } from '../utility/verification/verify-resource';
export default async function githubEventsToS3(app: Probot, context: any, resource: Resource): Promise<void> {
if (!(await validateResourceConfig(app, context, resource))) return;
const repoName = context.payload.repository?.name;
const eventName = context.payload.action === undefined ? context.name : `${context.name}.${context.payload.action}`;
context.uploaded_at = new Date().toISOString();
const now = new Date();
const [day, month, year] = [now.getDate(), now.getMonth() + 1, now.getFullYear()].map((num) => String(num).padStart(2, '0'));
try {
const s3Client = new S3Client({ region: String(process.env.REGION) });
const putObjectCommand = new PutObjectCommand({
Bucket: String(process.env.OPENSEARCH_EVENTS_BUCKET),
Body: JSON.stringify(context),
Key: `${eventName}/${year}-${month}-${day}/${repoName}-${context.id}`,
});
await s3Client.send(putObjectCommand);
app.log.info('GitHub Event uploaded to S3 successfully.');
} catch (error) {
app.log.error(`Error uploading GitHub Event to S3 : ${error}`);
}
}