-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: use private key to get pat token for bot
- Loading branch information
Showing
4 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import jwt from "jsonwebtoken"; | ||
|
||
// Usage: node privateKeyToPAT.js <base 64 encoded contents of the private key.pem file> | ||
const privateKeyBase64 = process.argv[2]; | ||
const appId = "809549"; | ||
const installationId = "46654931"; | ||
|
||
// Step 1: Generate a JWT | ||
const privateKey = Buffer.from(privateKeyBase64, "base64").toString(); | ||
const payload = { | ||
iat: Math.floor(Date.now() / 1000), | ||
exp: Math.floor(Date.now() / 1000) + 600, // 10 minutes | ||
iss: appId, | ||
}; | ||
const token = jwt.sign(payload, privateKey, { algorithm: "RS256" }); | ||
|
||
// Step 2: Exchange JWT for an Access Token (PAT) | ||
const githubApiUrl = `https://api.github.com/app/installations/${installationId}/access_tokens`; | ||
|
||
fetch(githubApiUrl, { | ||
method : "POST", | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
Accept : "application/vnd.github.v3+json", | ||
}, | ||
}) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error(`Failed to retrieve access token: ${response.status} ${response.statusText}`); | ||
} | ||
return response.json(); | ||
}) | ||
.then(data => { | ||
console.log(`PAT=${data.token}`); | ||
}) | ||
.catch(error => { | ||
console.error("Error:", error); | ||
process.exit(1); | ||
}); |