How to use octokit.git.getRef method in qawolf

Best JavaScript code snippet using qawolf

git-client.spec.ts

Source:git-client.spec.ts Github

copy

Full Screen

1import { EventEmitter } from "events";2import { Octokit } from "@octokit/rest";3import { GitClient } from "./git-client";4import { expect, describe, it, jest, beforeAll } from "@jest/globals";5import { HandledError } from "./handled-error";6describe("git client", () => {7 const testFile = `8 some: yaml9 file: here10 `;11 const testRepoName = "some-repo";12 const pathToFile = "path/to/file.yaml";13 const testOrgName = "some_org";14 const testSourceBranch = "main";15 const testOutputBranch = "main";16 const testMessage = "test message";17 const existingFileSha = "test existing file sha";18 const testBranchName = "test_branch";19 describe("the get file method", () => {20 const eventEmitter = new EventEmitter();21 describe("given successful api response", () => {22 const octokit = mockOctokit({23 repos: {24 getContent: jest.fn(() => ({25 data: {26 content: Buffer.from(testFile).toString("base64"),27 sha: "testSha",28 },29 })),30 },31 });32 const client = new GitClient(octokit, eventEmitter);33 it("should get a file from github", async () => {34 const { data, sha } = await client.getFile(35 pathToFile,36 testRepoName,37 testOrgName,38 testBranchName,39 );40 expect(octokit.repos.getContent).toHaveBeenCalledWith({41 owner: testOrgName,42 repo: testRepoName,43 path: pathToFile,44 ref: `heads/${testBranchName}`,45 });46 expect(data).toEqual(testFile);47 expect(sha).toEqual("testSha");48 });49 });50 describe("when api errors", () => {51 const octokit = mockOctokit({52 repos: {53 getContent: () => {54 throw new Error();55 },56 },57 });58 const client = new GitClient(octokit, eventEmitter);59 it("should get a file from github", async () => {60 try {61 await client.getFile(62 pathToFile,63 testRepoName,64 testOrgName,65 testBranchName,66 );67 } catch (e) {68 expect(e).toBeInstanceOf(HandledError);69 }70 });71 });72 });73 describe("the putFile method", () => {74 describe("given the same target and output branch", () => {75 const octokit = mockOctokit({76 git: {77 getRef: jest.fn(() => ({78 data: {79 object: {80 sha: "refSha",81 },82 },83 })),84 createRef: jest.fn(() => ({85 data: {86 object: {87 sha: "newRefSha",88 },89 },90 })),91 },92 repos: {93 createOrUpdateFileContents: jest.fn(() => ({94 url: "resulting.url",95 })),96 },97 });98 const eventEmitter = new EventEmitter();99 const client = new GitClient(octokit, eventEmitter);100 beforeAll(async () => {101 await client.putFile(102 testFile,103 testRepoName,104 testOrgName,105 testSourceBranch,106 testOutputBranch,107 pathToFile,108 pathToFile,109 testMessage,110 existingFileSha,111 );112 });113 it("should not attempt to get ref", () => {114 expect(octokit.git.getRef).toHaveBeenCalledTimes(0);115 });116 it("should not create new branch", () => {117 expect(octokit.git.createRef).toHaveBeenCalledTimes(0);118 });119 it("should return url of the new blob", () => {120 expect(octokit.repos.createOrUpdateFileContents).toHaveBeenCalledWith({121 owner: testOrgName,122 message: testMessage,123 content: Buffer.from(testFile).toString("base64"),124 repo: testRepoName,125 path: pathToFile,126 branch: testOutputBranch,127 sha: existingFileSha,128 });129 });130 });131 describe("given a different target branch that already exists", () => {132 const eventEmitter = new EventEmitter();133 describe("when successfully getting branch ref", () => {134 const octokit = mockOctokit({135 git: {136 getRef: jest.fn(() => ({137 data: {138 ref: "baseBranchRef",139 },140 })),141 createRef: jest.fn(() => ({142 data: {143 object: {144 sha: "newRefSha",145 },146 },147 })),148 },149 repos: {150 createOrUpdateFileContents: jest.fn(() => ({151 url: "resulting.url",152 })),153 },154 });155 const client = new GitClient(octokit, eventEmitter);156 const mockLogListener = jest.fn();157 beforeAll(async () => {158 eventEmitter.on("log", mockLogListener);159 await client.putFile(160 testFile,161 testRepoName,162 testOrgName,163 testSourceBranch,164 "different_output_branch",165 pathToFile,166 pathToFile,167 testMessage,168 existingFileSha,169 );170 });171 it("should use existing branch", () => {172 expect(octokit.git.getRef).toHaveBeenCalledTimes(1);173 expect(octokit.git.createRef).toHaveBeenCalledTimes(0);174 });175 it("should return url of the new blob", () => {176 expect(octokit.repos.createOrUpdateFileContents).toHaveBeenCalledWith(177 {178 owner: testOrgName,179 message: testMessage,180 content: Buffer.from(testFile).toString("base64"),181 repo: testRepoName,182 path: pathToFile,183 branch: "baseBranchRef",184 sha: existingFileSha,185 },186 );187 });188 it("should emit log message", () => {189 expect(mockLogListener).toHaveBeenCalledTimes(1);190 });191 });192 describe("when api errors", () => {193 const octokit = mockOctokit({194 git: {195 getRef: jest.fn(() => {196 throw new Error();197 }),198 },199 repos: {200 createOrUpdateFileContents: jest.fn(() => ({201 url: "resulting.url",202 })),203 },204 });205 const eventEmitter = new EventEmitter();206 const client = new GitClient(octokit, eventEmitter);207 it("should throw handled exception", async () => {208 try {209 await client.putFile(210 testFile,211 testRepoName,212 testOrgName,213 testSourceBranch,214 "different_output_branch",215 pathToFile,216 pathToFile,217 testMessage,218 existingFileSha,219 );220 } catch (e) {221 expect(e).toBeInstanceOf(HandledError);222 }223 });224 });225 });226 describe("given new branch", () => {227 const eventEmitter = new EventEmitter();228 describe("when successfully creating branch", () => {229 const octokit = mockOctokit({230 git: {231 getRef: jest.fn(({ ref }) => {232 if (ref === `heads/different_output_branch`) {233 throw new Error();234 } else {235 return {236 data: {237 ref: "baseBranchRef",238 object: {239 sha: "baseRefSha",240 },241 },242 };243 }244 }),245 createRef: jest.fn(() => ({246 data: {247 ref: "newBranchRef",248 },249 })),250 },251 repos: {252 createOrUpdateFileContents: jest.fn(() => ({253 url: "resulting.url",254 })),255 },256 });257 const client = new GitClient(octokit, eventEmitter);258 const mockLogListener = jest.fn();259 beforeAll(async () => {260 eventEmitter.on("log", mockLogListener);261 await client.putFile(262 testFile,263 testRepoName,264 testOrgName,265 testSourceBranch,266 "different_output_branch",267 pathToFile,268 pathToFile,269 testMessage,270 existingFileSha,271 );272 });273 it("should create a new branch", () => {274 expect(octokit.git.getRef).toHaveBeenCalledTimes(2);275 expect(octokit.git.createRef).toHaveBeenCalledWith({276 owner: testOrgName,277 repo: testRepoName,278 ref: `refs/heads/different_output_branch`,279 sha: "baseRefSha",280 });281 });282 it("should return url of the new blob", () => {283 expect(octokit.repos.createOrUpdateFileContents).toHaveBeenCalledWith(284 {285 owner: testOrgName,286 message: testMessage,287 content: Buffer.from(testFile).toString("base64"),288 repo: testRepoName,289 path: pathToFile,290 branch: "newBranchRef",291 sha: existingFileSha,292 },293 );294 });295 it("should emit log message", () => {296 expect(mockLogListener).toHaveBeenCalledTimes(1);297 });298 });299 describe("when unable to create a new branch", () => {300 const octokit = mockOctokit({301 git: {302 getRef: jest.fn(({ ref }) => {303 if (ref === `heads/different_output_branch`) {304 throw new Error();305 } else {306 return {307 data: {308 ref: "baseBranchRef",309 object: {310 sha: "baseRefSha",311 },312 },313 };314 }315 }),316 createRef: jest.fn(() => {317 throw new Error();318 }),319 },320 });321 const client = new GitClient(octokit, eventEmitter);322 it("should thorw handled error", async () => {323 try {324 await client.putFile(325 testFile,326 testRepoName,327 testOrgName,328 testSourceBranch,329 "different_output_branch",330 pathToFile,331 pathToFile,332 testMessage,333 existingFileSha,334 );335 } catch (e) {336 expect(e).toBeInstanceOf(HandledError);337 }338 });339 });340 });341 describe("given new sourcePath and outputPath are different", () => {342 const eventEmitter = new EventEmitter();343 describe("when the output file file does not exist", () => {344 const octokit = mockOctokit({345 git: {346 getRef: jest.fn(({ ref }) => {347 if (ref === `heads/different_output_branch`) {348 throw new Error();349 } else {350 return {351 data: {352 ref: "baseBranchRef",353 object: {354 sha: "baseRefSha",355 },356 },357 };358 }359 }),360 createRef: jest.fn(() => ({361 data: {362 ref: "newBranchRef",363 },364 })),365 },366 repos: {367 createOrUpdateFileContents: jest.fn(() => ({368 url: "resulting.url",369 })),370 getContent: () => {371 throw new Error();372 },373 },374 });375 const client = new GitClient(octokit, eventEmitter);376 beforeAll(async () => {377 await client.putFile(378 testFile,379 testRepoName,380 testOrgName,381 testSourceBranch,382 "different_output_branch",383 pathToFile,384 "some/other/path/file.yaml",385 testMessage,386 existingFileSha,387 );388 });389 it("should use source file sha when PUTting file", () => {390 expect(391 octokit.repos.createOrUpdateFileContents,392 ).toHaveBeenCalledTimes(1);393 expect(octokit.repos.createOrUpdateFileContents).toHaveBeenCalledWith(394 {395 message: testMessage,396 content: Buffer.from(testFile).toString("base64"),397 owner: testOrgName,398 repo: testRepoName,399 path: "some/other/path/file.yaml",400 branch: "newBranchRef",401 sha: existingFileSha,402 },403 );404 });405 });406 describe("when the output file already exists", () => {407 const octokit = mockOctokit({408 git: {409 getRef: jest.fn(({ ref }) => {410 if (ref === `heads/different_output_branch`) {411 throw new Error();412 } else {413 return {414 data: {415 ref: "baseBranchRef",416 object: {417 sha: "baseRefSha",418 },419 },420 };421 }422 }),423 createRef: jest.fn(() => ({424 data: {425 ref: "newBranchRef",426 },427 })),428 },429 repos: {430 createOrUpdateFileContents: jest.fn(() => ({431 url: "resulting.url",432 })),433 getContent: () => ({434 data: {435 content: Buffer.from(testFile).toString(`base64`),436 sha: `otherFileSha`,437 },438 }),439 },440 });441 const client = new GitClient(octokit, eventEmitter);442 beforeAll(async () => {443 await client.putFile(444 testFile,445 testRepoName,446 testOrgName,447 testSourceBranch,448 "different_output_branch",449 pathToFile,450 "some/other/path/file.yaml",451 testMessage,452 existingFileSha,453 );454 });455 it("should use existing file sha when PUTting file", async () => {456 expect(457 octokit.repos.createOrUpdateFileContents,458 ).toHaveBeenCalledTimes(1);459 expect(octokit.repos.createOrUpdateFileContents).toHaveBeenCalledWith(460 {461 message: testMessage,462 content: Buffer.from(testFile).toString("base64"),463 owner: testOrgName,464 repo: testRepoName,465 path: "some/other/path/file.yaml",466 branch: "newBranchRef",467 sha: "otherFileSha",468 },469 );470 });471 });472 });473 describe("given api error throw while PUTting file", () => {474 const octokit = mockOctokit({475 git: {476 getRef: jest.fn(() => {477 return {478 data: {479 ref: "baseBranchRef",480 },481 };482 }),483 },484 repos: {485 createOrUpdateFileContents: jest.fn(() => {486 throw new Error();487 }),488 },489 });490 const eventEmitter = new EventEmitter();491 const client = new GitClient(octokit, eventEmitter);492 it("should throw a handled expection", async () => {493 try {494 await client.putFile(495 testFile,496 testRepoName,497 testOrgName,498 testSourceBranch,499 "different_output_branch",500 pathToFile,501 pathToFile,502 testMessage,503 existingFileSha,504 );505 } catch (e) {506 expect(e).toBeInstanceOf(HandledError);507 }508 });509 });510 });511 describe("the createPullRequest method", () => {512 const eventEmitter = new EventEmitter();513 describe("given succesfull api response", () => {514 const octokit = mockOctokit({515 pulls: {516 create: jest.fn(() => ({517 data: {518 url: "test.url",519 },520 })),521 },522 });523 const client = new GitClient(octokit, eventEmitter);524 it("should return pull request url", async () => {525 const result = await client.createPullRequest(526 testOutputBranch,527 testRepoName,528 "pr title",529 testSourceBranch,530 testOutputBranch,531 );532 expect(result).toBe("test.url");533 });534 });535 describe("given api error", () => {536 const octokit = mockOctokit({537 pulls: () => {538 throw new Error();539 },540 });541 const client = new GitClient(octokit, eventEmitter);542 it("should throw handled error", async () => {543 try {544 await client.createPullRequest(545 testOutputBranch,546 testRepoName,547 "pr title",548 testSourceBranch,549 testOutputBranch,550 );551 } catch (e) {552 expect(e).toBeInstanceOf(HandledError);553 }554 });555 });556 });557});558function mockOctokit(methods?: any) {559 const Mock = jest.fn<Octokit, unknown[]>(() => methods);560 return new Mock();...

Full Screen

Full Screen

github.js

Source:github.js Github

copy

Full Screen

...44 owner: repoOwner,45 repo: repoName,46 ref: `tags/${tag}`47 };48 return octokit.git.getRef(tagParams).then(() => octokit.git.deleteRef(tagParams));49 });50 return Promise.all([51 octokit.git.getRef(branchParams).then(() => octokit.git.deleteRef(branchParams)),52 ...removeTagPromises53 ]).catch(err => Assert.strictEqual(404, err.status));54}55/**56 * Remove Branch57 * @method removeBranch58 * @param {String} repoOwner Owner of the repository59 * @param {String} repoName Name of the repository60 * @param {String} branchName Name of the branch to delete61 * @return {Promise}62 */63function removeBranch(repoOwner, repoName, branchName) {64 const branchParams = {65 owner: repoOwner,66 repo: repoName,67 ref: `heads/${branchName}`68 };69 return octokit.git.getRef(branchParams).then(() => octokit.git.deleteRef(branchParams));70}71/**72 * Close a pull request for a given repository73 * @method closePullRequest74 * @param {String} repoOwner Owner of the repository75 * @param {String} repoName Name of the repository76 * @param {Number} prNumber Number of the pull request77 * @return {Promise}78 */79function closePullRequest(repoOwner, repoName, prNumber) {80 return octokit.pulls.update({81 owner: repoOwner,82 repo: repoName,83 pull_number: prNumber,...

Full Screen

Full Screen

repo.js

Source:repo.js Github

copy

Full Screen

...5 branch = branch.join('/');6 let octokit = new Octokit({ auth: GITHUB_ACCESS_TOKEN });7 let ref;8 if (branch) {9 ref = (await octokit.git.getRef({ owner, repo, ref: `heads/${branch}` })).data;10 } else {11 let [master, main] = await Promise.all([12 octokit.git.getRef({ owner, repo, ref: `heads/master` }).catch(err => {}),13 octokit.git.getRef({ owner, repo, ref: `heads/main` }).catch(err => {}),14 ]);15 branch = master ? 'master' : 'main';16 ref = (master || main).data;17 }18 let { data: commit } = await octokit.git.getCommit({ owner, repo, commit_sha: ref.object.sha });19 let commitDate = new Date(commit.committer.date);20 let detailsUrl = `https://github.com/${owner}/${repo}`;21 if (!['master', 'main'].includes(branch)) detailsUrl += `/tree/${branch}`;22 res.json({23 schema_version: '3.0.0',24 packages: [{25 details: detailsUrl,26 releases: [{27 version: strftime(commitDate, '%Y.%m.%d.%H.%M.%S'),...

Full Screen

Full Screen

main.ts

Source:main.ts Github

copy

Full Screen

...9async function main() {10 core.info("start auto releasing...");11 const { owner, repo } = github.context.repo;12 const octokit = github.getOctokit(token);13 const developRef = await octokit.git.getRef({14 owner,15 repo,16 ref: `heads/${develop_branch}`,17 });18 try {19 const ref = await octokit.git.getRef({20 owner,21 repo,22 ref: `heads/${head}`,23 });24 core.info(`${head} already exists, skiping auto release.`);25 } catch (err) {26 if (err.status === 404) {27 await octokit.git.createRef({28 owner,29 repo,30 ref: `refs/heads/${head}`,31 sha: developRef.data.object.sha,32 });33 core.info("start creating pull request");...

Full Screen

Full Screen

github-helper.js

Source:github-helper.js Github

copy

Full Screen

1const { info } = require('./log')2/**3 *4 * @param env5 * @returns {Promise<void>}6 */7module.exports.ensureBaseBranchExists = async function ensureBaseBranchExists(8 env9) {10 info(`checking for base branch ${env.config.base_branch}`)11 await env.octokit.git12 .getRef({ ref: `heads/${env.config.base_branch}`, ...env.repo_context })13 .catch(() => {14 throw new Error(15 `Could not find base branch '${env.config.base_branch}' - please check your configuration`16 )17 })18 return true19}20module.exports.doesReleaseBranchExists = async function doesReleaseBranchExists(21 env22) {23 const result = await env.octokit.git24 .getRef({ ref: `heads/${env.config.release_branch}`, ...env.repo_context })25 .catch(() => {26 return undefined27 })28 return result != undefined...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { Octokit } = require('@octokit/rest');2const octokit = new Octokit();3async function main() {4 const { data } = await octokit.git.getRef({5 });6 console.log(data);7}8main();9const { Octokit } = require('@octokit/rest');10const octokit = new Octokit();11async function main() {12 const { data } = await octokit.git.getRef({13 });14 console.log(data);15}16main();17const { Octokit } = require('@octokit/rest');18const octokit = new Octokit();19async function main() {20 const { data } = await octokit.git.getRef({21 });22 console.log(data);23}24main();25const { Octokit } = require('@octokit/rest');26const octokit = new Octokit();27async function main() {28 const { data } = await octokit.git.getRef({29 });30 console.log(data);31}32main();33const { Octokit } = require('@octokit/rest');34const octokit = new Octokit();35async function main() {36 const { data } = await octokit.git.getRef({37 });38 console.log(data);39}40main();41const { Octokit } = require('@octokit/rest');

Full Screen

Using AI Code Generation

copy

Full Screen

1const { octokit } = require("qawolf");2async function test() {3 const ref = await octokit.git.getRef({4 });5 console.log(ref);6}7test();8const { Octokit } = require("@octokit/rest");9const octokit = new Octokit();10async function test() {11 const ref = await octokit.git.getRef({12 });13 console.log(ref);14}15test();16const { Octokit } = require("@octokit/rest");17const octokit = new Octokit();18async function test() {19 const ref = await octokit.git.getRef({20 });21 console.log(ref);22}23test();24const { Octokit } = require("@octokit/rest");25const octokit = new Octokit();26async function test() {27 const ref = await octokit.git.getRef({28 });29 console.log(ref);30}31test();32const { Octokit } = require("@octokit/rest");33const octokit = new Octokit();34async function test() {35 const ref = await octokit.git.getRef({36 });37 console.log(ref);38}39test();40const { Octokit } = require("@octokit/rest");41const octokit = new Octokit();42async function test() {43 const ref = await octokit.git.getRef({44 });45 console.log(ref);46}47test();48const { Octokit } = require("@octokit/rest");49const octokit = new Octokit();50async function test() {

Full Screen

Using AI Code Generation

copy

Full Screen

1const { Octokit } = require("@octokit/rest");2const octokit = new Octokit({3});4const getRef = async () => {5 try {6 const { data } = await octokit.git.getRef({7 });8 console.log("data", data);9 return data;10 } catch (error) {11 console.log("error", error);12 }13};14const createRef = async () => {15 const ref = await getRef();16 try {17 const { data } = await octokit.git.createRef({18 });19 console.log("data", data);20 } catch (error) {21 console.log("error", error);22 }23};24createRef();25{26 "scripts": {27 },28 "dependencies": {29 }30}31{32 "dependencies": {33 "@octokit/auth-app": {

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run qawolf automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful