Skip to content

Commit 0f5ca46

Browse files
authored
Added indexing user activity events operation (#19)
* Added indexing user activity events operation Signed-off-by: Brandon Shien <bshien@amazon.com> * Added tests and prettier formatting Signed-off-by: Brandon Shien <bshien@amazon.com> --------- Signed-off-by: Brandon Shien <bshien@amazon.com>
1 parent 92f58cb commit 0f5ca46

File tree

4 files changed

+161
-1
lines changed

4 files changed

+161
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
name: Activity Events Monitor
3+
4+
events:
5+
- issues.opened
6+
- issues.closed
7+
- issues.labeled
8+
- issues.unlabeled
9+
- issues.transferred
10+
- issues.assigned
11+
- issue_comment.created
12+
- pull_request.closed
13+
- pull_request.opened
14+
- pull_request.labeled
15+
- pull_request.unlabeled
16+
- pull_request.assigned
17+
- pull_request_review.submitted
18+
- pull_request_review_comment.created
19+
- gollum
20+
# - push
21+
# - release.released
22+
# - project.edited
23+
24+
tasks:
25+
- name: Activity Events Monitor Operation
26+
call: github-activity-events-monitor@default

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.7",
3+
"version": "0.1.8",
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",
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* The OpenSearch Contributors require contributions made to
6+
* this file be licensed under the Apache-2.0 license or a
7+
* compatible open source license.
8+
*/
9+
10+
// Name : githubActivityEventsMonitor
11+
// Description : Indexes events pertaining to user activity to OpenSearch
12+
13+
import { Probot } from 'probot';
14+
import { Resource } from '../service/resource/resource';
15+
import { validateResourceConfig } from '../utility/verification/verify-resource';
16+
import { OpensearchClient } from '../utility/opensearch/opensearch-client';
17+
18+
export default async function githubActivityEventsMonitor(app: Probot, context: any, resource: Resource): Promise<void> {
19+
if (!(await validateResourceConfig(app, context, resource))) return;
20+
21+
const repoName = context.payload.repository?.name;
22+
const orgName = context.payload.organization?.login || context.payload.repository?.owner?.login;
23+
24+
const event = {
25+
id: context.id,
26+
organization: orgName,
27+
repository: repoName,
28+
type: context.name,
29+
action: context.payload.action,
30+
sender: context.payload.sender?.login,
31+
created_at: new Date().toISOString(),
32+
};
33+
34+
const client = await new OpensearchClient().getClient();
35+
36+
const [month, year] = [new Date().getMonth() + 1, new Date().getFullYear()].map((num) => String(num).padStart(2, '0'));
37+
38+
try {
39+
await client.index({
40+
index: `github-activity-events-${month}-${year}`,
41+
body: event,
42+
});
43+
app.log.info('Event indexed successfully.');
44+
} catch (error) {
45+
app.log.error(`Error indexing event: ${error}`);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* The OpenSearch Contributors require contributions made to
6+
* this file be licensed under the Apache-2.0 license or a
7+
* compatible open source license.
8+
*/
9+
10+
import { Logger, Probot } from 'probot';
11+
import { OpensearchClient } from '../../src/utility/opensearch/opensearch-client';
12+
import githubActivityEventsMonitor from '../../src/call/github-activity-events-monitor';
13+
14+
jest.mock('../../src/utility/opensearch/opensearch-client');
15+
16+
describe('githubActivityEventsMonitor', () => {
17+
let app: Probot;
18+
let context: any;
19+
let resource: any;
20+
21+
beforeEach(() => {
22+
app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' });
23+
app.log = {
24+
info: jest.fn(),
25+
error: jest.fn(),
26+
} as unknown as Logger;
27+
context = {
28+
name: 'eventType',
29+
id: 'id',
30+
payload: {
31+
repository: {
32+
name: 'repo',
33+
owner: { login: 'org' },
34+
},
35+
action: 'action',
36+
sender: {
37+
login: 'sender',
38+
},
39+
},
40+
};
41+
resource = {
42+
organizations: new Map([
43+
[
44+
'org',
45+
{
46+
repositories: new Map([['repo', 'repo object']]),
47+
},
48+
],
49+
]),
50+
};
51+
});
52+
53+
it('should index events', async () => {
54+
const mockClient = {
55+
index: jest.fn().mockResolvedValue({}),
56+
};
57+
(OpensearchClient as jest.Mock).mockImplementation(() => {
58+
return { getClient: jest.fn().mockResolvedValue(mockClient) };
59+
});
60+
jest.spyOn(Date.prototype, 'toISOString').mockReturnValue('2024-10-04T21:00:06.875Z');
61+
await githubActivityEventsMonitor(app, context, resource);
62+
expect(mockClient.index).toHaveBeenCalledWith({
63+
index: expect.stringMatching(/^github-activity-events-\d{2}-\d{4}$/),
64+
body: expect.objectContaining({
65+
id: 'id',
66+
organization: 'org',
67+
repository: 'repo',
68+
type: 'eventType',
69+
action: 'action',
70+
sender: 'sender',
71+
created_at: '2024-10-04T21:00:06.875Z',
72+
}),
73+
});
74+
expect(app.log.info).toHaveBeenCalledWith('Event indexed successfully.');
75+
});
76+
77+
it('should log an error if indexing fails', async () => {
78+
const mockClient = {
79+
index: jest.fn().mockRejectedValue(new Error('Indexing failed')),
80+
};
81+
(OpensearchClient as jest.Mock).mockImplementation(() => {
82+
return { getClient: jest.fn().mockResolvedValue(mockClient) };
83+
});
84+
await githubActivityEventsMonitor(app, context, resource);
85+
expect(app.log.error).toHaveBeenCalledWith('Error indexing event: Error: Indexing failed');
86+
});
87+
});

0 commit comments

Comments
 (0)