Skip to content

Commit 2878548

Browse files
assess pr comments
1 parent fd42d41 commit 2878548

4 files changed

Lines changed: 21 additions & 17 deletions

File tree

src/api-sdk/github/github.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class Github {
8181
}
8282

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

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

102-
const { prUrl, prPostbody } = createPullRequestParams(repoUrl, body.prTitle, body.prBody, body.branchName, 'main');
102+
const { prUrl, prPostbody } = createPullRequestParams(repoUrl, body.prTitle, body.prBody, body.branchName, body.baseBranch);
103103
return this.postData(prUrl, prPostbody);
104104
}
105105

src/api-sdk/github/utils.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { ApiResponse, ApiErrorResponse } from '../response';
22

3+
const defaultBranch = 'main';
4+
35
export const extractBaseShaHelper = (response: ApiResponse<any> | ApiErrorResponse) => {
4-
if ('resource' in response && response.resource && 'object' in response.resource && 'sha' in response.resource.object) {
6+
if ('resource' in response && 'object' in response.resource){
57
return response.resource.object.sha;
6-
} else {
7-
return response;
88
}
9+
throw new Error(`Error: ${JSON.stringify(response)}`);
910
};
1011

1112
export const extractShaHelper = (response: ApiResponse<any> | ApiErrorResponse) => {
12-
if ('resource' in response && response.resource && 'sha' in response.resource) {
13+
if ('resource' in response){
1314
return response.resource.sha;
14-
} else {
15-
return response;
1615
}
16+
throw new Error(`Error: ${JSON.stringify(response)}`);
1717
};
1818

19-
export const getShaParams = (repoUrl: string) => {
20-
const shaUrl = `${repoUrl}/git/refs/heads/main`;
19+
export const getShaParams = (repoUrl: string, baseBranch: string = defaultBranch) => {
20+
const shaUrl = `${repoUrl}/git/refs/heads/${baseBranch}`;
2121
return shaUrl;
2222
};
2323

@@ -73,7 +73,7 @@ export const updateBranchReferenceParams = (repoUrl: string, branch: string, com
7373
return { refUrl, refBody };
7474
};
7575

76-
export const createPullRequestParams = (repoUrl: string, prTitle: string, prBody: string, branchName: string, baseBranch: string) => {
76+
export const createPullRequestParams = (repoUrl: string, prTitle: string, prBody: string, branchName: string, baseBranch: string = defaultBranch) => {
7777
const prUrl = `${repoUrl}/pulls`;
7878
const prPostbody = {
7979
title: prTitle,

test/mock/data.mock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ export const MOCK_INVALID_SHA_RESPONSE = {
207207
object: ['Resource not found']
208208
};
209209

210+
export const MOCK_API_ERROR = new Error(`Error: ${JSON.stringify(MOCK_INVALID_SHA_RESPONSE)}`);
211+
210212
export const MOCK_POST_BRANCH = { ref: 'refs/heads/test-branch', sha: 'ABC12345678' };
211213

212214
export const MOCK_POST_BLOB = {

test/unit/api-sdk/github/utils.spec.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
createPullRequestParams
1313
} from '../../../../src/api-sdk/github/utils';
1414
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';
15-
import { MOCK_POST_BLOB, MOCK_INVALID_SHA_RESPONSE, MOCK_POST_BRANCH, MOCK_POST_TREE, MOCK_POST_COMMIT, MOCK_POST_PR } from '../../../mock/data.mock';
15+
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';
1616

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

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

3232
test('extractBaseShaHelper should return response if resource does not exist', () => {
33-
const result = extractBaseShaHelper(MOCK_INVALID_SHA_RESPONSE);
34-
expect(result).toEqual(MOCK_INVALID_SHA_RESPONSE);
33+
jest.spyOn(global, 'Error').mockImplementationOnce(() => MOCK_API_ERROR);
34+
35+
expect(() => extractBaseShaHelper(MOCK_INVALID_SHA_RESPONSE)).toThrowError(MOCK_API_ERROR);
3536
});
3637

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

4647
test('extractShaHelper should return response if sha does not exist', () => {
47-
const result = extractShaHelper(MOCK_INVALID_SHA_RESPONSE);
48-
expect(result).toEqual(MOCK_INVALID_SHA_RESPONSE);
48+
jest.spyOn(global, 'Error').mockImplementationOnce(() => MOCK_API_ERROR);
49+
50+
expect(() => extractShaHelper(MOCK_INVALID_SHA_RESPONSE)).toThrowError(MOCK_API_ERROR);
4951
});
5052

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

8991
test('createPullRequestParams should return the correct PR URL and body', () => {
90-
const { prUrl, prPostbody } = createPullRequestParams(MOCK_REPO_URL, MOCK_PR_TITLE, MOCK_PR_BODY, MOCK_BRANCH_NAME, 'main');
92+
const { prUrl, prPostbody } = createPullRequestParams(MOCK_REPO_URL, MOCK_PR_TITLE, MOCK_PR_BODY, MOCK_BRANCH_NAME);
9193
expect(prUrl).toBe(`${MOCK_REPO_URL}/pulls`);
9294
expect(prPostbody).toEqual(MOCK_POST_PR);
9395
});

0 commit comments

Comments
 (0)