Skip to content

Commit b3ea737

Browse files
Added undefined check to event action (#27)
* Added undefined check to event action Signed-off-by: Brandon Shien <bshien@amazon.com> * Added tests for the event action undef check, added clear all mocks to tests Signed-off-by: Brandon Shien <bshien@amazon.com> * Update version check Signed-off-by: Peter Zhu <zhujiaxi@amazon.com> * bump package version Signed-off-by: Peter Zhu <zhujiaxi@amazon.com> * bump package version Signed-off-by: Peter Zhu <zhujiaxi@amazon.com> --------- Signed-off-by: Brandon Shien <bshien@amazon.com> Signed-off-by: Peter Zhu <zhujiaxi@amazon.com> Co-authored-by: Peter Zhu <zhujiaxi@amazon.com>
1 parent 7286e99 commit b3ea737

10 files changed

+83
-6
lines changed

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

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

1819
- name: Log when changed
1920
if: steps.check.outputs.changed == 'true'

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.11",
3+
"version": "0.1.12",
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default async function githubEventsToS3(app: Probot, context: any, resour
1919
if (!(await validateResourceConfig(app, context, resource))) return;
2020

2121
const repoName = context.payload.repository?.name;
22+
const eventName = context.payload.action === undefined ? context.name : `${context.name}.${context.payload.action}`;
2223

2324
const now = new Date();
2425
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
2829
const putObjectCommand = new PutObjectCommand({
2930
Bucket: String(process.env.OPENSEARCH_EVENTS_BUCKET),
3031
Body: JSON.stringify(context),
31-
Key: `${context.name}.${context.payload.action}/${year}-${month}-${day}/${repoName}-${context.id}`,
32+
Key: `${eventName}/${year}-${month}-${day}/${repoName}-${context.id}`,
3233
});
3334
await s3Client.send(putObjectCommand);
3435
app.log.info('GitHub Event uploaded to S3 successfully.');

test/call/create-issue-comment.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ describe('createIssueCommentFunctions', () => {
7070
};
7171
});
7272

73+
afterEach(() => {
74+
jest.clearAllMocks();
75+
});
76+
7377
describe('createIssueComment', () => {
7478
it('should write comments based on user defined text', async () => {
7579
await createIssueComment(app, context, resource, args);

test/call/github-activity-events-monitor.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ describe('githubActivityEventsMonitor', () => {
5050
};
5151
});
5252

53+
afterEach(() => {
54+
jest.clearAllMocks();
55+
});
56+
5357
it('should index events', async () => {
5458
const mockClient = {
5559
index: jest.fn().mockResolvedValue({}),

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

+56-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import githubEventsToS3 from '../../src/call/github-events-to-s3';
1313

1414
jest.mock('@aws-sdk/client-s3');
1515

16-
describe('githubWorkflowRunsMonitor', () => {
16+
describe('githubEventsToS3', () => {
1717
let app: Probot;
1818
let context: any;
1919
let resource: any;
@@ -53,6 +53,10 @@ describe('githubWorkflowRunsMonitor', () => {
5353
(S3Client as jest.Mock).mockImplementation(() => mockS3Client);
5454
});
5555

56+
afterEach(() => {
57+
jest.clearAllMocks();
58+
});
59+
5660
it('should upload to S3 on event listened', async () => {
5761
mockS3Client.send.mockResolvedValue({});
5862

@@ -69,4 +73,55 @@ describe('githubWorkflowRunsMonitor', () => {
6973

7074
expect(app.log.error).toHaveBeenCalledWith('Error uploading GitHub Event to S3 : Error: S3 error');
7175
});
76+
77+
it('S3 key name set with action', async () => {
78+
context = {
79+
name: 'name',
80+
id: 'id',
81+
payload: {
82+
repository: {
83+
name: 'repo',
84+
owner: { login: 'org' },
85+
},
86+
action: 'action',
87+
},
88+
};
89+
90+
jest.spyOn(Date.prototype, 'getDate').mockReturnValue(4);
91+
jest.spyOn(Date.prototype, 'getMonth').mockReturnValue(8);
92+
jest.spyOn(Date.prototype, 'getFullYear').mockReturnValue(2024);
93+
94+
await githubEventsToS3(app, context, resource);
95+
96+
expect(PutObjectCommand).toHaveBeenCalledWith(
97+
expect.objectContaining({
98+
Key: expect.stringMatching(`name.action/2024-09-04/repo-id`),
99+
}),
100+
);
101+
});
102+
103+
it('S3 key name set without action', async () => {
104+
context = {
105+
name: 'name',
106+
id: 'id',
107+
payload: {
108+
repository: {
109+
name: 'repo',
110+
owner: { login: 'org' },
111+
},
112+
},
113+
};
114+
115+
jest.spyOn(Date.prototype, 'getDate').mockReturnValue(4);
116+
jest.spyOn(Date.prototype, 'getMonth').mockReturnValue(8);
117+
jest.spyOn(Date.prototype, 'getFullYear').mockReturnValue(2024);
118+
119+
await githubEventsToS3(app, context, resource);
120+
121+
expect(PutObjectCommand).toHaveBeenCalledWith(
122+
expect.objectContaining({
123+
Key: expect.stringMatching(`name/2024-09-04/repo-id`),
124+
}),
125+
);
126+
});
72127
});

test/call/github-merged-pulls-monitor.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ describe('githubMergedPullsMonitor', () => {
8282
};
8383
});
8484

85+
afterEach(() => {
86+
jest.clearAllMocks();
87+
});
88+
8589
it('should skip processing if the pull request is not merged', async () => {
8690
context.payload.pull_request.merged = false;
8791
await githubMergedPullsMonitor(app, context, resource);

test/call/github-workflow-runs-monitor.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ describe('githubWorkflowRunsMonitor', () => {
7878
(CloudWatchClient as jest.Mock).mockImplementation(() => mockCloudWatchClient);
7979
});
8080

81+
afterEach(() => {
82+
jest.clearAllMocks();
83+
});
84+
8185
it('should skip indexing when the event is not relevant', async () => {
8286
const events = ['pull_request', 'release'];
8387
const workflows = ['Publish snapshots to maven'];

test/call/print-to-console.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ describe('printToConsoleFunctions', () => {
5252
};
5353
});
5454

55+
afterEach(() => {
56+
jest.clearAllMocks();
57+
});
58+
5559
describe('printToConsole', () => {
5660
it('should print defined text in task', async () => {
5761
await printToConsole(app, context, resource, args);

0 commit comments

Comments
 (0)