Matched functionality of the CLI with the Bash predecessor.
This commit is contained in:
49
dist/check-token.js
vendored
Normal file → Executable file
49
dist/check-token.js
vendored
Normal file → Executable file
@@ -1,2 +1,49 @@
|
||||
#!/usr/bin/env node
|
||||
"use strict";var n=require("util");async function i(t,e){let s=`https://api.github.com/repos/${t}/releases/latest`,r={Accept:"application/vnd.github.v3+json","User-Agent":"setup-github-release-action"};e&&(r.Authorization=`token ${e}`);let o=await fetch(s,{headers:r});if(!o.ok){let a=await o.text();throw new Error(`Failed to fetch latest release for ${t}: ${o.statusText}. ${a}`)}return await o.json()}async function c(){let{positionals:t}=(0,n.parseArgs)({allowPositionals:!0}),e=t[0]||process.env.GITHUB_TOKEN;e||(console.error("Error: No GitHub token provided as an argument or found in GITHUB_TOKEN environment variable."),process.exit(1));try{console.log("Verifying GitHub token..."),await i("actions/checkout",e),console.log("\x1B[32mSuccess: The provided GitHub token is valid and has sufficient permissions to access public repositories.\x1B[0m")}catch(s){console.error("\x1B[31mError: GitHub token verification failed.\x1B[0m"),console.error(`Reason: ${s.message}`),process.exit(1)}}c();
|
||||
"use strict";
|
||||
|
||||
// src/check-token.ts
|
||||
var import_util = require("util");
|
||||
|
||||
// src/core/downloader.ts
|
||||
function getGithubApiHeaders(token) {
|
||||
const headers = {
|
||||
"Accept": "application/vnd.github.v3+json",
|
||||
"User-Agent": "setup-github-release-action"
|
||||
};
|
||||
if (token) {
|
||||
headers["Authorization"] = `token ${token}`;
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
async function fetchLatestRelease(repository, token) {
|
||||
const url = `https://api.github.com/repos/${repository}/releases/latest`;
|
||||
const headers = getGithubApiHeaders(token);
|
||||
const response = await fetch(url, { headers });
|
||||
if (!response.ok) {
|
||||
const errorBody = await response.text();
|
||||
throw new Error(`Failed to fetch latest release for ${repository}: ${response.statusText}. ${errorBody}`);
|
||||
}
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
// src/check-token.ts
|
||||
async function run() {
|
||||
const { positionals } = (0, import_util.parseArgs)({
|
||||
allowPositionals: true
|
||||
});
|
||||
const token = positionals[0] || process.env.GITHUB_TOKEN;
|
||||
if (!token) {
|
||||
console.error("Error: No GitHub token provided as an argument or found in GITHUB_TOKEN environment variable.");
|
||||
process.exit(1);
|
||||
}
|
||||
try {
|
||||
console.log("Verifying GitHub token...");
|
||||
await fetchLatestRelease("actions/checkout", token);
|
||||
console.log("\x1B[32mSuccess: The provided GitHub token is valid and has sufficient permissions to access public repositories.\x1B[0m");
|
||||
} catch (error) {
|
||||
console.error("\x1B[31mError: GitHub token verification failed.\x1B[0m");
|
||||
console.error(`Reason: ${error.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
run();
|
||||
|
||||
Reference in New Issue
Block a user