Skip to content

Commit

Permalink
Added undefined check to event action (#27)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
bshien and peterzhuamazon authored Oct 10, 2024
1 parent 7286e99 commit b3ea737
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/check-version-bump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/${{ github.event.pull_request.base.ref }}/package.json"
static-checking: localIsNew

- name: Log when changed
if: steps.check.outputs.changed == 'true'
Expand Down
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.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",
Expand Down
3 changes: 2 additions & 1 deletion src/call/github-events-to-s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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.');
Expand Down
4 changes: 4 additions & 0 deletions test/call/create-issue-comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions test/call/github-activity-events-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ describe('githubActivityEventsMonitor', () => {
};
});

afterEach(() => {
jest.clearAllMocks();
});

it('should index events', async () => {
const mockClient = {
index: jest.fn().mockResolvedValue({}),
Expand Down
57 changes: 56 additions & 1 deletion test/call/github-events-to-s3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({});

Expand All @@ -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`),
}),
);
});
});
4 changes: 4 additions & 0 deletions test/call/github-merged-pulls-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions test/call/github-workflow-runs-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
4 changes: 4 additions & 0 deletions test/call/print-to-console.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b3ea737

Please sign in to comment.