-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from cabinetoffice/IDP-442-add-call-to-submit-…
…well-formatted-pr-object-to-terraform-repo Idp 442 add call to submit well formatted pr object to terraform repo
- Loading branch information
Showing
7 changed files
with
298 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { ApiResponse, ApiErrorResponse } from '../response'; | ||
|
||
const defaultBranch = 'main'; | ||
|
||
export const extractBaseShaHelper = (response: ApiResponse<any> | ApiErrorResponse) => { | ||
if ('resource' in response && 'object' in response.resource){ | ||
return response.resource.object.sha; | ||
} | ||
throw new Error(`Error: ${JSON.stringify(response)}`); | ||
}; | ||
|
||
export const extractShaHelper = (response: ApiResponse<any> | ApiErrorResponse) => { | ||
if ('resource' in response){ | ||
return response.resource.sha; | ||
} | ||
throw new Error(`Error: ${JSON.stringify(response)}`); | ||
}; | ||
|
||
export const getShaParams = (repoUrl: string, baseBranch: string = defaultBranch) => { | ||
const shaUrl = `${repoUrl}/git/refs/heads/${baseBranch}`; | ||
return shaUrl; | ||
}; | ||
|
||
export const createBranchParams = (repoUrl: string, branchName: string, baseSha: string) => { | ||
const branchUrl = `${repoUrl}/git/refs`; | ||
const branchBody = { | ||
ref: `refs/heads/${branchName}`, | ||
sha: baseSha | ||
}; | ||
return { branchUrl, branchBody }; | ||
}; | ||
|
||
export const createBlobParams = (repoUrl: string, content: string) => { | ||
const blobUrl = `${repoUrl}/git/blobs`; | ||
const blobBody = { | ||
content: Buffer.from(content).toString('base64'), | ||
encoding: 'base64' | ||
}; | ||
return { blobUrl, blobBody }; | ||
}; | ||
|
||
export const createTreeParams = (repoUrl: string, baseTreeSha: string, path: string, blobSha: string) => { | ||
const treeUrl = `${repoUrl}/git/trees`; | ||
const treeBody = { | ||
base_tree: baseTreeSha, | ||
tree: [ | ||
{ | ||
path: path, | ||
mode: '100644', | ||
type: 'blob', | ||
sha: blobSha | ||
} | ||
] | ||
}; | ||
return { treeUrl, treeBody }; | ||
}; | ||
|
||
export const createCommitParams = (repoUrl: string, message: string, treeSha: string, parentSha: string) => { | ||
const commitUrl = `${repoUrl}/git/commits`; | ||
const commitBody = { | ||
message: message, | ||
tree: treeSha, | ||
parents: [parentSha] | ||
}; | ||
return { commitUrl, commitBody }; | ||
}; | ||
|
||
export const updateBranchReferenceParams = (repoUrl: string, branch: string, commitSha: string) => { | ||
const refUrl = `${repoUrl}/git/refs/heads/${branch}`; | ||
const refBody = { | ||
sha: commitSha | ||
}; | ||
return { refUrl, refBody }; | ||
}; | ||
|
||
export const createPullRequestParams = (repoUrl: string, prTitle: string, prBody: string, branchName: string, baseBranch: string = defaultBranch) => { | ||
const prUrl = `${repoUrl}/pulls`; | ||
const prPostbody = { | ||
title: prTitle, | ||
body: prBody, | ||
head: branchName, | ||
base: baseBranch | ||
}; | ||
return { prUrl, prPostbody }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const MOCK_REPO_URL = 'https://api.github.com/repos/test-repo'; | ||
|
||
export const MOCK_BASE_SHA = 'ABC12345678'; | ||
export const MOCK_BLOB_SHA = 'ABC12345678'; | ||
export const MOCK_TREE_SHA = 'ABC12345678'; | ||
export const MOCK_COMMIT_SHA = 'ABC12345678'; | ||
|
||
export const MOCK_COMMIT_MESSAGE = 'commit message'; | ||
export const MOCK_BRANCH_NAME = 'test-branch'; | ||
|
||
export const MOCK_PATH = 'terraform/account-1.tf'; | ||
|
||
export const MOCK_PR_TITLE = 'PR Title'; | ||
export const MOCK_PR_BODY = 'PR Body'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { jest, afterEach, describe, test, expect } from '@jest/globals'; | ||
|
||
import { | ||
extractBaseShaHelper, | ||
extractShaHelper, | ||
getShaParams, | ||
createBranchParams, | ||
createBlobParams, | ||
createTreeParams, | ||
createCommitParams, | ||
updateBranchReferenceParams, | ||
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, MOCK_API_ERROR } from '../../../mock/data.mock'; | ||
|
||
describe('Github utils test suites', () => { | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
test('extractBaseShaHelper should return sha if it exists', () => { | ||
const mockBaseShaResponse = { | ||
httpStatusCode: 200, | ||
resource: { object: { sha: MOCK_BASE_SHA } } | ||
}; | ||
const result = extractBaseShaHelper(mockBaseShaResponse); | ||
expect(result).toBe(MOCK_BASE_SHA); | ||
}); | ||
|
||
test('extractBaseShaHelper should return response if resource does not exist', () => { | ||
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', () => { | ||
const mockShaResponse = { | ||
httpStatusCode: 200, | ||
resource: { sha: MOCK_BLOB_SHA } | ||
}; | ||
const result = extractShaHelper(mockShaResponse); | ||
expect(result).toBe(MOCK_BLOB_SHA); | ||
}); | ||
|
||
test('extractShaHelper should return response if sha does not exist', () => { | ||
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', () => { | ||
const result = getShaParams(MOCK_REPO_URL); | ||
expect(result).toBe(`${MOCK_REPO_URL}/git/refs/heads/main`); | ||
}); | ||
|
||
test('createBranchParams should return the correct branch URL and body', () => { | ||
const { branchUrl, branchBody } = createBranchParams(MOCK_REPO_URL, MOCK_BRANCH_NAME, MOCK_BASE_SHA); | ||
|
||
expect(branchUrl).toBe(`${MOCK_REPO_URL}/git/refs`); | ||
expect(branchBody).toEqual(MOCK_POST_BRANCH); | ||
}); | ||
|
||
test('createBlobParams should return the correct blob URL and body', () => { | ||
const { blobUrl, blobBody } = createBlobParams(MOCK_REPO_URL, 'test content'); | ||
|
||
expect(blobUrl).toBe(`${MOCK_REPO_URL}/git/blobs`); | ||
expect(blobBody).toEqual(MOCK_POST_BLOB); | ||
}); | ||
|
||
test('createTreeParams should return the correct tree URL and body', () => { | ||
const { treeUrl, treeBody } = createTreeParams(MOCK_REPO_URL, MOCK_BASE_SHA, MOCK_PATH, MOCK_BLOB_SHA); | ||
|
||
expect(treeUrl).toBe(`${MOCK_REPO_URL}/git/trees`); | ||
expect(treeBody).toEqual(MOCK_POST_TREE); | ||
}); | ||
|
||
test('createCommitParams should return the correct commit URL and body', () => { | ||
const { commitUrl, commitBody } = createCommitParams(MOCK_REPO_URL, MOCK_COMMIT_MESSAGE, MOCK_TREE_SHA, MOCK_BASE_SHA); | ||
expect(commitUrl).toBe(`${MOCK_REPO_URL}/git/commits`); | ||
expect(commitBody).toEqual(MOCK_POST_COMMIT); | ||
}); | ||
|
||
test('updateBranchReferenceParams should return the correct ref URL and body', () => { | ||
const { refUrl, refBody } = updateBranchReferenceParams(MOCK_REPO_URL, MOCK_BRANCH_NAME, MOCK_COMMIT_SHA); | ||
expect(refUrl).toBe(`${MOCK_REPO_URL}/git/refs/heads/${MOCK_BRANCH_NAME}`); | ||
expect(refBody).toEqual({ sha: MOCK_COMMIT_SHA }); | ||
}); | ||
|
||
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); | ||
expect(prUrl).toBe(`${MOCK_REPO_URL}/pulls`); | ||
expect(prPostbody).toEqual(MOCK_POST_PR); | ||
}); | ||
}); |