From 21c11208673b9f520ee4684245998b4c86486cd7 Mon Sep 17 00:00:00 2001 From: qkqpttgf <45693631+qkqpttgf@users.noreply.github.com> Date: Wed, 1 Dec 2021 14:54:29 +0800 Subject: [PATCH] add setup=auth, check if another building --- platform/Vercel.php | 137 ++++++++++++++++++++++++++------------------ 1 file changed, 81 insertions(+), 56 deletions(-) diff --git a/platform/Vercel.php b/platform/Vercel.php index 6a65b2f..3c6a97a 100644 --- a/platform/Vercel.php +++ b/platform/Vercel.php @@ -151,7 +151,7 @@ function setConfig($arr, $disktag = '') } $envs = array_filter($envs, 'array_value_isnot_null'); //ksort($envs); - sortConfig($envs); + //sortConfig($envs); //error_log1(json_encode($arr, JSON_PRETTY_PRINT) . ' => tmp:' . json_encode($envs, JSON_PRETTY_PRINT)); //echo json_encode($arr, JSON_PRETTY_PRINT) . ' => tmp:' . json_encode($envs, JSON_PRETTY_PRINT); return setVercelConfig($envs, getConfig('HerokuappId'), getConfig('APIKey')); @@ -187,16 +187,24 @@ function install() $title = 'Error'; return message($html, $title, 400); } else { - /*$html = ''; - return message($html, $title, 201, 1);*/ - $data["dplId"] = $response['DplStatus']; - return output(json_encode($data), 201); + return message($html, $title, 201, 1); } } } @@ -208,10 +216,9 @@ language:
'; $html .= '
'; } - //if (getConfig('APIKey')=='') $html .= '
' . getconstStr('Create') . ' token
-
'; +
'; $html .= '

'; $html .= ' @@ -244,52 +251,7 @@ language:
'; alert(\'input Token\'); return false; } - t.style.display = "none"; - errordiv.innerHTML = "' . getconstStr('Wait') . '"; - var xhr = new XMLHttpRequest(); - xhr.open("POST", t.action); - xhr.onload = function(e) { - if (xhr.status==201) { - var res = JSON.parse(xhr.responseText); - getStatus(res.dplId, t.APIKey.value); - } else { - t.style.display = ""; - errordiv.innerHTML = xhr.status + "
" + xhr.responseText; - } - } - xhr.send("admin=" + t.admin.value + "&APIKey=" + t.APIKey.value); - - var x = ""; - var min = 0; - function getStatus(id, VercelToken) { - x += "."; - min++; - var xhr = new XMLHttpRequest(); - var url = "https://api.vercel.com/v11/now/deployments/" + id; - xhr.open("GET", url); - xhr.setRequestHeader("Authorization", "Bearer " + VercelToken); - xhr.onload = function(e) { - if (xhr.status==200) { - var deployStat = JSON.parse(xhr.responseText).readyState; - if (deployStat=="READY") { - x = ""; - min = 0; - errordiv.innerHTML = "Deploy done."; - location.href = "/"; - } else { - errordiv.innerHTML = deployStat + ", " + min + ".
' . getconstStr('Wait') . ' " + x; - if (deployStat!=="ERROR") setTimeout(function() { getStatus(id, VercelToken) }, 1000); - } - } else { - t.style.display = ""; - console.log(xhr.status); - console.log(xhr.responseText); - } - } - xhr.send(null); - } - - return false; + return true; } '; $title = getconstStr('SelectLanguage'); @@ -329,7 +291,7 @@ function copyFolder($from, $to) function setVercelConfig($envs, $appId, $token) { - //sortConfig($envs); cant view in vercel, not need sort. + sortConfig($envs); $outPath = '/tmp/code/'; $outPath_Api = $outPath . 'api/'; $coderoot = __DIR__; @@ -345,7 +307,8 @@ function setVercelConfig($envs, $appId, $token) function VercelUpdate($appId, $token, $sourcePath = "") { - $url = "https://api.vercel.com/v12/now/deployments"; + if (checkBuilding($appId, $token)) return '{"error":{"message":"Another building is in progress."}}'; + $url = "https://api.vercel.com/v13/deployments"; $header["Authorization"] = "Bearer " . $token; $header["Content-Type"] = "application/json"; $data["name"] = "OneManager"; @@ -367,6 +330,23 @@ function VercelUpdate($appId, $token, $sourcePath = "") return json_encode($result); } +function checkBuilding($projectId, $token) +{ + $r = 0; + $url = "https://api.vercel.com/v6/deployments/?projectId=" . $projectId; + $header["Authorization"] = "Bearer " . $token; + $header["Content-Type"] = "application/json"; + $response = curl("GET", $url, '', $header); + //echo json_encode($response, JSON_PRETTY_PRINT) . " ,res
"; + $result = json_decode($response["body"], true); + foreach ( $result['deployments'] as $deployment ) { + if ($deployment['state']!=="READY") $r++; + } + return $r; + //if ($r===0) return true; + //else return false; +} + function getEachFiles(&$file, $base, $path = "") { //if (substr($base, -1)=="/") $base = substr($base, 0, -1); @@ -453,7 +433,7 @@ function WaitFunction($deployid = '') { } $header["Authorization"] = "Bearer " . getConfig('APIKey'); $header["Content-Type"] = "application/json"; - $url = "https://api.vercel.com/v11/now/deployments/" . $deployid; + $url = "https://api.vercel.com/v11/deployments/" . $deployid; $response = curl("GET", $url, "", $header); if ($response['stat']==200) { $result = json_decode($response['body'], true); @@ -465,3 +445,48 @@ function WaitFunction($deployid = '') { return $response; } } + +function changeAuthKey() { + if ($_POST['APIKey']!='') { + $APIKey = $_POST['APIKey']; + $tmp['APIKey'] = $APIKey; + $response = setConfigResponse( setVercelConfig($tmp, getConfig('HerokuappId'), $APIKey) ); + if (api_error($response)) { + $html = api_error_msg($response); + $title = 'Error'; + return message($html, $title, 400); + } else { + $html = getconstStr('Success') . ' + '; + return message($html, $title, 201, 1); + } + } + $html = ' +
+ ' . getconstStr('Create') . ' token
+
+ +
+ '; + return message($html, 'Change platform Auth token or key', 200); +}