From a9c90c8250cac7d45c67511073331e231dd34323 Mon Sep 17 00:00:00 2001 From: Brandon Shien Date: Wed, 9 Oct 2024 13:48:48 -0700 Subject: [PATCH 1/5] Added undefined check to event action Signed-off-by: Brandon Shien --- package-lock.json | 4 ++-- package.json | 2 +- src/call/github-events-to-s3.ts | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1052259..45415e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "opensearch-automation-app", - "version": "0.1.10", + "version": "0.1.11", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opensearch-automation-app", - "version": "0.1.10", + "version": "0.1.11", "dependencies": { "@aws-sdk/client-cloudwatch": "^3.664.0", "@aws-sdk/client-opensearch": "^3.658.1", diff --git a/package.json b/package.json index 917b56a..4b1341d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opensearch-automation-app", - "version": "0.1.10", + "version": "0.1.11", "description": "An Automation App that handles all your GitHub Repository Activities", "author": "Peter Zhu", "homepage": "https://github.com/opensearch-project/automation-app", diff --git a/src/call/github-events-to-s3.ts b/src/call/github-events-to-s3.ts index 487ebcd..6749364 100644 --- a/src/call/github-events-to-s3.ts +++ b/src/call/github-events-to-s3.ts @@ -19,6 +19,7 @@ export default async function githubEventsToS3(app: Probot, context: any, resour 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}`; const now = new Date(); const [day, month, year] = [now.getDate(), now.getMonth() + 1, now.getFullYear()].map((num) => String(num).padStart(2, '0')); @@ -28,7 +29,7 @@ export default async function githubEventsToS3(app: Probot, context: any, resour const putObjectCommand = new PutObjectCommand({ Bucket: String(process.env.OPENSEARCH_EVENTS_BUCKET), Body: JSON.stringify(context), - Key: `${context.name}.${context.payload.action}/${year}-${month}-${day}/${repoName}-${context.id}`, + Key: `${eventName}/${year}-${month}-${day}/${repoName}-${context.id}`, }); await s3Client.send(putObjectCommand); app.log.info('GitHub Event uploaded to S3 successfully.'); From d65102d870b56cb330639b9a75fbd250f8bcbe10 Mon Sep 17 00:00:00 2001 From: Brandon Shien Date: Wed, 9 Oct 2024 17:15:19 -0700 Subject: [PATCH 2/5] Added tests for the event action undef check, added clear all mocks to tests Signed-off-by: Brandon Shien --- test/call/create-issue-comment.test.ts | 4 ++ .../github-activity-events-monitor.test.ts | 4 ++ test/call/github-events-to-s3.test.ts | 57 ++++++++++++++++++- test/call/github-merged-pulls-monitor.test.ts | 4 ++ .../call/github-workflow-runs-monitor.test.ts | 4 ++ test/call/print-to-console.test.ts | 4 ++ 6 files changed, 76 insertions(+), 1 deletion(-) diff --git a/test/call/create-issue-comment.test.ts b/test/call/create-issue-comment.test.ts index 13fbcac..ca08d25 100644 --- a/test/call/create-issue-comment.test.ts +++ b/test/call/create-issue-comment.test.ts @@ -70,6 +70,10 @@ describe('createIssueCommentFunctions', () => { }; }); + afterEach(() => { + jest.clearAllMocks(); + }); + describe('createIssueComment', () => { it('should write comments based on user defined text', async () => { await createIssueComment(app, context, resource, args); diff --git a/test/call/github-activity-events-monitor.test.ts b/test/call/github-activity-events-monitor.test.ts index 863e518..2b544d1 100644 --- a/test/call/github-activity-events-monitor.test.ts +++ b/test/call/github-activity-events-monitor.test.ts @@ -50,6 +50,10 @@ describe('githubActivityEventsMonitor', () => { }; }); + afterEach(() => { + jest.clearAllMocks(); + }); + it('should index events', async () => { const mockClient = { index: jest.fn().mockResolvedValue({}), diff --git a/test/call/github-events-to-s3.test.ts b/test/call/github-events-to-s3.test.ts index 43f3557..4c5259b 100644 --- a/test/call/github-events-to-s3.test.ts +++ b/test/call/github-events-to-s3.test.ts @@ -13,7 +13,7 @@ import githubEventsToS3 from '../../src/call/github-events-to-s3'; jest.mock('@aws-sdk/client-s3'); -describe('githubWorkflowRunsMonitor', () => { +describe('githubEventsToS3', () => { let app: Probot; let context: any; let resource: any; @@ -53,6 +53,10 @@ describe('githubWorkflowRunsMonitor', () => { (S3Client as jest.Mock).mockImplementation(() => mockS3Client); }); + afterEach(() => { + jest.clearAllMocks(); + }); + it('should upload to S3 on event listened', async () => { mockS3Client.send.mockResolvedValue({}); @@ -69,4 +73,55 @@ describe('githubWorkflowRunsMonitor', () => { expect(app.log.error).toHaveBeenCalledWith('Error uploading GitHub Event to S3 : Error: S3 error'); }); + + it('S3 key name set with action', async () => { + context = { + name: 'name', + id: 'id', + payload: { + repository: { + name: 'repo', + owner: { login: 'org' }, + }, + action: 'action', + }, + }; + + jest.spyOn(Date.prototype, 'getDate').mockReturnValue(4); + jest.spyOn(Date.prototype, 'getMonth').mockReturnValue(8); + jest.spyOn(Date.prototype, 'getFullYear').mockReturnValue(2024); + + await githubEventsToS3(app, context, resource); + + expect(PutObjectCommand).toHaveBeenCalledWith( + expect.objectContaining({ + Key: expect.stringMatching(`name.action/2024-09-04/repo-id`), + }), + ); + }); + + it('S3 key name set without action', async () => { + context = { + name: 'name', + id: 'id', + payload: { + repository: { + name: 'repo', + owner: { login: 'org' }, + }, + }, + }; + + jest.spyOn(Date.prototype, 'getDate').mockReturnValue(4); + jest.spyOn(Date.prototype, 'getMonth').mockReturnValue(8); + jest.spyOn(Date.prototype, 'getFullYear').mockReturnValue(2024); + + await githubEventsToS3(app, context, resource); + + expect(PutObjectCommand).toHaveBeenCalledWith( + expect.objectContaining({ + Key: expect.stringMatching(`name/2024-09-04/repo-id`), + }), + ); + }); }); diff --git a/test/call/github-merged-pulls-monitor.test.ts b/test/call/github-merged-pulls-monitor.test.ts index 6260c0e..f40e5b9 100644 --- a/test/call/github-merged-pulls-monitor.test.ts +++ b/test/call/github-merged-pulls-monitor.test.ts @@ -82,6 +82,10 @@ describe('githubMergedPullsMonitor', () => { }; }); + afterEach(() => { + jest.clearAllMocks(); + }); + it('should skip processing if the pull request is not merged', async () => { context.payload.pull_request.merged = false; await githubMergedPullsMonitor(app, context, resource); diff --git a/test/call/github-workflow-runs-monitor.test.ts b/test/call/github-workflow-runs-monitor.test.ts index d0494cf..24c3c5a 100644 --- a/test/call/github-workflow-runs-monitor.test.ts +++ b/test/call/github-workflow-runs-monitor.test.ts @@ -78,6 +78,10 @@ describe('githubWorkflowRunsMonitor', () => { (CloudWatchClient as jest.Mock).mockImplementation(() => mockCloudWatchClient); }); + afterEach(() => { + jest.clearAllMocks(); + }); + it('should skip indexing when the event is not relevant', async () => { const events = ['pull_request', 'release']; const workflows = ['Publish snapshots to maven']; diff --git a/test/call/print-to-console.test.ts b/test/call/print-to-console.test.ts index fcfe4bf..7126be4 100644 --- a/test/call/print-to-console.test.ts +++ b/test/call/print-to-console.test.ts @@ -52,6 +52,10 @@ describe('printToConsoleFunctions', () => { }; }); + afterEach(() => { + jest.clearAllMocks(); + }); + describe('printToConsole', () => { it('should print defined text in task', async () => { await printToConsole(app, context, resource, args); From 520255e49a77f049ed614128822474f7f05e1d28 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Thu, 10 Oct 2024 14:20:31 -0400 Subject: [PATCH 3/5] Update version check Signed-off-by: Peter Zhu --- .github/workflows/check-version-bump.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check-version-bump.yml b/.github/workflows/check-version-bump.yml index 6180f58..0db54d2 100644 --- a/.github/workflows/check-version-bump.yml +++ b/.github/workflows/check-version-bump.yml @@ -13,7 +13,8 @@ jobs: id: check uses: EndBug/version-check@v2 with: - diff-search: true + file-url: https://raw.githubusercontent.com/opensearch-project/automation-app/refs/heads/main/package.json + static-checking: localIsNew - name: Log when changed if: steps.check.outputs.changed == 'true' From ced963840352fccdc769e11c77d39461c76d3d42 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Thu, 10 Oct 2024 14:21:42 -0400 Subject: [PATCH 4/5] bump package version Signed-off-by: Peter Zhu --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 45415e0..b7e074d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "opensearch-automation-app", - "version": "0.1.11", + "version": "0.1.12", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opensearch-automation-app", - "version": "0.1.11", + "version": "0.1.12", "dependencies": { "@aws-sdk/client-cloudwatch": "^3.664.0", "@aws-sdk/client-opensearch": "^3.658.1", diff --git a/package.json b/package.json index 4b1341d..2bc1443 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opensearch-automation-app", - "version": "0.1.11", + "version": "0.1.12", "description": "An Automation App that handles all your GitHub Repository Activities", "author": "Peter Zhu", "homepage": "https://github.com/opensearch-project/automation-app", From 70f474d120a8cfb8c6507edef6b24af65ad22e6e Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Thu, 10 Oct 2024 14:24:54 -0400 Subject: [PATCH 5/5] bump package version Signed-off-by: Peter Zhu --- .github/workflows/check-version-bump.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check-version-bump.yml b/.github/workflows/check-version-bump.yml index 0db54d2..ff84f11 100644 --- a/.github/workflows/check-version-bump.yml +++ b/.github/workflows/check-version-bump.yml @@ -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/main/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