Skip to content

Commit

Permalink
Merge pull request #7 from cabinetoffice/add-members-per-team-github-…
Browse files Browse the repository at this point in the history
…method

Add members per team GitHub method
  • Loading branch information
harley-harris authored Dec 11, 2023
2 parents 6ba8463 + c7748c4 commit 6c1f14d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/api-sdk/github/github.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GitHubRepos, GitHubMembers, GitHubTeams } from './type';
import { GitHubRepos, GitHubMembers, GitHubTeams, GitHubMembersPerTeam } from './type';
import { ApiResponse, ApiErrorResponse } from '../response';
import { reposMapping, membersMapping, teamsMapping } from './mapping';
import { reposMapping, membersMapping, teamsMapping, membersPerTeamMapping } from './mapping';
import { HttpRequest } from '../../http-request';

export class Github {
Expand All @@ -19,6 +19,10 @@ export class Github {
return await this.fetchData<GitHubTeams[]>(url, teamsMapping);
}

public async getMembersPerTeam(url: string): Promise<ApiResponse<GitHubMembersPerTeam[]> | ApiErrorResponse> {
return await this.fetchData<GitHubMembersPerTeam[]>(url, membersPerTeamMapping);
}

private async fetchData<T>(
url: string,
mappingFunction: (body: any) => T
Expand Down
8 changes: 7 additions & 1 deletion src/api-sdk/github/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GitHubMembers, GitHubRepos, GitHubTeams } from './type';
import { GitHubMembers, GitHubRepos, GitHubTeams, GitHubMembersPerTeam } from './type';

export const reposMapping = (body: any[]): GitHubRepos[] => {
return body.map((obj) => ({
Expand Down Expand Up @@ -31,3 +31,9 @@ export const teamsMapping = (body: any[]): GitHubTeams[] => {
members_url: obj.members_url
}));
};

export const membersPerTeamMapping = (body: any[]): GitHubMembersPerTeam[] => {
return body.map((obj) => ({
login: obj.login,
}));
};
4 changes: 4 additions & 0 deletions src/api-sdk/github/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ export interface GitHubMembers {
html_url: string;
repos_url: string;
}

export interface GitHubMembersPerTeam {
login: string;
}
14 changes: 14 additions & 0 deletions test/mock/data.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ export const MOCK_TEAM_FETCH_RESPONSE = {
resource: MOCK_TEAMS
};

export const MOCK_MEMBERS_PER_TEAM = [
{
login: 'member1',
},
{
login: 'member2',
}
];

export const MOCK_MEMBERS_PER_TEAM_RESPONSE = {
httpStatusCode: 200,
resource: MOCK_MEMBERS_PER_TEAM
};

export const MOCK_ERROR = {
error: 'Error: test error'
};
Expand Down
4 changes: 2 additions & 2 deletions test/unit/api-sdk/api-sdk.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { jest, beforeEach, afterEach, describe, test, expect } from '@jest/globals';
import { beforeEach, describe, test, expect } from '@jest/globals';

import ApiSDK from '../../../src/api-sdk/api-sdk';
import { HttpRequest } from '../../../src/http-request';
Expand All @@ -12,7 +12,7 @@ describe('api-sdk test suites', () => {
apiSdk = new ApiSDK(new HttpRequest(MOCK_HEADERS));
});

test('ApiSDK should have a gitHub property which is an instance of Github class', async () => {
test('ApiSDK should have a gitHub property which is an instance of Github class', () => {
expect(apiSdk.gitHub).toBeInstanceOf(Github);
});
});
12 changes: 11 additions & 1 deletion test/unit/api-sdk/github/github.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
MOCK_TEAMS,
MOCK_ERROR_RESPONSE,
MOCK_ERROR,
MOCK_UNMAPPED_ENTREE_REPO
MOCK_UNMAPPED_ENTREE_REPO,
MOCK_MEMBERS_PER_TEAM,
MOCK_MEMBERS_PER_TEAM_RESPONSE
} from '../../../mock/data.mock';
import { HttpResponse } from '../../../../src/http-request/type';

Expand Down Expand Up @@ -67,6 +69,14 @@ describe('Github sdk module test suites', () => {
expect(result).toEqual(MOCK_TEAM_FETCH_RESPONSE);
});

test('Should return membersPerTeam data as an object', async () => {
httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse(MOCK_MEMBERS_PER_TEAM));

const url = 'https://api.github.com/organizations/test_org_id/team/test_team_id/members';
const result = await github.getMembersPerTeam(url);
expect(result).toEqual(MOCK_MEMBERS_PER_TEAM_RESPONSE);
});

test('Should return an object with an error property', async () => {
httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse(MOCK_TEAMS, 500, MOCK_ERROR));

Expand Down

0 comments on commit 6c1f14d

Please sign in to comment.