Skip to content

Commit ed12302

Browse files
zylaclaude
andcommitted
Make GitHub token refresh threshold configurable
The hardcoded 300-second (5-minute) refresh threshold was causing the github-token-refresh test to fail. With a 2-second token lifetime, fresh tokens were immediately considered "expiring" (2s < 300s threshold), triggering unnecessary refreshes on every getClient() call. Changes: - Add githubTokenRefreshThresholdSeconds to Settings (default: 300) - Read TASKRUNNER_GITHUB_TOKEN_REFRESH_THRESHOLD_SECONDS env var - Use configurable threshold in getClient and loadOrRefreshClient - Set threshold to 1 second in github-token-refresh test This allows the test to verify proper token refresh behavior: - Fresh tokens (2s remaining >= 1s threshold) are reused - Expired tokens trigger refresh - Test now passes with 2 token requests instead of 4 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 7bd8cda commit ed12302

File tree

4 files changed

+8
-2
lines changed

4 files changed

+8
-2
lines changed

src/App.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ getSettings = do
6363
primeCacheMode <- (==Just "1") <$> lookupEnv "TASKRUNNER_PRIME_CACHE_MODE"
6464
mainBranch <- map toText <$> lookupEnv "TASKRUNNER_MAIN_BRANCH"
6565
quietMode <- (==Just "1") <$> lookupEnv "TASKRUNNER_QUIET"
66+
githubTokenRefreshThresholdSeconds <- maybe 300 read <$> lookupEnv "TASKRUNNER_GITHUB_TOKEN_REFRESH_THRESHOLD_SECONDS"
6667
pure Settings
6768
{ stateDirectory
6869
, rootDirectory
@@ -78,6 +79,7 @@ getSettings = do
7879
, mainBranch
7980
, force = False
8081
, quietMode
82+
, githubTokenRefreshThresholdSeconds
8183
}
8284

8385
main :: IO ()

src/CommitStatus.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ getClient appState = do
7878
Just client -> do
7979
-- Fast path: check if cached token is still valid
8080
now <- getCurrentTime
81-
if diffUTCTime client.expiresAt now >= 300
81+
let threshold = fromIntegral appState.settings.githubTokenRefreshThresholdSeconds
82+
if diffUTCTime client.expiresAt now >= threshold
8283
then pure client
8384
else do
8485
-- Token expiring, need to refresh
@@ -92,6 +93,7 @@ loadOrRefreshClient :: AppState -> IO GithubClient
9293
loadOrRefreshClient appState = do
9394
let cacheFile = credentialsCacheFile appState.settings
9495
let lockFile = cacheFile <> ".lock"
96+
let threshold = fromIntegral appState.settings.githubTokenRefreshThresholdSeconds
9597

9698
client <- withFileLock lockFile Exclusive \_ -> do
9799
-- Under EXCLUSIVE lock: read, check, refresh if needed
@@ -100,7 +102,7 @@ loadOrRefreshClient appState = do
100102
now <- getCurrentTime
101103
case mCached of
102104
Just (cachedToken, expiresAt)
103-
| diffUTCTime expiresAt now >= 300 -> do
105+
| diffUTCTime expiresAt now >= threshold -> do
104106
-- Valid cached token
105107
logDebug appState "Using cached GitHub token from file"
106108
buildClientWithToken appState cachedToken expiresAt

src/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ data Settings = Settings
2121
, mainBranch :: Maybe Text
2222
, force :: Bool
2323
, quietMode :: Bool
24+
, githubTokenRefreshThresholdSeconds :: Int
2425
} deriving (Show)
2526

2627
type JobName = String

test/t/slow/github-token-refresh.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# github token lifetime 2
55

66
export TASKRUNNER_ENABLE_COMMIT_STATUS=1
7+
export TASKRUNNER_GITHUB_TOKEN_REFRESH_THRESHOLD_SECONDS=1
78

89
git init -q
910
git commit --allow-empty -q -m "Initial commit"

0 commit comments

Comments
 (0)