Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cpp): handle download failures and don't use GitHub api to get latest releases #247

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
- [Python] Provide informative exception for trailing escapes in tables ([#241](https://github.com/cucumber/gherkin/pull/241))
- (i18n) Provide trailing space in Irish keywords ([#243](https://github.com/cucumber/gherkin/pull/243))
- (i18n) Tamil "And" and "But" translations should have single trailing space ([#243](https://github.com/cucumber/gherkin/pull/243))
- Intermittent failure of cpp test jobs in CI ([#217](https://github.com/cucumber/gherkin/issues/217))

## [28.0.0] - 2024-02-15
### Added
Expand Down
4 changes: 4 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_compile_definitions(_CRT_SECURE_NO_WARNINGS _SCL_SECURE_NO_WARNINGS)
endif()

find_package(nlohmann_json CONFIG REQUIRED)
find_package(cucumber_messages CONFIG REQUIRED)

Expand Down
83 changes: 40 additions & 43 deletions cpp/cmake/cmate
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ function(cmate_setgdir VAR VAL)
file(MAKE_DIRECTORY ${${VAR}})
endfunction()

function(cmate_sleep DURATION)
execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${DURATION})
endfunction()

function(cmate_load_version)
if(NOT "${CMATE_VERSION}" STREQUAL "")
return()
Expand Down Expand Up @@ -176,15 +180,37 @@ endfunction()
function(cmate_download URL FILE)
if(CMATE_SIMULATE)
cmate_msg("download ${URL} to ${FILE}")
else()
file(DOWNLOAD ${URL} ${FILE} STATUS ST)
return()
endif()

list(GET ST 0 RC)
set(WAIT_INTERVAL 5)
set(MAX_RETRIES 10)
set(RETRIES ${MAX_RETRIES})

if(RC)
cmate_die("download of ${URL} failed: ${ST}")
endif()
cmate_msg("downloading ${URL}")

while(1)
file(DOWNLOAD ${URL} ${FILE} STATUS ST)

list(GET ST 0 RC)

if(RC)
if(RETRIES GREATER 1)
math(EXPR RETRIES "${RETRIES} - 1")
math(EXPR ATTEMPT "${MAX_RETRIES} - ${RETRIES}")
cmate_msg(
"download of ${URL} failed"
" (attempt ${ATTEMPT} of ${MAX_RETRIES}"
", retrying in ${WAIT_INTERVAL}s)"
)
cmate_sleep(${WAIT_INTERVAL})
else()
cmate_die("download of ${URL} failed: ${ST}")
endif()
else()
break()
endif()
endwhile()
endfunction()

function(cmate_set_build_type RELEASE_FLAG_VAR)
Expand All @@ -202,41 +228,16 @@ function(cmate_set_build_type RELEASE_FLAG_VAR)
cmate_setg(CMATE_BUILD_DIR "${CMATE_BUILD_BASE_DIR}/${TDIR}")
endfunction()

function(cmate_github_get_latest REPO VAR RE)
set(URL "https://api.github.com/repos/${REPO}/releases/latest")
set(TDIR "${CMATE_TMP_DIR}/${REPO}")
set(INFO "${TDIR}/info.json")

if (NOT EXISTS ${INFO})
file(MAKE_DIRECTORY ${TDIR})
cmate_download(${URL} ${INFO})
endif()

file(READ ${INFO} VINFO)
cmate_json_get_array(${VINFO} "assets" ASSETS)

foreach(ASSET ${ASSETS})
string(
JSON
BDURL
ERROR_VARIABLE ERR
GET "${ASSET}" "browser_download_url"
)
function(cmate_github_get_latest REPO PKG VAR)
set(URL "https://github.com/${REPO}/releases/latest/download/${PKG}")

if(NOT ERR AND ${BDURL} MATCHES ${RE})
string(JSON FILE GET "${ASSET}" "name")
set(FILE "${CMATE_DL_DIR}/${FILE}")
set(FILE "${CMATE_DL_DIR}/${PKG}")

if (NOT EXISTS ${FILE})
cmate_download(${BDURL} ${FILE})
endif()

set(${VAR} ${FILE} PARENT_SCOPE)
break()
endif()
endforeach()
if (NOT EXISTS ${FILE})
cmate_download(${URL} ${FILE})
endif()

file(REMOVE_RECURSE ${TDIR})
set(${VAR} ${FILE} PARENT_SCOPE)
endfunction()

function(cmate_check_ninja VAR)
Expand All @@ -259,11 +260,7 @@ function(cmate_check_ninja VAR)
endif()

if(NOT EXISTS "${CMATE_ENV_BIN_DIR}/${NCMD}")
cmate_github_get_latest(
"ninja-build/ninja"
NZIP
"ninja-${NOS}.zip$"
)
cmate_github_get_latest("ninja-build/ninja" "ninja-${NOS}.zip" NZIP)

file(REMOVE_RECURSE ${TDIR})
file(ARCHIVE_EXTRACT INPUT ${NZIP} DESTINATION ${TDIR})
Expand Down