Finding a specific GitLab tag from PHP

- 1 answer

Ad

We have a JIRA instance that our custom PHP app built in Laravel pulls from and for each issue looks to see if a specific branch or tag exists:

chdir($path . $repo);
exec("git rev-parse --verify ".$branch, $branch_dump, $return_var);
if ($return_var == 0) {
  return true;
} else {
  return false;
}

However, we have migrated all of our git projects to GitLab and that method no longer works, since you need root to get into GitLab's repo data directory.

We looked at GitLab's API and found that we could do this:

http://gitlab/api/v3/projects/10/repository/commits/OUR-TAG-HERE?private_token=XXX

However this requires us to specify an arbitrary GitLab project ID (10 in this case) and therefore isn't predictable, so we can't programmatically execute the search for each JIRA API return like we did before. This method would work if we could simply search for tags using the project name only, but I can't find a way to do that.

Here's an overlook at the way the app works:

  • JIRA contains all issues we want
  • Each issue contains several custom fields we use to search our git repos with, generically they are "Repo Name" and "Tag Name"
  • Our Laravel app connects to JIRA's api and harvests all issues into an array we use to build a table listing information about each issue
  • The two custom fields "Repo Name" and "Tag Name" are matched against our git repositories to determine which of several options to provide the end user (clone tag, create tag if repo exists but no tag exists, or none if neither)

We briefly considered adding another custom field to our JIRA issues which we would fill with GitLab's project ID, but we have hundreds of issues and it is an inelegant solution that really only acts as another potential point of failure, to say nothing of the extra maintenance.

Any ideas?

Ad

Answer

Ad

The best solution I found to this issue was to use the API to get the list of projects and use that list to pair name and ID.

For example, this code will output the tag names for all your projects:

//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>";
}
Ad
source: stackoverflow.com
Ad