Skip to content

Commit 4a90494

Browse files
authored
Remove resource config validation for github-events-to-s3 call (#36)
* Remove resource config validation for github-events-to-s3 call Signed-off-by: Brandon Shien <bshien@amazon.com> * Added private repo error log Signed-off-by: Brandon Shien <bshien@amazon.com> --------- Signed-off-by: Brandon Shien <bshien@amazon.com>
1 parent b3e35e6 commit 4a90494

File tree

7 files changed

+62
-40
lines changed

7 files changed

+62
-40
lines changed

.github/workflows/check-version-bump.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
id: check
1414
uses: EndBug/version-check@v2
1515
with:
16-
file-url: "https://raw.githubusercontent.com/opensearch-project/automation-app/refs/heads/${{ github.event.pull_request.base.ref }}/package.json"
16+
file-url: 'https://raw.githubusercontent.com/opensearch-project/automation-app/refs/heads/${{ github.event.pull_request.base.ref }}/package.json'
1717
static-checking: localIsNew
1818

1919
- name: Log when changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
organizations:
3+
- name: opensearch-project

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opensearch-automation-app",
3-
"version": "0.1.17",
3+
"version": "0.1.18",
44
"description": "An Automation App that handles all your GitHub Repository Activities",
55
"author": "Peter Zhu",
66
"homepage": "https://github.com/opensearch-project/automation-app",

src/call/github-events-to-s3.ts

+27-20
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,37 @@
1212

1313
import { Probot } from 'probot';
1414
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
15-
import { Resource } from '../service/resource/resource';
16-
import { validateResourceConfig } from '../utility/verification/verify-resource';
17-
18-
export default async function githubEventsToS3(app: Probot, context: any, resource: Resource): Promise<void> {
19-
if (!(await validateResourceConfig(app, context, resource))) return;
2015

16+
export default async function githubEventsToS3(app: Probot, context: any): Promise<void> {
17+
// Removed validateResourceConfig to let this function listen on all repos, and filter for only the repos that are public.
18+
// This is done so when a new repo is made public, this app can automatically start processing its events.
19+
//
20+
// This is only for the s3 data lake specific case, everything else should still specify repos required to be listened in resource config.
21+
//
22+
// if (!(await validateResourceConfig(app, context, resource))) return;
23+
//
2124
const repoName = context.payload.repository?.name;
22-
const eventName = context.payload.action === undefined ? context.name : `${context.name}.${context.payload.action}`;
25+
if (context.payload.repository?.private === false) {
26+
const eventName = context.payload.action === undefined ? context.name : `${context.name}.${context.payload.action}`;
2327

24-
context.uploaded_at = new Date().toISOString();
28+
context.uploaded_at = new Date().toISOString();
2529

26-
const now = new Date();
27-
const [day, month, year] = [now.getDate(), now.getMonth() + 1, now.getFullYear()].map((num) => String(num).padStart(2, '0'));
30+
const now = new Date();
31+
const [day, month, year] = [now.getDate(), now.getMonth() + 1, now.getFullYear()].map((num) => String(num).padStart(2, '0'));
2832

29-
try {
30-
const s3Client = new S3Client({ region: String(process.env.REGION) });
31-
const putObjectCommand = new PutObjectCommand({
32-
Bucket: String(process.env.OPENSEARCH_EVENTS_BUCKET),
33-
Body: JSON.stringify(context),
34-
Key: `${eventName}/${year}-${month}-${day}/${repoName}-${context.id}`,
35-
});
36-
await s3Client.send(putObjectCommand);
37-
app.log.info('GitHub Event uploaded to S3 successfully.');
38-
} catch (error) {
39-
app.log.error(`Error uploading GitHub Event to S3 : ${error}`);
33+
try {
34+
const s3Client = new S3Client({ region: String(process.env.REGION) });
35+
const putObjectCommand = new PutObjectCommand({
36+
Bucket: String(process.env.OPENSEARCH_EVENTS_BUCKET),
37+
Body: JSON.stringify(context),
38+
Key: `${eventName}/${year}-${month}-${day}/${repoName}-${context.id}`,
39+
});
40+
await s3Client.send(putObjectCommand);
41+
app.log.info('GitHub Event uploaded to S3 successfully.');
42+
} catch (error) {
43+
app.log.error(`Error uploading GitHub Event to S3 : ${error}`);
44+
}
45+
} else {
46+
app.log.error(`Event from ${repoName} skipped because it is a private repository.`);
4047
}
4148
}

test/call/github-events-to-s3.test.ts

+27-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ jest.mock('@aws-sdk/client-s3');
1616
describe('githubEventsToS3', () => {
1717
let app: Probot;
1818
let context: any;
19-
let resource: any;
2019
let mockS3Client: any;
2120

2221
beforeEach(() => {
@@ -33,19 +32,10 @@ describe('githubEventsToS3', () => {
3332
repository: {
3433
name: 'repo',
3534
owner: { login: 'org' },
35+
private: false,
3636
},
3737
},
3838
};
39-
resource = {
40-
organizations: new Map([
41-
[
42-
'org',
43-
{
44-
repositories: new Map([['repo', 'repo object']]),
45-
},
46-
],
47-
]),
48-
};
4939

5040
mockS3Client = {
5141
send: jest.fn(),
@@ -60,16 +50,36 @@ describe('githubEventsToS3', () => {
6050
it('should upload to S3 on event listened', async () => {
6151
mockS3Client.send.mockResolvedValue({});
6252

63-
await githubEventsToS3(app, context, resource);
53+
await githubEventsToS3(app, context);
6454

6555
expect(mockS3Client.send).toHaveBeenCalledWith(expect.any(PutObjectCommand));
6656
expect(app.log.info).toHaveBeenCalledWith('GitHub Event uploaded to S3 successfully.');
6757
});
6858

59+
it('should not upload to S3 on event listened on private repo', async () => {
60+
context = {
61+
name: 'name',
62+
id: 'id',
63+
payload: {
64+
repository: {
65+
name: 'repo',
66+
owner: { login: 'org' },
67+
private: true,
68+
},
69+
},
70+
};
71+
mockS3Client.send.mockResolvedValue({});
72+
73+
await githubEventsToS3(app, context);
74+
75+
expect(mockS3Client.send).not.toHaveBeenCalledWith(expect.any(PutObjectCommand));
76+
expect(app.log.error).toHaveBeenCalledWith('Event from repo skipped because it is a private repository.');
77+
});
78+
6979
it('should log an error if S3 upload fails', async () => {
7080
mockS3Client.send.mockRejectedValue(new Error('S3 error'));
7181

72-
await githubEventsToS3(app, context, resource);
82+
await githubEventsToS3(app, context);
7383

7484
expect(app.log.error).toHaveBeenCalledWith('Error uploading GitHub Event to S3 : Error: S3 error');
7585
});
@@ -82,6 +92,7 @@ describe('githubEventsToS3', () => {
8292
repository: {
8393
name: 'repo',
8494
owner: { login: 'org' },
95+
private: false,
8596
},
8697
action: 'action',
8798
},
@@ -92,7 +103,7 @@ describe('githubEventsToS3', () => {
92103
jest.spyOn(Date.prototype, 'getFullYear').mockReturnValue(2024);
93104
jest.spyOn(Date.prototype, 'toISOString').mockReturnValue('2024-10-04T21:00:06.875Z');
94105

95-
await githubEventsToS3(app, context, resource);
106+
await githubEventsToS3(app, context);
96107

97108
expect(PutObjectCommand).toHaveBeenCalledWith(
98109
expect.objectContaining({
@@ -110,6 +121,7 @@ describe('githubEventsToS3', () => {
110121
repository: {
111122
name: 'repo',
112123
owner: { login: 'org' },
124+
private: false,
113125
},
114126
},
115127
};
@@ -119,7 +131,7 @@ describe('githubEventsToS3', () => {
119131
jest.spyOn(Date.prototype, 'getFullYear').mockReturnValue(2024);
120132
jest.spyOn(Date.prototype, 'toISOString').mockReturnValue('2024-10-04T21:00:06.875Z');
121133

122-
await githubEventsToS3(app, context, resource);
134+
await githubEventsToS3(app, context);
123135

124136
expect(PutObjectCommand).toHaveBeenCalledWith(
125137
expect.objectContaining({

test/utility/probot/octokit.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Probot, ProbotOctokit, Logger } from 'probot';
1313
describe('octokitFunctions', () => {
1414
let app: Probot;
1515
let installationId: number;
16-
let octokitMock: ProbotOctokit
16+
let octokitMock: ProbotOctokit;
1717

1818
beforeEach(() => {
1919
app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' });

0 commit comments

Comments
 (0)