Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove resource config validation for github-events-to-s3 call #36

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check-version-bump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
id: check
uses: EndBug/version-check@v2
with:
file-url: "https://raw.githubusercontent.com/opensearch-project/automation-app/refs/heads/${{ github.event.pull_request.base.ref }}/package.json"
file-url: 'https://raw.githubusercontent.com/opensearch-project/automation-app/refs/heads/${{ github.event.pull_request.base.ref }}/package.json'
static-checking: localIsNew

- name: Log when changed
Expand Down
3 changes: 3 additions & 0 deletions configs/resources/opensearch-project-only-org.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
organizations:
- name: opensearch-project
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opensearch-automation-app",
"version": "0.1.17",
"version": "0.1.18",
"description": "An Automation App that handles all your GitHub Repository Activities",
"author": "Peter Zhu",
"homepage": "https://github.com/opensearch-project/automation-app",
Expand Down
47 changes: 27 additions & 20 deletions src/call/github-events-to-s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,37 @@

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;

export default async function githubEventsToS3(app: Probot, context: any): Promise<void> {
// Removed validateResourceConfig to let this function listen on all repos, and filter for only the repos that are public.
// This is done so when a new repo is made public, this app can automatically start processing its events.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add one more comment along the lines of:

This is only for the s3 data lake specific case, everything else should still specify repos required to be listened in resource config.

//
// This is only for the s3 data lake specific case, everything else should still specify repos required to be listened in resource config.
//
// 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}`;
if (context.payload.repository?.private === false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if we need an else branch to log which repo is skipped due to being private.

const eventName = context.payload.action === undefined ? context.name : `${context.name}.${context.payload.action}`;

context.uploaded_at = new Date().toISOString();
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'));
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}`);
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}`);
}
} else {
app.log.error(`Event from ${repoName} skipped because it is a private repository.`);
}
}
42 changes: 27 additions & 15 deletions test/call/github-events-to-s3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jest.mock('@aws-sdk/client-s3');
describe('githubEventsToS3', () => {
let app: Probot;
let context: any;
let resource: any;
let mockS3Client: any;

beforeEach(() => {
Expand All @@ -33,19 +32,10 @@ describe('githubEventsToS3', () => {
repository: {
name: 'repo',
owner: { login: 'org' },
private: false,
},
},
};
resource = {
organizations: new Map([
[
'org',
{
repositories: new Map([['repo', 'repo object']]),
},
],
]),
};

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

await githubEventsToS3(app, context, resource);
await githubEventsToS3(app, context);

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

it('should not upload to S3 on event listened on private repo', async () => {
context = {
name: 'name',
id: 'id',
payload: {
repository: {
name: 'repo',
owner: { login: 'org' },
private: true,
},
},
};
mockS3Client.send.mockResolvedValue({});

await githubEventsToS3(app, context);

expect(mockS3Client.send).not.toHaveBeenCalledWith(expect.any(PutObjectCommand));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add an app.log.error for private repo then you can check if that is being called when repo is private.

expect(app.log.error).toHaveBeenCalledWith('Event from repo skipped because it is a private repository.');
});

it('should log an error if S3 upload fails', async () => {
mockS3Client.send.mockRejectedValue(new Error('S3 error'));

await githubEventsToS3(app, context, resource);
await githubEventsToS3(app, context);

expect(app.log.error).toHaveBeenCalledWith('Error uploading GitHub Event to S3 : Error: S3 error');
});
Expand All @@ -82,6 +92,7 @@ describe('githubEventsToS3', () => {
repository: {
name: 'repo',
owner: { login: 'org' },
private: false,
},
action: 'action',
},
Expand All @@ -92,7 +103,7 @@ describe('githubEventsToS3', () => {
jest.spyOn(Date.prototype, 'getFullYear').mockReturnValue(2024);
jest.spyOn(Date.prototype, 'toISOString').mockReturnValue('2024-10-04T21:00:06.875Z');

await githubEventsToS3(app, context, resource);
await githubEventsToS3(app, context);

expect(PutObjectCommand).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -110,6 +121,7 @@ describe('githubEventsToS3', () => {
repository: {
name: 'repo',
owner: { login: 'org' },
private: false,
},
},
};
Expand All @@ -119,7 +131,7 @@ describe('githubEventsToS3', () => {
jest.spyOn(Date.prototype, 'getFullYear').mockReturnValue(2024);
jest.spyOn(Date.prototype, 'toISOString').mockReturnValue('2024-10-04T21:00:06.875Z');

await githubEventsToS3(app, context, resource);
await githubEventsToS3(app, context);

expect(PutObjectCommand).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down
2 changes: 1 addition & 1 deletion test/utility/probot/octokit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Probot, ProbotOctokit, Logger } from 'probot';
describe('octokitFunctions', () => {
let app: Probot;
let installationId: number;
let octokitMock: ProbotOctokit
let octokitMock: ProbotOctokit;

beforeEach(() => {
app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' });
Expand Down
Loading