How to use GITHUB_APP_CERTIFICATE method in Best

Best JavaScript code snippet using best

git-app.ts

Source:git-app.ts Github

copy

Full Screen

1/*2 * Copyright (c) 2019, salesforce.com, inc.3 * All rights reserved.4 * SPDX-License-Identifier: MIT5 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT6*/7// -- Modules & libs --------------------------------------------------------------------8import fs from 'fs';9import https from 'https';10import expandTilde from 'expand-tilde';11import jwt from 'jsonwebtoken';12import base64 from 'base-64';13import {Octokit} from '@octokit/rest';14// -- Env & config ----------------------------------------------------------------------15const GITHUB_USER_TOKEN = process.env.GIT_USER_TOKEN;16const GITHUB_APP_ID = process.env.GIT_APP_ID;17const GITHUB_APP_CERTIFICATE_PATH = process.env.GIT_APP_CERT_PATH;18const GITHUB_APP_CERTIFICATE_BASE64 = process.env.GIT_APP_CERT_BASE64;19const GITHUB_APP_CERTIFICATE = normalizeCert({20 cert: process.env.GITHUB_APP_CERTIFICATE,21 certPath: GITHUB_APP_CERTIFICATE_PATH,22 certBase64: GITHUB_APP_CERTIFICATE_BASE64,23});24// -- Types -----------------------------------------------------------------------------25export interface GithubFactoryConfig {26 applicationId: string;27 certificate: string;28 userToken: string;29}30// -- Utils -----------------------------------------------------------------------------31function stripQuotes(str = '') {32 const q = "'";33 return str[0] === q && str[str.length - 1] === q ? str.slice(1, -1) : str;34}35function normalizeCert({36 cert,37 certPath,38 certBase64,39}: {40 cert?: string;41 certPath?: string;42 certBase64?: string;43}): string | undefined {44 return (45 cert ||46 (certPath && fs.readFileSync(expandTilde(certPath), 'utf8')) ||47 (certBase64 && base64.decode(stripQuotes(certBase64)))48 );49}50function generateJwt(id: string, cert: string) {51 const payload = {52 iat: Math.floor(+new Date() / 1000), // Issued at time53 exp: Math.floor(+new Date() / 1000) - 1, // JWT expiration time54 iss: id, // Integration's GitHub id55 };56 // Sign with RSA SHA25657 return jwt.sign(payload, cert, { algorithm: 'RS256' });58}59// -- Public API & exports --------------------------------------------------------------60class GithubFactory {61 id?: string;62 cert?: string;63 token?: string;64 gitOpts: Octokit.Options;65 constructor({ applicationId, certificate, userToken }: Partial<GithubFactoryConfig>, gitClientOpts: Octokit.Options = {}) {66 if (!applicationId) {67 throw new Error ('APP_ID is required');68 }69 this.id = applicationId;70 this.cert = certificate;71 this.token = userToken;72 this.gitOpts = gitClientOpts;73 }74 async authenticateAsApplication(gitOpts = this.gitOpts) {75 const { id, cert } = this;76 if (!cert || !id) {77 throw new Error('CERT and ID are required to authenticated as an App');78 }79 const token = generateJwt(id, cert);80 const github = new Octokit({81 ...gitOpts,82 auth: `Bearer ${token}`83 });84 return github;85 }86 async authenticateAsInstallation(installationId?: number, gitOpts = this.gitOpts) {87 if (!installationId) {88 throw new Error ('installationId is required to authenticate as user');89 }90 const token = await this.createInstallationToken(installationId, gitOpts);91 const github = new Octokit({92 ...gitOpts,93 auth: `token ${token}`94 });95 return github;96 }97 async createInstallationToken(installation_id: number, gitOpts = this.gitOpts) {98 if (!installation_id) {99 throw new Error ('installation_id is required to authenticate as user');100 }101 const github = await this.authenticateAsApplication(gitOpts);102 const response = await github.apps.createInstallationToken({103 installation_id,104 });105 106 return response.data.token;107 }108 async authenticateAsAppAndInstallation(git: { repo: string, owner: string }, gitOpts = this.gitOpts) {109 const gitAppAuth = await this.authenticateAsApplication();110 111 const repoInstallation = await gitAppAuth.apps.getRepoInstallation(git);112 const installationId = repoInstallation.data.id;113 114 return this.authenticateAsInstallation(installationId);115 }116}117export default function GithubApplicationFactory(118 { applicationId , certificate, userToken }: Partial<GithubFactoryConfig> = { applicationId: GITHUB_APP_ID, certificate: GITHUB_APP_CERTIFICATE, userToken: GITHUB_USER_TOKEN },119 githubClientOptions: Octokit.Options = {},120) {121 const { baseUrl } = githubClientOptions;122 if (baseUrl) {123 const agent = new https.Agent({ rejectUnauthorized: false });124 githubClientOptions = { agent, baseUrl };125 }126 return new GithubFactory({ applicationId, certificate, userToken }, githubClientOptions);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fs = require('fs');2const { Octokit } = require("@octokit/core");3const { createAppAuth } = require("@octokit/auth-app");4const appId = 1234;5const privateKey = fs.readFileSync("./private-key.pem", "utf-8");6const installationId = 5678;7const octokit = new Octokit({8 auth: {9 },10});11octokit.request("GET /").then((response) => {12 console.log(response.data);13});14const { createAppAuth } = require("@octokit/auth-app");15const auth = createAppAuth({16});17const { createTokenAuth } = require("@octokit/auth-app");

Full Screen

Using AI Code Generation

copy

Full Screen

1const fs = require('fs');2const path = require('path');3const { createAppAuth } = require("@octokit/auth-app");4const { Octokit } = require("@octokit/rest");5const appID = process.env.GITHUB_APP_ID;6const privateKey = fs.readFileSync(path.join(__dirname, 'private-key.pem'), 'utf8');7const installationID = process.env.GITHUB_INSTALLATION_ID;8const auth = createAppAuth({9});10auth({ type: "app" }).then((auth) => {11 const octokit = new Octokit({12 });13 octokit.request("GET /installation/repositories").then((response) => {14 console.log(response.data);15 });16});

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 Best 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