Best JavaScript code snippet using qawolf
commit-and-push.ts
Source:commit-and-push.ts
1// Copyright 2020 Google LLC2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14/* eslint-disable node/no-unsupported-features/node-builtins */15import * as assert from 'assert';16import {describe, it, before, afterEach} from 'mocha';17import {octokit, setup} from './util';18import * as sinon from 'sinon';19import {GetResponseTypeFromEndpointMethod} from '@octokit/types';20import * as handler from '../src/github/commit-and-push';21import {Changes, FileData, TreeObject, RepoDomain} from '../src/types';22type GetCommitResponse = GetResponseTypeFromEndpointMethod<23 typeof octokit.git.getCommit24>;25type CreateTreeResponse = GetResponseTypeFromEndpointMethod<26 typeof octokit.git.createTree27>;28type CreateCommitResponse = GetResponseTypeFromEndpointMethod<29 typeof octokit.git.createCommit30>;31before(() => {32 setup();33});34describe('Push', () => {35 const sandbox = sinon.createSandbox();36 const origin: RepoDomain = {37 owner: 'Foo',38 repo: 'Bar',39 };40 const sha = 'asdf1234';41 afterEach(() => {42 sandbox.restore();43 });44 it('GitHub tree objects that are generated correctly for text files in a sub-directory', () => {45 const changes: Changes = new Map();46 changes.set('a/foo.txt', new FileData('Foo content'));47 assert.deepStrictEqual(handler.generateTreeObjects(changes), [48 {49 path: 'a/foo.txt',50 mode: '100644',51 type: 'blob',52 content: 'Foo content',53 },54 ]);55 });56 it('has objects that are generated correctly for text files that are deleted', () => {57 const changes: Changes = new Map();58 changes.set('b/bar.txt', new FileData(null));59 assert.deepStrictEqual(handler.generateTreeObjects(changes), [60 {61 path: 'b/bar.txt',62 mode: '100644',63 type: 'blob',64 sha: null,65 },66 ]);67 });68 it('has objects that are generated correctly for deleted exe files', () => {69 const changes: Changes = new Map();70 changes.set('baz.exe', new FileData(null, '100755'));71 assert.deepStrictEqual(handler.generateTreeObjects(changes), [72 {73 path: 'baz.exe',74 mode: '100755',75 type: 'blob',76 sha: null,77 },78 ]);79 });80 it('has objects that are generated correctly for empty text files', () => {81 const changes: Changes = new Map();82 changes.set('empty.txt', new FileData(''));83 assert.deepStrictEqual(handler.generateTreeObjects(changes), [84 {85 path: 'empty.txt',86 mode: '100644',87 type: 'blob',88 content: '',89 },90 ]);91 });92 it('Calls octokit functions with correct params', async () => {93 const tree: TreeObject[] = [94 {95 path: 'a/foo.txt',96 mode: '100644',97 type: 'blob',98 content: 'Foo content',99 },100 {101 path: 'b/bar.txt',102 mode: '100644',103 type: 'blob',104 sha: null,105 },106 {107 path: 'baz.exe',108 mode: '100755',109 type: 'blob',110 sha: null,111 },112 {113 path: 'empty.txt',114 mode: '100644',115 type: 'blob',116 content: '',117 },118 ];119 const commitResponseData = await import(120 './fixtures/get-commit-response.json'121 );122 const createTreeResponseData = await import(123 './fixtures/create-tree-response.json'124 );125 const getCommitResponse = {126 headers: {},127 status: 200,128 url: 'http://fake-url.com',129 data: commitResponseData,130 } as unknown as GetCommitResponse;131 const createTreeResponse = {132 headers: {},133 status: 201,134 url: 'http://fake-url.com',135 data: createTreeResponseData,136 } as unknown as CreateTreeResponse;137 // setup138 const stubGetCommit = sandbox139 .stub(octokit.git, 'getCommit')140 .resolves(getCommitResponse);141 const stubCreateTree = sandbox142 .stub(octokit.git, 'createTree')143 .resolves(createTreeResponse);144 // tests145 const treeSha = await handler.createTree(octokit, origin, sha, tree);146 sandbox.assert.calledOnceWithExactly(stubGetCommit, {147 owner: origin.owner,148 repo: origin.repo,149 commit_sha: sha,150 });151 sandbox.assert.calledWithExactly(stubCreateTree, {152 owner: origin.owner,153 repo: origin.repo,154 tree,155 base_tree: getCommitResponse.data.tree.sha,156 });157 assert.strictEqual(treeSha, createTreeResponse.data.sha);158 });159});160describe('Commit', () => {161 const sandbox = sinon.createSandbox();162 afterEach(() => {163 sandbox.restore();164 });165 const origin: RepoDomain = {166 owner: 'Foo',167 repo: 'Bar',168 };169 const treeSha = 'TREE-asfd1234';170 const head = 'head-asdf1234';171 const message = 'Hello world';172 it('Invokes octokit function called with correct values', async () => {173 // setup174 const createCommitResponseData = await import(175 './fixtures/create-commit-response.json'176 );177 const createCommitResponse = {178 headers: {},179 status: 201,180 url: 'http://fake-url.com',181 data: createCommitResponseData,182 } as unknown as CreateCommitResponse;183 const stubCreateCommit = sandbox184 .stub(octokit.git, 'createCommit')185 .resolves(createCommitResponse);186 // tests187 const sha = await handler.createCommit(188 octokit,189 origin,190 head,191 treeSha,192 message193 );194 assert.strictEqual(sha, createCommitResponse.data.sha);195 sandbox.assert.calledOnceWithExactly(stubCreateCommit, {196 owner: origin.owner,197 repo: origin.repo,198 message,199 tree: treeSha,200 parents: [head],201 });202 });203});204describe('Update branch reference', () => {205 const sandbox = sinon.createSandbox();206 afterEach(() => {207 sandbox.restore();208 });209 const origin: RepoDomain = {210 owner: 'Foo',211 repo: 'Bar',212 };213 const sha = 'asdf1234';214 it('Invokes octokit function called with correct values', async () => {215 // setup216 const stubUpdateRef = sandbox.stub(octokit.git, 'updateRef');217 // tests218 await handler.updateRef(219 octokit,220 {branch: 'test-branch-name', ...origin},221 sha,222 false223 );224 sandbox.assert.calledOnceWithExactly(stubUpdateRef, {225 owner: origin.owner,226 repo: origin.repo,227 sha,228 ref: 'heads/test-branch-name',229 force: false,230 });231 });232});233describe('Commit and push function', async () => {234 const sandbox = sinon.createSandbox();235 const oldHeadSha = 'OLD-head-Sha-asdf1234';236 const changes: Changes = new Map();237 const origin: RepoDomain = {238 owner: 'Foo',239 repo: 'Bar',240 };241 const branchName = 'test-branch-name';242 const message = 'Hello world';243 const commitResponseData = await import(244 './fixtures/get-commit-response.json'245 );246 const createTreeResponseData = await import(247 './fixtures/create-tree-response.json'248 );249 const getCommitResponse = {250 headers: {},251 status: 200,252 url: 'http://fake-url.com',253 data: commitResponseData,254 } as unknown as GetCommitResponse;255 const createTreeResponse = {256 headers: {},257 status: 201,258 url: 'http://fake-url.com',259 data: createTreeResponseData,260 } as unknown as CreateTreeResponse;261 const createCommitResponseData = await import(262 './fixtures/create-commit-response.json'263 );264 const createCommitResponse = {265 headers: {},266 status: 201,267 url: 'http://fake-url.com',268 data: createCommitResponseData,269 } as unknown as CreateCommitResponse;270 afterEach(() => {271 sandbox.restore();272 });273 it('When everything works it calls functions with correct parameter values', async () => {274 // setup275 const stubGetCommit = sandbox276 .stub(octokit.git, 'getCommit')277 .resolves(getCommitResponse);278 const stubCreateTree = sandbox279 .stub(octokit.git, 'createTree')280 .resolves(createTreeResponse);281 const stubCreateCommit = sandbox282 .stub(octokit.git, 'createCommit')283 .resolves(createCommitResponse);284 const stubUpdateRef = sandbox.stub(octokit.git, 'updateRef');285 // tests286 await handler.commitAndPush(287 octokit,288 oldHeadSha,289 changes,290 {branch: branchName, ...origin},291 message,292 true293 );294 sandbox.assert.calledOnceWithExactly(stubGetCommit, {295 owner: origin.owner,296 repo: origin.repo,297 commit_sha: oldHeadSha,298 });299 sandbox.assert.calledWithExactly(stubCreateTree, {300 owner: origin.owner,301 repo: origin.repo,302 tree: [],303 base_tree: getCommitResponse.data.tree.sha,304 });305 sandbox.assert.calledOnceWithExactly(stubCreateCommit, {306 owner: origin.owner,307 repo: origin.repo,308 message,309 tree: createTreeResponse.data.sha,310 parents: [oldHeadSha],311 });312 sandbox.assert.calledOnceWithExactly(stubUpdateRef, {313 owner: origin.owner,314 repo: origin.repo,315 sha: createCommitResponse.data.sha,316 ref: 'heads/test-branch-name',317 force: true,318 });319 });320 it('Forwards GitHub error if getCommit fails', async () => {321 const error = new Error('Error committing');322 sandbox.stub(octokit.git, 'getCommit').rejects(error);323 await assert.rejects(handler.createTree(octokit, origin, '', []), error);324 });325 it('Forwards GitHub error if createTree fails', async () => {326 // setup327 const error = new Error('Error committing');328 sandbox.stub(octokit.git, 'getCommit').resolves(getCommitResponse);329 sandbox.stub(octokit.git, 'createTree').rejects(error);330 // tests331 await assert.rejects(handler.createTree(octokit, origin, '', []), error);332 });...
move-release-notes
Source:move-release-notes
1#!/usr/bin/env node2const authenticate = require('./github-authentication')3const { GITKEEP_FILE, UNRELEASED_DIR, RELEASE_NOTES_DIR } = require('./constants')4const { program } = require('commander')5program6 .option('-d, --debug', 'enable extreme logging')7 .requiredOption('--deliverino-private-key <deliverino-private-key>', 'private key of the deliverino github app in pem format with base64 encoding')8 .requiredOption('--owner <owner>', 'owner of the current repository, usually "Integreat"')9 .requiredOption('--repo <repo>', 'the current repository, usually "integreat-webapp" or "integreat-react-native-app"')10 .requiredOption('--branch <branch>', 'the current branch')11 .requiredOption('--commit-sha <commit-sha>', 'the sha1 of the current commit')12const moveReleaseNotes = async ({ newVersionName, deliverinoPrivateKey, owner, repo, branch, commitSha }) => {13 const appOctokit = await authenticate({ deliverinoPrivateKey, owner, repo })14 const lastCommit = await appOctokit.git.getCommit({ owner, repo, commit_sha: commitSha })15 // Tree of the root folder of the project16 const rootTreeSha = lastCommit.data.tree.sha17 const rootTree = await appOctokit.git.getTree({ owner, repo, tree_sha: rootTreeSha })18 // Tree of the 'release-notes' folder of the project19 const releaseNoteTreeObject = rootTree.data.tree.find(it => it.path === RELEASE_NOTES_DIR)20 if (!releaseNoteTreeObject) {21 console.error(rootTree)22 throw new Error('Root tree does not contain release notes tree, probably since the response is truncated.')23 }24 const releaseNoteTree = await appOctokit.git.getTree({ owner, repo, tree_sha: releaseNoteTreeObject.sha })25 // Tree of the unreleased release notes26 const unreleasedTreeObject = releaseNoteTree.data.tree.find(it => it.path === UNRELEASED_DIR)27 if (!unreleasedTreeObject) {28 console.error(releaseNoteTree)29 throw new Error('Release note tree does not contain unreleased tree, probably since the response is truncated.')30 }31 const unreleasedTree = await appOctokit.git.getTree({ owner, repo, tree_sha: unreleasedTreeObject.sha })32 // Only keep file '.gitkeep' in 'unreleased' tree, move everything else to '<new-version-name>'33 const unreleasedReleaseNotes = unreleasedTree.data.tree.filter(it => it.path !== GITKEEP_FILE)34 const keepFile = unreleasedTree.data.tree.find(it => it.path === GITKEEP_FILE)35 // Creating an empty tree is not possible36 if (unreleasedReleaseNotes.length === 0) {37 return38 }39 const newVersionTree = await appOctokit.git.createTree({ owner, repo, tree: unreleasedReleaseNotes })40 const newUnreleasedTree = await appOctokit.git.createTree({ owner, repo, tree: [keepFile] })41 const newReleaseNotesContent = releaseNoteTree.data.tree.map(it => {42 const { size, url, ...rest } = it43 if (it.path === UNRELEASED_DIR) {44 return { ...rest, sha: newUnreleasedTree.data.sha }45 }46 return rest47 })48 // mode: '040000' means subdirectory(tree)49 // See https://developer.github.com/v3/git/trees/#create-a-tree50 newReleaseNotesContent.push({ path: newVersionName, sha: newVersionTree.data.sha, type: 'tree', mode: '040000' })51 const newReleaseNotesTree = await appOctokit.git.createTree({ owner, repo, tree: newReleaseNotesContent })52 // Update the root tree with the new 'release-notes' tree53 const rootUpdate = rootTree.data.tree.map(it => {54 const { size, url, ...rest } = it55 if (it.path === RELEASE_NOTES_DIR) {56 return { ...rest, sha: newReleaseNotesTree.data.sha }57 }58 return rest59 })60 const updatedRootTree = await appOctokit.git.createTree({ owner, repo, tree: rootUpdate })61 // Commit the changes in the tree62 const renameCommit = await appOctokit.git.createCommit({63 owner,64 repo,65 message: `Move release notes to ${newVersionName}\n[skip ci]`,66 tree: updatedRootTree.data.sha,67 parents: [commitSha]68 })69 await appOctokit.git.updateRef({70 owner,71 repo,72 ref: `heads/${branch}`,73 sha: renameCommit.data.sha74 })75}76program77 .command('move-to <new-version-name>')78 .description('move the release notes in \'unreleased\' to a new subdirectory <new-version-name>')79 .action(async newVersionName => {80 try {81 await moveReleaseNotes({ newVersionName, ...program })82 } catch (e) {83 console.error(e)84 process.exit(1)85 }86 })...
createPullRequest.js
Source:createPullRequest.js
...15 per_page: 116 })17 let latestCommitSha = response.data[0].sha18 const treeSha = response.data[0].commit.tree.sha19 response = await octokit.git.createTree({20 owner,21 repo,22 base_tree: treeSha,23 tree: Object.keys(changes.files).map(path => {24 return {25 path,26 mode: '100644',27 content: changes.files[path]28 }29 })30 })31 const newTreeSha = response.data.sha32 response = await octokit.git.createCommit({33 owner,...
Using AI Code Generation
1const { createTree } = require('qawolf');2const { Octokit } = require('@octokit/rest');3const octokit = new Octokit({4});5const createTree = async () => {6 const tree = await octokit.git.createTree({7 {8 },9 });10};11createTree();12const { createTree } = require('qawolf');13const { Octokit } = require('@octokit/rest');14const octokit = new Octokit({15});16const createTree = async () => {17 const tree = await octokit.git.createTree({18 {19 },20 });21};22createTree();23const { createTree } = require('qawolf');24const { Octokit } = require('@octokit/rest');25const octokit = new Octokit({26});27const createTree = async () => {28 const tree = await octokit.git.createTree({29 {30 },31 });32};33createTree();34const { createTree } = require('qawolf');35const { Octokit } = require('@octokit/rest');36const octokit = new Octokit({37});38const createTree = async () => {
Using AI Code Generation
1const { Octokit } = require("@octokit/rest");2const octokit = new Octokit({3});4const tree = await octokit.git.createTree({5 {6 },7});8console.log(tree);
Using AI Code Generation
1const { Octokit } = require("@octokit/rest");2const octokit = new Octokit({3});4const params = {5 {6 },7};8octokit.git.createTree(params).then((response) => {9 console.log(response);10});11const { Octokit } = require("@octokit/rest");12const octokit = new Octokit({13});14const params = {15 {16 },17};18octokit.git.createTree(params).then((response) => {19 console.log(response);20});21const { Octokit } = require("@octokit/rest");22const octokit = new Octokit({23});24const params = {25 {26 },
Using AI Code Generation
1const tree = await octokit.git.createTree({2 {3 content: "console.log('Hello World!');",4 },5});6const commit = await octokit.git.createCommit({7});8const ref = await octokit.git.createRef({9});10const pullRequest = await octokit.pulls.create({11});12const tree = await octokit.git.createTree({13 {14 content: "console.log('Hello World!');",15 },16});17const commit = await octokit.git.createCommit({18});19const ref = await octokit.git.createRef({20});
Using AI Code Generation
1const qawolf = require("qawolf");2async function main() {3 const { octokit, owner, repo } = await qawolf.githubUtils.getOctokit();4 const { sha: treeSha } = await octokit.git.createTree({5 {6 content: "console.log('hello world')"7 }8 });9 const { sha: commitSha } = await octokit.git.createCommit({10 });11 const { data } = await octokit.git.getCommit({12 });13 console.log(data);14}15main();
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!