Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"babel-polyfill": "6.16.0",
"babel-preset-es2015": "6.16.0",
"babel-preset-stage-0": "6.16.0",
"chalk": "^2.4.1",
"dotenv": "^5.0.1"
},
"scripts": {
Expand Down
44 changes: 44 additions & 0 deletions scripts/meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const chalk = require( 'chalk' );

const util = require( './util' );

const client = util.createClient();

// Gather app metadata
async function getMetadata() {
const response = await client.request( {
url: '/app',
} );
return response.data;
}

// Gather all installations from the API
async function getInstalls() {
const response = await client.request( {
url: '/app/installations',
} );
const { data } = response;

while ( client.hasNextPage( response ) ) {
response = await client.getNextPage( response );
data = data.concat( response.data )
}

return data;
}

const installPromise = getInstalls();

getMetadata().then( metadata => {
console.log( `Data for ${ chalk.green.bold( metadata.name ) } (ID ${ metadata.id })\n` );

installPromise
.then( installs => {
console.log( 'Installed on:' );
installs.forEach( install => console.log( chalk.grey( '• ' ) + install.account.login ) );
} )
.catch( err => {
console.log( 'Cannot fetch installs' );
console.log( err );
} );
} );
32 changes: 32 additions & 0 deletions scripts/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require( 'fs' );
const jwt = require( 'jsonwebtoken' );
const Octokit = require( '@octokit/rest' );

const id = process.env.APP_ID || 8936;
const certName = id === 8936 ? 'development.private-key.pem' : 'private-key.pem';
const cert = fs.readFileSync( certName, 'utf8' );

function createToken() {
const payload = {
exp: Math.floor( Date.now() / 1000 ) + 60, // JWT expiration time
iat: Math.floor( Date.now() / 1000 ), // Issued at time
iss: id // GitHub App ID
};

// Sign with RSA SHA256
return jwt.sign( payload, cert, { algorithm: 'RS256' } );
}

function createClient() {
const github = new Octokit();
github.authenticate( {
type: 'integration',
token: createToken(),
} );

return github;
}

module.exports = {
createClient,
};