-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
organizations: | ||
- name: opensearch-project |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
// | ||
// 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.`); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(() => { | ||
|
@@ -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(), | ||
|
@@ -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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
}); | ||
|
@@ -82,6 +92,7 @@ describe('githubEventsToS3', () => { | |
repository: { | ||
name: 'repo', | ||
owner: { login: 'org' }, | ||
private: false, | ||
}, | ||
action: 'action', | ||
}, | ||
|
@@ -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({ | ||
|
@@ -110,6 +121,7 @@ describe('githubEventsToS3', () => { | |
repository: { | ||
name: 'repo', | ||
owner: { login: 'org' }, | ||
private: false, | ||
}, | ||
}, | ||
}; | ||
|
@@ -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({ | ||
|
There was a problem hiding this comment.
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: