From ae757bef84bad415f2a5b952e8e76dedaa0f63dc Mon Sep 17 00:00:00 2001 From: qkqpttgf <45693631+qkqpttgf@users.noreply.github.com> Date: Wed, 1 Dec 2021 14:55:41 +0800 Subject: [PATCH] add setup=auth, check if another building --- platform/Vercel_env.php | 136 +++++++++++++++++++++++----------------- 1 file changed, 79 insertions(+), 57 deletions(-) diff --git a/platform/Vercel_env.php b/platform/Vercel_env.php index 4ebd486..8b1888d 100644 --- a/platform/Vercel_env.php +++ b/platform/Vercel_env.php @@ -163,9 +163,6 @@ function install() //$tmp['language'] = $_POST['language']; $tmp['timezone'] = $_COOKIE['timezone']; $APIKey = $_POST['APIKey']; - //if ($APIKey=='') { - // $APIKey = getConfig('APIKey'); - //} $tmp['APIKey'] = $APIKey; $token = $APIKey; @@ -184,16 +181,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); } } } @@ -205,10 +210,9 @@ language:
'; $html .= '
'; } - //if (getConfig('APIKey')=='') $html .= '
' . getconstStr('Create') . ' token
-
'; +
'; $html .= '

'; $html .= ' @@ -241,52 +245,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'); @@ -335,7 +294,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"; @@ -357,6 +317,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); @@ -438,7 +415,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); @@ -450,3 +427,48 @@ function WaitFunction($deployid) { return $response; } } + +function changeAuthKey() { + if ($_POST['APIKey']!='') { + $APIKey = $_POST['APIKey']; + $tmp['APIKey'] = $APIKey; + $response = json_decode(setVercelConfig($tmp, getConfig('HerokuappId'), $APIKey), true); + 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); +}