From 8a87514c0b0de492164a2450ee7f4c85dfbb33c2 Mon Sep 17 00:00:00 2001
From: Leonid Pospelov <leonidpospelov.dev@gmail.com>
Date: Sun, 28 Apr 2024 01:42:54 +0500
Subject: [PATCH] internal: support PR labels in gamemode (#1940)

---
 skymp5-functions-lib/CMakeLists.txt           | 23 +++++-
 .../download-and-build-with-prs.cmake         | 77 +++++++++++++++++++
 2 files changed, 97 insertions(+), 3 deletions(-)
 create mode 100644 skymp5-functions-lib/download-and-build-with-prs.cmake

diff --git a/skymp5-functions-lib/CMakeLists.txt b/skymp5-functions-lib/CMakeLists.txt
index 47624eeffa..f57c0c0e79 100644
--- a/skymp5-functions-lib/CMakeLists.txt
+++ b/skymp5-functions-lib/CMakeLists.txt
@@ -19,17 +19,34 @@ if(BUILD_GAMEMODE)
   set(SCRIPT_PATH "${CMAKE_CURRENT_LIST_DIR}/download-and-build.cmake")
   configure_file(${SCRIPT_PATH} ${CMAKE_BINARY_DIR}/download-and-build.cmake)
 
+  set(DEPLOY_BRANCH "$ENV{DEPLOY_BRANCH}")
+  set(AUTO_MERGE_REPO "Pospelove/auto-merge-action")
+  set(AUTO_MERGE_BRANCH "main")
+  set(AUTO_MERGE_REPO_URL "https://github.com/${AUTO_MERGE_REPO}.git")
+  set(GAMEMODE_REPO_URL "https://${GITHUB_TOKEN}:x-oauth-basic@github.com/${GAMEMODE_REPO_OWNER}/${GAMEMODE_REPO_NAME}.git")
+  set(ENV_INPUT_REPOSITORIES "[{\"owner\": \"Pospelove\", \"repo\": \"skymp5-gamemode\", \"labels\": [\"merge-to:${DEPLOY_BRANCH}\"], \"token\": \"${GITHUB_TOKEN}\"}]")
+
+  # Pass variables to the download script with PRS.
+  set(SCRIPT_PATH2 "${CMAKE_CURRENT_LIST_DIR}/download-and-build-with-prs.cmake")
+  configure_file(${SCRIPT_PATH2} ${CMAKE_BINARY_DIR}/download-and-build-with-prs.cmake ESCAPE_QUOTES)
+
+  if("$ENV{CI}" STREQUAL "true" AND NOT "$ENV{DEPLOY_BRANCH}" STREQUAL "")
+    set(source download-and-build-with-prs.cmake)
+  else()
+    set(source download-and-build.cmake)
+  endif()
+
   add_custom_target(skymp5-functions-lib ALL
-    SOURCES ${sources}
+    SOURCES ${source}
     COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/dist/server
     COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_CURRENT_BINARY_DIR}/${GAMEMODE_REPO_NAME}-${GAMEMODE_BRANCH}
     COMMAND ${CMAKE_COMMAND} -E env DOWNLOAD_NO_PROGRESS=1
-            ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/download-and-build.cmake
+            ${CMAKE_COMMAND} -P ${CMAKE_BINARY_DIR}/${source}
     COMMENT "Downloading & building gamemode repo..."
   )
 else()
   add_custom_target(skymp5-functions-lib ALL
-    SOURCES ${sources}
+    SOURCES ${source}
     COMMAND ${CMAKE_COMMAND} -E echo "Building skymp5-functions-lib is disabled. To enable it, set BUILD_GAMEMODE to ON."
     COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/dist/server
     COMMAND ${CMAKE_COMMAND} -E true > ${CMAKE_BINARY_DIR}/dist/server/gamemode.js
diff --git a/skymp5-functions-lib/download-and-build-with-prs.cmake b/skymp5-functions-lib/download-and-build-with-prs.cmake
new file mode 100644
index 0000000000..b75bf2d3f2
--- /dev/null
+++ b/skymp5-functions-lib/download-and-build-with-prs.cmake
@@ -0,0 +1,77 @@
+include(${CMAKE_SOURCE_DIR}/cmake/yarn.cmake)
+
+message(STATUS "Downloading gamemode sources")
+
+# Download the repository using Git
+
+# TODO: Fix CMakeLists.txt: GIT_RESULT/GIT_OUTPUT do not help since configure_file eliminates the variables
+file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/gamemode-sources)
+execute_process(
+    COMMAND git clone "${GAMEMODE_REPO_URL}" ${CMAKE_BINARY_DIR}/gamemode-sources
+    RESULT_VARIABLE GIT_RESULT
+    OUTPUT_VARIABLE GIT_OUTPUT
+)
+
+if(GIT_RESULT EQUAL 0)
+    message(STATUS "Downloaded gamemode sources")
+else()
+    message(FATAL_ERROR "Failed to download gamemode sources: ${GIT_OUTPUT}")
+endif()
+
+message(STATUS "Downloading Pospelove/auto-merge-action@main (dist/index.js)")
+
+message(STATUS "Downloading ${AUTO_MERGE_REPO}@${AUTO_MERGE_BRANCH} (dist/index.js)")
+
+file(REMOVE_RECURSE ${CMAKE_BINARY_DIR}/auto-merge-action)
+execute_process(
+    COMMAND git clone --branch "${AUTO_MERGE_BRANCH}" "${AUTO_MERGE_REPO_URL}" ${CMAKE_BINARY_DIR}/auto-merge-action
+    RESULT_VARIABLE GIT_AM_RESULT
+    OUTPUT_VARIABLE GIT_AM_OUTPUT
+)
+
+if(GIT_AM_RESULT EQUAL 0)
+    message(STATUS "Downloaded ${AUTO_MERGE_REPO}@${AUTO_MERGE_BRANCH} (dist/index.js)")
+else()
+    message(FATAL_ERROR "Failed to download ${AUTO_MERGE_REPO}@${AUTO_MERGE_BRANCH}: ${GIT_AM_OUTPUT}")
+endif()
+
+message(STATUS "Run Pospelove/auto-merge-action@main (dist/index.js)")
+
+# Execute the NodeJS script
+set(ENV{INPUT_REPOSITORIES} "${ENV_INPUT_REPOSITORIES}")
+set(ENV{INPUT_PATH} ${CMAKE_BINARY_DIR}/gamemode-sources)
+execute_process(
+    COMMAND node ${CMAKE_BINARY_DIR}/auto-merge-action/dist/index.js
+    RESULT_VARIABLE NODE_RESULT
+    #OUTPUT_VARIABLE NODE_OUTPUT
+)
+
+if(NODE_RESULT EQUAL 0)
+    message(STATUS "Successfully ran Pospelove/auto-merge-action@main")
+else()
+    message(FATAL_ERROR "Failed to run Pospelove/auto-merge-action@main: ${NODE_OUTPUT}")
+endif()
+
+message(STATUS "Installing yarn dependencies for gamemode")
+
+yarn_execute_command(
+    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/gamemode-sources
+    COMMAND install
+)
+
+message(STATUS "Installed yarn dependencies for gamemode")
+
+message(STATUS "Building gamemode.js")
+
+yarn_execute_command(
+    WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/gamemode-sources
+    COMMAND build
+)
+
+message(STATUS "Built gamemode.js")
+
+message(STATUS "Installing gamemode.js")
+
+file(COPY ${CMAKE_BINARY_DIR}/gamemode-sources/build/gamemode.js DESTINATION "${GAMEMODE_JS_DEST_DIR}")
+
+message(STATUS "Installed gamemode.js")