Skip to content

Commit

Permalink
assess pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMurray97 committed Oct 14, 2024
1 parent fd42d41 commit 2878548
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/api-sdk/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Github {
}

public async postPullRequest (repoUrl: string, body: GitHubPullRequest): Promise<ApiResponse<any> | ApiErrorResponse> {
const shaResponse = await this.getData(getShaParams(repoUrl));
const shaResponse = await this.getData(getShaParams(repoUrl, body.baseBranch));
const baseSha = extractBaseShaHelper(shaResponse);

const { branchUrl, branchBody } = createBranchParams(repoUrl, body.branchName, baseSha);
Expand All @@ -99,7 +99,7 @@ export class Github {
const { refUrl, refBody } = updateBranchReferenceParams(repoUrl, body.branchName, commitSha);
await this.postData(refUrl, refBody);

const { prUrl, prPostbody } = createPullRequestParams(repoUrl, body.prTitle, body.prBody, body.branchName, 'main');
const { prUrl, prPostbody } = createPullRequestParams(repoUrl, body.prTitle, body.prBody, body.branchName, body.baseBranch);
return this.postData(prUrl, prPostbody);
}

Expand Down
18 changes: 9 additions & 9 deletions src/api-sdk/github/utils.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { ApiResponse, ApiErrorResponse } from '../response';

const defaultBranch = 'main';

export const extractBaseShaHelper = (response: ApiResponse<any> | ApiErrorResponse) => {
if ('resource' in response && response.resource && 'object' in response.resource && 'sha' in response.resource.object) {
if ('resource' in response && 'object' in response.resource){
return response.resource.object.sha;
} else {
return response;
}
throw new Error(`Error: ${JSON.stringify(response)}`);
};

export const extractShaHelper = (response: ApiResponse<any> | ApiErrorResponse) => {
if ('resource' in response && response.resource && 'sha' in response.resource) {
if ('resource' in response){
return response.resource.sha;
} else {
return response;
}
throw new Error(`Error: ${JSON.stringify(response)}`);
};

export const getShaParams = (repoUrl: string) => {
const shaUrl = `${repoUrl}/git/refs/heads/main`;
export const getShaParams = (repoUrl: string, baseBranch: string = defaultBranch) => {
const shaUrl = `${repoUrl}/git/refs/heads/${baseBranch}`;
return shaUrl;
};

Expand Down Expand Up @@ -73,7 +73,7 @@ export const updateBranchReferenceParams = (repoUrl: string, branch: string, com
return { refUrl, refBody };
};

export const createPullRequestParams = (repoUrl: string, prTitle: string, prBody: string, branchName: string, baseBranch: string) => {
export const createPullRequestParams = (repoUrl: string, prTitle: string, prBody: string, branchName: string, baseBranch: string = defaultBranch) => {
const prUrl = `${repoUrl}/pulls`;
const prPostbody = {
title: prTitle,
Expand Down
2 changes: 2 additions & 0 deletions test/mock/data.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ export const MOCK_INVALID_SHA_RESPONSE = {
object: ['Resource not found']
};

export const MOCK_API_ERROR = new Error(`Error: ${JSON.stringify(MOCK_INVALID_SHA_RESPONSE)}`);

export const MOCK_POST_BRANCH = { ref: 'refs/heads/test-branch', sha: 'ABC12345678' };

export const MOCK_POST_BLOB = {
Expand Down
14 changes: 8 additions & 6 deletions test/unit/api-sdk/github/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
createPullRequestParams
} from '../../../../src/api-sdk/github/utils';
import { MOCK_REPO_URL, MOCK_BASE_SHA, MOCK_BLOB_SHA, MOCK_TREE_SHA, MOCK_COMMIT_SHA, MOCK_BRANCH_NAME, MOCK_PATH, MOCK_COMMIT_MESSAGE, MOCK_PR_TITLE, MOCK_PR_BODY } from '../../../mock/text.mock';
import { MOCK_POST_BLOB, MOCK_INVALID_SHA_RESPONSE, MOCK_POST_BRANCH, MOCK_POST_TREE, MOCK_POST_COMMIT, MOCK_POST_PR } from '../../../mock/data.mock';
import { MOCK_POST_BLOB, MOCK_INVALID_SHA_RESPONSE, MOCK_POST_BRANCH, MOCK_POST_TREE, MOCK_POST_COMMIT, MOCK_POST_PR, MOCK_API_ERROR } from '../../../mock/data.mock';

describe('Github utils test suites', () => {

Expand All @@ -30,8 +30,9 @@ describe('Github utils test suites', () => {
});

test('extractBaseShaHelper should return response if resource does not exist', () => {
const result = extractBaseShaHelper(MOCK_INVALID_SHA_RESPONSE);
expect(result).toEqual(MOCK_INVALID_SHA_RESPONSE);
jest.spyOn(global, 'Error').mockImplementationOnce(() => MOCK_API_ERROR);

expect(() => extractBaseShaHelper(MOCK_INVALID_SHA_RESPONSE)).toThrowError(MOCK_API_ERROR);
});

test('extractShaHelper should return sha if it exists', () => {
Expand All @@ -44,8 +45,9 @@ describe('Github utils test suites', () => {
});

test('extractShaHelper should return response if sha does not exist', () => {
const result = extractShaHelper(MOCK_INVALID_SHA_RESPONSE);
expect(result).toEqual(MOCK_INVALID_SHA_RESPONSE);
jest.spyOn(global, 'Error').mockImplementationOnce(() => MOCK_API_ERROR);

expect(() => extractShaHelper(MOCK_INVALID_SHA_RESPONSE)).toThrowError(MOCK_API_ERROR);
});

test('getShaParams should return the correct sha URL', () => {
Expand Down Expand Up @@ -87,7 +89,7 @@ describe('Github utils test suites', () => {
});

test('createPullRequestParams should return the correct PR URL and body', () => {
const { prUrl, prPostbody } = createPullRequestParams(MOCK_REPO_URL, MOCK_PR_TITLE, MOCK_PR_BODY, MOCK_BRANCH_NAME, 'main');
const { prUrl, prPostbody } = createPullRequestParams(MOCK_REPO_URL, MOCK_PR_TITLE, MOCK_PR_BODY, MOCK_BRANCH_NAME);
expect(prUrl).toBe(`${MOCK_REPO_URL}/pulls`);
expect(prPostbody).toEqual(MOCK_POST_PR);
});
Expand Down

0 comments on commit 2878548

Please sign in to comment.