Skip to content

Commit

Permalink
chore: add js to generate token
Browse files Browse the repository at this point in the history
  • Loading branch information
koenmetsu authored Oct 11, 2023
1 parent 4759196 commit 02233bd
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/generate-token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const axios = require('axios');
const jwt = require('jsonwebtoken');
const fs = require('fs');

const APP_ID = process.env.APP_ID;
const PRIVATE_KEY = process.env.PRIVATE_KEY.replace(/\\n/g, '\n');

// Create JWT
const jwtToken = jwt.sign({}, PRIVATE_KEY, {
algorithm: 'RS256',
expiresIn: '10m', // 10 minutes
issuer: APP_ID
});

// Fetch the installation ID dynamically
axios.get(`https://api.github.com/app/installations`, {
headers: {
Authorization: `Bearer ${jwtToken}`,
Accept: 'application/vnd.github.v3+json',
}
}).then(response => {
const installationId = response.data[0].id; // Assuming only one installation. Adjust if needed.

// Now, fetch installation token
return axios.post(`https://api.github.com/app/installations/${installationId}/access_tokens`, {}, {
headers: {
Authorization: `Bearer ${jwtToken}`,
Accept: 'application/vnd.github.v3+json',
}
});

}).then(response => {
const installationToken = response.data.token;
console.log(`::set-output name=installationToken::${installationToken}`);
}).catch(error => {
console.error('Error fetching installation token:', error);
});

0 comments on commit 02233bd

Please sign in to comment.