[英]Finding a specific GitLab tag from PHP
我們有一個JIRA實例,我們在Laravel中構建的自定義PHP應用程序來自每個問題,看看是否存在特定的分支或標記:
chdir($path . $repo);
exec("git rev-parse --verify ".$branch, $branch_dump, $return_var);
if ($return_var == 0) {
return true;
} else {
return false;
}
但是,我們已經將所有git項目遷移到GitLab,並且該方法不再有效,因為您需要root才能進入GitLab的repo數據目錄。
我們查看了GitLab的API,發現我們可以這樣做:
http://gitlab/api/v3/projects/10/repository/commits/OUR-TAG-HERE?private_token=XXX
但是,這需要我們指定一個任意的GitLab項目ID(在這種情況下為10)因此是不可預測的,因此我們無法像以前那樣以編程方式執行每個JIRA API返回的搜索。 如果我們只使用項目名稱搜索標簽,這種方法就可以工作,但我找不到辦法做到這一點。
這是應用程序工作方式的俯視:
我們簡要地考慮在我們的JIRA問題上添加另一個自定義字段,我們將填寫GitLab的項目ID,但是我們有數百個問題,這是一個不優雅的解決方案,實際上只是另一個潛在的失敗點,更不用說額外的維護了。
有任何想法嗎?
我發現此問題的最佳解決方案是使用API獲取項目列表並使用該列表來配對名稱和ID。
例如,此代碼將輸出所有項目的標記名稱:
//Get Projects list via API
$header = array("PRIVATE-TOKEN: <YOUR_TOKEN>");
$ch = curl_init("https://<YOUR_GITLAB_DOMAIN>/api/v3/projects/");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
//Parse returned list to an array
$projectsArray= json_decode($result, true);
//Loop over the array of projects accessing the list of tags via the API
foreach ($projectsArray as $project) {
echo $project["name"] . " Tags:<br>";
$tagURL = "https://<YOUR_GITLAB_DOMAIN>/api/v3/projects/" . $project["id"] . "/repository/tags";
$ch2 = curl_init($tagURL);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
$result2 = curl_exec($ch2);
curl_close($ch2);
$tagsArray= json_decode($result2, true);
foreach ($tagsArray as $tag) {echo $tag["name"] . "<br>";}
echo "<br>";
}
由於GitLab API仍然需要任意項目ID來完成此功能,因此我們完全廢棄了API。 相反,我們現在只是簡單地編寫HTTP響應代碼。 這是我們查看問題是否有標記的方法之一:
public function HasTag($projectName, $nameSpace, $tagName)
{
$url=$this->gitLabUrl.'/'.$nameSpace.'/'.$projectName.'/tags/'.$tagName;
$ch = curl_init(); // Initiate curl
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Will return the response, if false print the response
curl_setopt($ch, CURLOPT_URL, $url); // Set the url
curl_exec($ch); // Execute
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] == 200) {
return true;
} else {
return false;
}
}
這是我們檢查分支的方法:
public function HasBranch($projectName, $nameSpace, $branchName)
{
$url=$this->gitLabUrl.'/'.$nameSpace.'/'.$projectName.'/tree/'.$branchName;
$ch = curl_init(); // Initiate curl
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Will return the response, if false print the response
curl_setopt($ch, CURLOPT_URL, $url); // Set the url
curl_exec($ch); // Execute
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] == 200) {
return true;
} else {
return false;
}
}
正如您所看到的,這非常簡單和hacky,但它適用於我們的實現,因為所訪問的項目都不是私有的(我們的GitLab實例純粹是內部的)。
希望將來GitLab將從其API中刪除ID要求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.