-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
4 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
name: Activity Events Monitor | ||
|
||
events: | ||
- issues.opened | ||
- issues.closed | ||
- issues.labeled | ||
- issues.unlabeled | ||
- issues.transferred | ||
- issues.assigned | ||
- issue_comment.created | ||
- pull_request.closed | ||
- pull_request.opened | ||
- pull_request.labeled | ||
- pull_request.unlabeled | ||
- pull_request.assigned | ||
- pull_request_review.submitted | ||
- pull_request_review_comment.created | ||
- gollum | ||
# - push | ||
# - release.released | ||
# - project.edited | ||
|
||
tasks: | ||
- name: Activity Events Monitor Operation | ||
call: github-activity-events-monitor@default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
// Name : githubActivityEventsMonitor | ||
// Description : Indexes events pertaining to user activity to OpenSearch | ||
|
||
import { Probot } from 'probot'; | ||
import { Resource } from '../service/resource/resource'; | ||
import { validateResourceConfig } from '../utility/verification/verify-resource'; | ||
import { OpensearchClient } from '../utility/opensearch/opensearch-client'; | ||
|
||
export default async function githubActivityEventsMonitor(app: Probot, context: any, resource: Resource): Promise<void> { | ||
if (!(await validateResourceConfig(app, context, resource))) return; | ||
|
||
const repoName = context.payload.repository?.name; | ||
const orgName = context.payload.organization?.login || context.payload.repository?.owner?.login; | ||
|
||
const event = { | ||
id: context.id, | ||
organization: orgName, | ||
repository: repoName, | ||
type: context.name, | ||
action: context.payload.action, | ||
sender: context.payload.sender?.login, | ||
created_at: new Date().toISOString(), | ||
}; | ||
|
||
const client = await new OpensearchClient().getClient(); | ||
|
||
const [month, year] = [new Date().getMonth() + 1, new Date().getFullYear()].map((num) => String(num).padStart(2, '0')); | ||
|
||
try { | ||
await client.index({ | ||
index: `github-activity-events-${month}-${year}`, | ||
body: event, | ||
}); | ||
app.log.info('Event indexed successfully.'); | ||
} catch (error) { | ||
app.log.error(`Error indexing event: ${error}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { Logger, Probot } from 'probot'; | ||
import { OpensearchClient } from '../../src/utility/opensearch/opensearch-client'; | ||
import githubActivityEventsMonitor from '../../src/call/github-activity-events-monitor'; | ||
|
||
jest.mock('../../src/utility/opensearch/opensearch-client'); | ||
|
||
describe('githubActivityEventsMonitor', () => { | ||
let app: Probot; | ||
let context: any; | ||
let resource: any; | ||
|
||
beforeEach(() => { | ||
app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' }); | ||
app.log = { | ||
info: jest.fn(), | ||
error: jest.fn(), | ||
} as unknown as Logger; | ||
context = { | ||
name: 'eventType', | ||
id: 'id', | ||
payload: { | ||
repository: { | ||
name: 'repo', | ||
owner: { login: 'org' }, | ||
}, | ||
action: 'action', | ||
sender: { | ||
login: 'sender', | ||
}, | ||
}, | ||
}; | ||
resource = { | ||
organizations: new Map([ | ||
[ | ||
'org', | ||
{ | ||
repositories: new Map([['repo', 'repo object']]), | ||
}, | ||
], | ||
]), | ||
}; | ||
}); | ||
|
||
it('should index events', async () => { | ||
const mockClient = { | ||
index: jest.fn().mockResolvedValue({}), | ||
}; | ||
(OpensearchClient as jest.Mock).mockImplementation(() => { | ||
return { getClient: jest.fn().mockResolvedValue(mockClient) }; | ||
}); | ||
jest.spyOn(Date.prototype, 'toISOString').mockReturnValue('2024-10-04T21:00:06.875Z'); | ||
await githubActivityEventsMonitor(app, context, resource); | ||
expect(mockClient.index).toHaveBeenCalledWith({ | ||
index: expect.stringMatching(/^github-activity-events-\d{2}-\d{4}$/), | ||
body: expect.objectContaining({ | ||
id: 'id', | ||
organization: 'org', | ||
repository: 'repo', | ||
type: 'eventType', | ||
action: 'action', | ||
sender: 'sender', | ||
created_at: '2024-10-04T21:00:06.875Z', | ||
}), | ||
}); | ||
expect(app.log.info).toHaveBeenCalledWith('Event indexed successfully.'); | ||
}); | ||
|
||
it('should log an error if indexing fails', async () => { | ||
const mockClient = { | ||
index: jest.fn().mockRejectedValue(new Error('Indexing failed')), | ||
}; | ||
(OpensearchClient as jest.Mock).mockImplementation(() => { | ||
return { getClient: jest.fn().mockResolvedValue(mockClient) }; | ||
}); | ||
await githubActivityEventsMonitor(app, context, resource); | ||
expect(app.log.error).toHaveBeenCalledWith('Error indexing event: Error: Indexing failed'); | ||
}); | ||
}); |