Skip to content

Commit

Permalink
Add print-to-console test
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Zhu <[email protected]>
  • Loading branch information
peterzhuamazon committed Sep 27, 2024
1 parent 4323b91 commit fdf744c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/call/github-merged-pulls-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { OpensearchClient } from '../utility/opensearch/opensearch-client';
import { verifyOrgRepo } from '../utility/verification/verify-resource';

export default async function githubMergedPullsMonitor(app: Probot, context: any, resource: Resource): Promise<void> {
if (!(await verifyOrgRepo(app, context, resource)) && !(await verifyOrgRepo(app, context, resource, true))) return;
if (!(await verifyOrgRepo(app, context, resource))) return;

const pr = context.payload.pull_request;

Expand Down
2 changes: 1 addition & 1 deletion src/call/github-workflow-runs-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface WorkflowRunMonitorArgs {
}

export default async function githubWorkflowRunsMonitor(app: Probot, context: any, resource: Resource, { events }: WorkflowRunMonitorArgs): Promise<void> {
if (!(await verifyOrgRepo(app, context, resource)) && !(await verifyOrgRepo(app, context, resource, true))) return;
if (!(await verifyOrgRepo(app, context, resource))) return;

const job = context.payload.workflow_run;

Expand Down
5 changes: 3 additions & 2 deletions src/call/print-to-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default async function printToConsole(app: Probot, context: any, resource
app.log.info(text);
}

export async function printToConsoleHelloWorld(app: Probot): Promise<void> {
app.log.info('Hello World!');
export async function printToConsoleHelloWorld(app: Probot, context: any, resource: Resource): Promise<void> {
if (!(await verifyOrgRepo(app, context, resource))) return;
app.log.info('Hello World');
}
4 changes: 2 additions & 2 deletions src/utility/verification/verify-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import { Probot } from 'probot';
import { Resource } from '../../service/resource/resource';

export async function verifyOrgRepo(app: Probot, context: any, resource: Resource, repoOwner: boolean = false): Promise<boolean> {
const contextOrgName = repoOwner ? context.payload.repository?.owner?.login : context.payload.organization?.login;
export async function verifyOrgRepo(app: Probot, context: any, resource: Resource): Promise<boolean> {
const contextOrgName = context.payload.organization?.login || context.payload.repository?.owner?.login;
const contextRepoName = context.payload.repository?.name;

const org = resource.organizations.get(contextOrgName);
Expand Down
68 changes: 68 additions & 0 deletions test/call/print-to-console.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 printToConsole, { PrintToConsoleParams, printToConsoleHelloWorld } from '../../src/call/print-to-console';
import { Probot, Logger } from 'probot';

describe('printToConsoleFunctions', () => {
let app: Probot;
let context: any;
let resource: any;
let args: PrintToConsoleParams;

beforeEach(() => {
app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' });
app.log = {
info: jest.fn(),
error: jest.fn(),
} as unknown as Logger;
context = {
payload: {
repository: {
name: 'repo',
owner: { login: 'org' },
},
org: {
login: 'org',
},
},
log: {
info: jest.fn(),
error: jest.fn(),
},
};
resource = {
organizations: new Map([
[
'org',
{
repositories: new Map([['repo', 'repo object']]),
},
],
]),
};
args = {
text: 'test message 123',
};
});

describe('printToConsole', () => {
it('should print defined text in task', async () => {
await printToConsole(app, context, resource, args);
expect(app.log.info).toHaveBeenCalledWith('test message 123');
});
});

describe('printToConsoleHelloWorld', () => {
it('should print Hello World in task', async () => {
await printToConsoleHelloWorld(app, context, resource);
expect(app.log.info).toHaveBeenCalledWith('Hello World');
});
});
});

0 comments on commit fdf744c

Please sign in to comment.