spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / .devcontainer / cache-github-api.sh
109 lines · 3.2 KBRaw
1#!/bin/bash
2set -e
3
4# Script to cache GitHub API data
5# Used to prevent rate limiting during container builds
6
7# Configuration
8# Store cache in the home directory
9CACHE_DIR="${HOME}/.github-meta-cache"
10CACHE_FILE="${CACHE_DIR}/meta.json"
11TIMESTAMP_FILE="${CACHE_DIR}/meta-timestamp.txt"
12MAX_AGE_SECONDS=3600 # Cache expires after 1 hour
13
14# Create cache directory if it doesn't exist
15mkdir -p "${CACHE_DIR}"
16
17# Function to get current timestamp
18get_timestamp() {
19 date +%s
20}
21
22# Function to check if cache is valid
23is_cache_valid() {
24 if [[ ! -f "${CACHE_FILE}" || ! -f "${TIMESTAMP_FILE}" ]]; then
25 return 1
26 fi
27
28 local cache_time=$(cat "${TIMESTAMP_FILE}")
29 local current_time=$(get_timestamp)
30 local age=$((current_time - cache_time))
31
32 if [[ ${age} -gt ${MAX_AGE_SECONDS} ]]; then
33 echo "Cache is expired (${age} seconds old)"
34 return 1
35 fi
36
37 echo "Using cached GitHub API data (${age} seconds old)"
38 return 0
39}
40
41# Function to fetch data using authenticated gh cli
42fetch_with_gh() {
43 echo "Attempting to fetch GitHub API data using authenticated gh CLI..."
44 if gh auth status &>/dev/null; then
45 gh api meta > "${CACHE_FILE}" &&
46 get_timestamp > "${TIMESTAMP_FILE}" &&
47 echo "Successfully fetched and cached GitHub API data using gh CLI"
48 return $?
49 else
50 echo "gh CLI not authenticated"
51 return 1
52 fi
53}
54
55# Function to fetch data using curl
56fetch_with_curl() {
57 echo "Attempting to fetch GitHub API data using curl..."
58 # First try with GITHUB_TOKEN if available
59 if [[ -n "${GITHUB_TOKEN}" ]]; then
60 echo "Using GITHUB_TOKEN for authentication"
61 curl -s -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/meta > "${CACHE_FILE}" &&
62 get_timestamp > "${TIMESTAMP_FILE}" &&
63 echo "Successfully fetched and cached GitHub API data using curl with token"
64 return $?
65 else
66 # Fall back to unauthenticated request
67 echo "No GITHUB_TOKEN found, making unauthenticated request (may be rate limited)"
68 curl -s https://api.github.com/meta > "${CACHE_FILE}"
69
70 # Check if the response indicates rate limiting
71 if grep -q "API rate limit exceeded" "${CACHE_FILE}"; then
72 echo "Rate limit exceeded for unauthenticated request"
73 return 1
74 else
75 get_timestamp > "${TIMESTAMP_FILE}"
76 echo "Successfully fetched and cached GitHub API data using curl without auth"
77 return 0
78 fi
79 fi
80}
81
82# Main logic
83if is_cache_valid; then
84 echo "Using existing cache from $(cat ${TIMESTAMP_FILE})"
85 exit 0
86fi
87
88# Try with gh CLI first
89if ! fetch_with_gh; then
90 # Fall back to curl
91 if ! fetch_with_curl; then
92 # Both methods failed, check if we have an existing cache file
93 if [[ -f "${CACHE_FILE}" ]]; then
94 echo "Warning: Failed to update cache, using existing cached data (which may be expired)"
95 exit 0
96 else
97 echo "Error: Failed to fetch GitHub API data and no cache exists"
98 exit 1
99 fi
100 fi
101fi
102
103# Display a summary of the cached data
104echo "GitHub API meta data cached successfully. Summary:"
105jq -r '.domains.actions | length' "${CACHE_FILE}" > /dev/null 2>&1 &&
106 echo "- Actions domains: $(jq -r '.domains.actions | length' "${CACHE_FILE}")" ||
107 echo "- Could not parse actions domains from cache file"
108
109exit 0