diff --git a/plugins/github-issues/src/api/gitHubIssuesApi.test.ts b/plugins/github-issues/src/api/gitHubIssuesApi.test.ts index 4d1c3ffff3..070e9ab2f6 100644 --- a/plugins/github-issues/src/api/gitHubIssuesApi.test.ts +++ b/plugins/github-issues/src/api/gitHubIssuesApi.test.ts @@ -22,80 +22,123 @@ import { ConfigApi, ErrorApi } from '@backstage/core-plugin-api'; import { ForwardedError } from '@backstage/errors'; import { gitHubIssuesApi } from './gitHubIssuesApi'; +function getFragment( + filterBy = '', + orderBy = 'field: UPDATED_AT, direction: DESC', +) { + return ( + '\n' + + ' \n' + + ' fragment issues on Repository {\n' + + ' issues(\n' + + ' states: OPEN\n' + + ' first: 10\n' + + ` filterBy: { ${filterBy} }\n` + + ` orderBy: { ${orderBy} }\n` + + ' ) {\n' + + ' totalCount\n' + + ' edges {\n' + + ' node {\n' + + ' assignees(first: 10) {\n' + + ' edges {\n' + + ' node {\n' + + ' avatarUrl\n' + + ' login\n' + + ' }\n' + + ' }\n' + + ' }\n' + + ' author {\n' + + ' login\n' + + ' }\n' + + ' repository {\n' + + ' nameWithOwner\n' + + ' }\n' + + ' title\n' + + ' url\n' + + ' participants {\n' + + ' totalCount\n' + + ' }\n' + + ' updatedAt\n' + + ' createdAt\n' + + ' comments(last: 1) {\n' + + ' totalCount\n' + + ' }\n' + + ' }\n' + + ' }\n' + + ' }\n' + + ' }\n' + + ' \n' + + '\n' + + ' query {\n' + + ' \n' + + ' yoyo: repository(name: "yo-yo", owner: "mrwolny") {\n' + + ' ...issues\n' + + ' }\n' + + ' ,\n' + + ' yoyox: repository(name: "yoyo", owner: "mrwolny") {\n' + + ' ...issues\n' + + ' }\n' + + ' ,\n' + + ' yoyoxx: repository(name: "yo.yo", owner: "mrwolny") {\n' + + ' ...issues\n' + + ' }\n' + + ' \n' + + ' } \n' + + ' ' + ); +} + describe('gitHubIssuesApi', () => { describe('fetchIssuesByRepoFromGitHub', () => { - it('should call GitHub API with correct query with fragment for each repo', async () => { - const api = gitHubIssuesApi( + let api: ReturnType; + + afterEach(() => { + jest.clearAllMocks(); + }); + + beforeEach(() => { + api = gitHubIssuesApi( { getAccessToken: jest.fn() }, { getOptionalConfigArray: jest.fn(), } as unknown as ConfigApi, { post: jest.fn() } as unknown as ErrorApi, ); + }); + it('should call GitHub API with correct query with fragment for each repo', async () => { await api.fetchIssuesByRepoFromGitHub( ['mrwolny/yo-yo', 'mrwolny/yoyo', 'mrwolny/yo.yo'], 10, ); + + expect(mockGraphQLQuery).toHaveBeenCalledTimes(1); + expect(mockGraphQLQuery).toHaveBeenCalledWith(getFragment()); + }); + + it('should call Github API with the correct filterBy and orderBy clauses', async () => { + await api.fetchIssuesByRepoFromGitHub( + ['mrwolny/yo-yo', 'mrwolny/yoyo', 'mrwolny/yo.yo'], + 10, + { + filterBy: { + labels: ['bug'], + states: ['OPEN'], + assignee: 'someone', + }, + orderBy: { + field: 'COMMENTS', + direction: 'ASC', + }, + }, + ); + + const expectedFilterBy = `labels: [ "bug"], states: OPEN, assignee: "someone"`; + const expectedOrderBy = 'field: COMMENTS, direction: ASC'; + expect(mockGraphQLQuery).toHaveBeenCalledTimes(1); expect(mockGraphQLQuery).toHaveBeenCalledWith( - '\n' + - ' \n' + - ' fragment issues on Repository {\n' + - ' issues(\n' + - ' states: OPEN\n' + - ' first: 10\n' + - ' orderBy: { field: UPDATED_AT, direction: DESC }\n' + - ' ) {\n' + - ' totalCount\n' + - ' edges {\n' + - ' node {\n' + - ' assignees(first: 10) {\n' + - ' edges {\n' + - ' node {\n' + - ' avatarUrl\n' + - ' login\n' + - ' }\n' + - ' }\n' + - ' }\n' + - ' author {\n' + - ' login\n' + - ' }\n' + - ' repository {\n' + - ' nameWithOwner\n' + - ' }\n' + - ' title\n' + - ' url\n' + - ' participants {\n' + - ' totalCount\n' + - ' }\n' + - ' updatedAt\n' + - ' createdAt\n' + - ' comments(last: 1) {\n' + - ' totalCount\n' + - ' }\n' + - ' }\n' + - ' }\n' + - ' }\n' + - ' }\n' + - ' \n' + - '\n' + - ' query {\n' + - ' \n' + - ' yoyo: repository(name: "yo-yo", owner: "mrwolny") {\n' + - ' ...issues\n' + - ' }\n' + - ' ,\n' + - ' yoyox: repository(name: "yoyo", owner: "mrwolny") {\n' + - ' ...issues\n' + - ' }\n' + - ' ,\n' + - ' yoyoxx: repository(name: "yo.yo", owner: "mrwolny") {\n' + - ' ...issues\n' + - ' }\n' + - ' \n' + - ' } \n' + - ' ', + getFragment(expectedFilterBy, expectedOrderBy), ); }); }); diff --git a/plugins/github-issues/src/api/gitHubIssuesApi.ts b/plugins/github-issues/src/api/gitHubIssuesApi.ts index dc5623a7bc..01a6a59532 100644 --- a/plugins/github-issues/src/api/gitHubIssuesApi.ts +++ b/plugins/github-issues/src/api/gitHubIssuesApi.ts @@ -110,7 +110,7 @@ export const gitHubIssuesApi = ( field: 'UPDATED_AT', direction: 'DESC', }, - }: IssuesByRepoOptions, + }: IssuesByRepoOptions = {}, ): Promise => { const graphql = await getOctokit(); const safeNames: Array = []; @@ -134,13 +134,6 @@ export const gitHubIssuesApi = ( }; }); - // eslint-disable-next-line no-console - console.log(` - --------------------------------------------------- - ${createIssueByRepoQuery(repositories, itemsPerRepo, { filterBy, orderBy })} - --------------------------------------------------- - `); - let issuesByRepo: IssuesByRepo = {}; try { issuesByRepo = await graphql(