import { githubAPIEndpoints } from './APIEndpoints'; export class APIHelper { private static buildGithubHeaders(ghToken: string) { return { Accept: 'application/vnd.github+json', Authorization: `Bearer ${ghToken}`, 'X-GitHub-Api-Version': '2022-11-28', }; } static requestHACAPI(options: Partial) { const ssoUrl = Cypress.expose('SSO_URL'); const oidcUser = JSON.parse(localStorage.getItem(`oidc.user:${ssoUrl}:cloud-services`)); const token = oidcUser.access_token as string; options.headers = { authorization: `Bearer ${token}`, accept: 'application/json', }; return cy.request(options); } static githubRequest(method: Cypress.HttpMethod, url: string, body?: Cypress.RequestBody) { return cy.env(['GH_TOKEN']).then(({ GH_TOKEN }: { GH_TOKEN: string }) => { const options: Partial = { method, url, headers: APIHelper.buildGithubHeaders(GH_TOKEN), }; if (body) { options.body = body; } return cy.request(options); }); } static checkResponseBodyAndStatusCode( url: string, responseBodyContent: string, waitInterval: number = 2000, retryNum: number = 0, maxRetryNum: number = 10, headers?: object, ) { expect(retryNum).to.be.lessThan(maxRetryNum); const options: Partial = { url, timeout: 30000, failOnStatusCode: false, }; if (headers) { options.headers = headers; } cy.request(options).then((resp) => { if (resp.status === 200 && JSON.stringify(resp.body).includes(responseBodyContent) === true) { cy.log( `The response body of URL: ${url}, now contains the content: ${responseBodyContent}`, ); return; } cy.log('The response body of URL doesnt contain the expected content yet, retrying...'); cy.wait(waitInterval); this.checkResponseBodyAndStatusCode( url, responseBodyContent, waitInterval, retryNum + 1, maxRetryNum, headers, ); }); } static deleteGitHubRepository(owner: string, repoName: string) { this.githubRequest('DELETE', githubAPIEndpoints.testRepo(owner, repoName)); } static deleteGitHubRepositoryIfExists(owner: string, repoName: string) { const url = githubAPIEndpoints.testRepo(owner, repoName); return cy.env(['GH_TOKEN']).then(({ GH_TOKEN }: { GH_TOKEN: string }) => cy .request({ method: 'DELETE', url, headers: APIHelper.buildGithubHeaders(GH_TOKEN), failOnStatusCode: false, }) .then((response) => { if (response.status === 404) { cy.log(`Repo not found during cleanup (already deleted): ${owner}/${repoName}`); } else if (response.status >= 400) { cy.log(`Repo cleanup failed with status ${response.status}: ${owner}/${repoName}`); } }), ); } static createRepositoryFromTemplate( templateOwner: string, templateRepoName: string, targetOwner: string, targetRepoName: string, isPrivate: boolean = false, ) { const body = { owner: targetOwner, name: targetRepoName, private: isPrivate, }; this.githubRequest( 'POST', githubAPIEndpoints.templateRepo(templateOwner, templateRepoName), body, ); } static mergePR( owner: string, repoName: string, pullNumber: number, commitTitle: string, commitMessage: string, ) { const body = { commit_title: `${commitTitle}`, commit_message: `${commitMessage}` }; APIHelper.githubRequest( 'PUT', githubAPIEndpoints.merge(owner, repoName, pullNumber), body, ).then((result) => { expect(result.body.merged).to.be.true; }); } }