Skip to content

Commit

Permalink
Use GITHUB_TOKEN if set (#5070)
Browse files Browse the repository at this point in the history
* Use GITHUB_TOKEN if set

We have been having an error from Github saying that API rate limit
exceeded for IP. It hits the limit more often if your public IP is
shared by many other collegues.

{"message":"API rate limit exceeded for 216.228.112.22. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)","documentation_url":"https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"}

The suggested solution from GitHub is to use gh.exe tool with "auth
login" arguments. It will store a token and allow us to use API more
than the limit set for IP without token.

However, our cmake build doesn't use gh.exe and API is called via REST.
This commit adds an extra header to the HTTP request with the
infomration of the github token if the value is.

Usage: cmake.exe --preset vs2019 -DSLANG_GITHUB_TOKEN=your_token_here

* Adding a warning message to use LANG_GITHUB_TOKEN

---------

Co-authored-by: Yong He <[email protected]>
  • Loading branch information
jkwak-work and csyonghe authored Sep 19, 2024
1 parent b7617d2 commit c6b702c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ option(SLANG_ENABLE_TESTS "Enable test targets, some tests may require SLANG_ENA
option(SLANG_ENABLE_EXAMPLES "Enable example targets, requires SLANG_ENABLE_GFX" ON)
option(SLANG_ENABLE_REPLAYER "Enable slang-replay tool" ON)

option(SLANG_GITHUB_TOKEN "Use a given token value for accessing Github REST API" "")

enum_option(
SLANG_LIB_TYPE
# Default
Expand Down Expand Up @@ -136,7 +138,7 @@ enum_option(
if(SLANG_SLANG_LLVM_FLAVOR MATCHES FETCH_BINARY)
# If the user didn't specify a URL, find the best one now
if(NOT SLANG_SLANG_LLVM_BINARY_URL)
get_best_slang_binary_release_url(url)
get_best_slang_binary_release_url("${SLANG_GITHUB_TOKEN}" url)
if(NOT DEFINED url)
message(FATAL_ERROR "Unable to find binary release for slang-llvm, please set a different SLANG_SLANG_LLVM_FLAVOR or set SLANG_SLANG_LLVM_BINARY_URL manually")
endif()
Expand Down
28 changes: 23 additions & 5 deletions cmake/GitHubRelease.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(check_release_and_get_latest owner repo version os arch out_var)
function(check_release_and_get_latest owner repo version os arch github_token out_var)
# Construct the URL for the specified version's release API endpoint
set(version_url "https://api.github.com/repos/${owner}/${repo}/releases/tags/v${version}")

Expand All @@ -17,8 +17,22 @@ function(check_release_and_get_latest owner repo version os arch out_var)
set(${found_var} "${found}" PARENT_SCOPE)
endfunction()

# Download the specified release info from GitHub
file(DOWNLOAD "${version_url}" "${json_output_file}" STATUS download_statuses)
# Prepare download arguments
set(download_args
"${version_url}"
"${json_output_file}"
STATUS download_statuses
)

if(github_token)
# Add authorization header if token is provided
list(APPEND download_args HTTPHEADER "Authorization: token ${github_token}")
endif()

# Perform the download
file(DOWNLOAD ${download_args})

# Check if the downloading was successful
list(GET download_statuses 0 status_code)
if(status_code EQUAL 0)
file(READ "${json_output_file}" json_content)
Expand All @@ -34,6 +48,10 @@ function(check_release_and_get_latest owner repo version os arch out_var)
message(WARNING "Failed to find ${desired_zip} in release assets for ${version} from ${version_url}\nFalling back to latest version if it differs")
else()
message(WARNING "Failed to download release info for version ${version} from ${version_url}\nFalling back to latest version if it differs")

if(status_code EQUAL 22)
message(WARNING "If API rate limit is exceeded, Github allows a higher limit when you use token. Try a cmake option -DSLANG_GITHUB_TOKEN=your_token_here")
endif()
endif()


Expand Down Expand Up @@ -69,7 +87,7 @@ function(check_release_and_get_latest owner repo version os arch out_var)
endif()
endfunction()

function(get_best_slang_binary_release_url out_var)
function(get_best_slang_binary_release_url github_token out_var)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
set(arch "x86_64")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64|arm64")
Expand All @@ -93,7 +111,7 @@ function(get_best_slang_binary_release_url out_var)
set(owner "shader-slang")
set(repo "slang")

check_release_and_get_latest(${owner} ${repo} ${SLANG_VERSION_NUMERIC} ${os} ${arch} release_version)
check_release_and_get_latest(${owner} ${repo} ${SLANG_VERSION_NUMERIC} ${os} ${arch} "${github_token}" release_version)
if(DEFINED release_version)
set(${out_var} "https://github.com/${owner}/${repo}/releases/download/v${release_version}/slang-${release_version}-${os}-${arch}.zip" PARENT_SCOPE)
endif()
Expand Down

0 comments on commit c6b702c

Please sign in to comment.