Skip to content

Commit 939953a

Browse files
committed
Hopeful Fix Commit
1 parent fe8176c commit 939953a

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

website-ts/scripts/nfl-scraper/chatgpt.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,14 @@ function loadRefreshToken(): string | null {
6060
return OPENAI_REFRESH_TOKEN || null
6161
}
6262

63-
// Save refresh token to file for reuse
63+
// Save refresh token to file for reuse (Secured for GitHub Actions)
6464
function saveRefreshToken(refreshToken: string): void {
65+
// Prevent writing to disk in GitHub Actions to avoid 'git add -A' committing the token
66+
if (process.env.GITHUB_ACTIONS === 'true') {
67+
console.log(' 🛡️ GitHub Actions detected: Skipping disk write for token (kept securely in memory).')
68+
return
69+
}
70+
6571
try {
6672
fs.writeFileSync(TOKEN_FILE, JSON.stringify({ refreshToken }, null, 2))
6773
} catch (error) {
@@ -327,3 +333,17 @@ Write the full article now:`
327333
console.log(` ✨ AI generated using ${MODEL_NAME} (ChatGPT OAuth)`)
328334
return generatedText.trim()
329335
}
336+
337+
// ============================================================================
338+
// Token Export for GitHub Actions CLI Update
339+
// ============================================================================
340+
341+
export function getLatestRefreshToken(): string | null {
342+
// Return the newly generated token if a refresh happened this run
343+
if (cachedToken && cachedToken.refreshToken) {
344+
return cachedToken.refreshToken
345+
}
346+
347+
// Otherwise, return the original token we started with
348+
return loadRefreshToken()
349+
}

website-ts/scripts/nfl-scraper/runAll.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const __filename = fileURLToPath(import.meta.url)
3434
const __dirname = path.dirname(__filename)
3535
const WEBSITE_ROOT = path.resolve(__dirname, '../..')
3636
const repoName = process.env.GITHUB_REPOSITORY;
37+
const { getLatestRefreshToken } = await import('./chatgpt');
38+
const newRefreshToken = getLatestRefreshToken();
3739

3840
// Resolve paths relative to website root
3941
function resolvePath(relativePath: string): string {
@@ -679,22 +681,30 @@ async function main(): Promise<void> {
679681
process.exit(1)
680682
}
681683

684+
// --- UPDATED TOKEN LOGIC ---
682685
if (repoName && process.env.SECRET_UPDATER) {
683-
console.log("Securely updating OpenAI refresh token...");
684-
685-
execSync(`gh secret set OPENAI_REFRESH_TOKEN --repo ${repoName}`, {
686-
input: process.env.OPENAI_REFRESH_TOKEN, // Pipes the token securely from memory
687-
env: {
688-
...process.env,
689-
GH_TOKEN: process.env.SECRET_UPDATER // Authorizes the CLI using the PAT
690-
}
691-
});
692-
693-
console.log("Token updated successfully.");
694-
}
695-
686+
// You will need to export this getter function from your chatgpt.ts file
687+
const { getLatestRefreshToken } = await import('./chatgpt');
688+
const newRefreshToken = getLatestRefreshToken();
689+
690+
// Only update if we actually have a new token that differs from the starting one
691+
if (newRefreshToken && newRefreshToken !== process.env.OPENAI_REFRESH_TOKEN) {
692+
console.log("Securely updating OpenAI refresh token...");
693+
694+
execSync(`gh secret set OPENAI_REFRESH_TOKEN --repo ${repoName}`, {
695+
input: newRefreshToken, // Pipes the NEW token securely from memory
696+
env: {
697+
...process.env,
698+
GH_TOKEN: process.env.SECRET_UPDATER
699+
}
700+
});
701+
702+
console.log("Token updated successfully.");
703+
} else {
704+
console.log("No new refresh token generated this run. Skipping secret update.");
705+
}
706+
}
696707
}
697708

698709

699-
700710
main()

0 commit comments

Comments
 (0)