Пример #1
0
 /**
  * Get a list of tag items for a project from GitLab
  * @return array of \Git-Deployer\Objects\Tag
  */
 public function getTags(\GitDeployer\Objects\Project $project, $url = 'projects/:id/repository/tags')
 {
     if (strlen($this->privateKey) > 0) {
         $client = $this->createClient($this->privateKey);
         try {
             $url = str_replace(':id', $project->id(), $url);
             $response = $client->get($url);
             $tagData = json_decode($response->getBody());
             $tagData = array_map(function ($t) {
                 $tag = new \GitDeployer\Objects\Tag();
                 $tag->name($t->name())->commit($h->commit->id);
                 return $tag;
             }, $tagData);
             if ($response->hasHeader('link')) {
                 // -> We have more than one page, extract the next
                 // page link and pass it to getProjects() again
                 $link = $response->getHeader('link')[0];
                 preg_match('/<.*projects\\/.*\\/repository/tags(.*)>; rel="next"/', $link, $matches);
                 if (isset($matches[1])) {
                     $cleanLink = 'projects/:id/repository/tags' . $matches[1];
                     $moreTags = $this->getTags($project, $cleanLink);
                     if (is_array($moreTags)) {
                         $tagData = array_merge($tagData, $moreTags);
                     }
                 }
             }
             return $tagData;
         } catch (\GuzzleHttp\Exception\ClientException $e) {
             if ($e->getResponse()->getStatusCode() == 401 || $e->getResponse()->getStatusCode() == 403) {
                 return $this->interactiveLogin();
             } else {
                 throw new \Exception($e->getResponse()->getStatusCode() . ' ' . $e->getResponse()->getReasonPhrase());
             }
         }
     } else {
         throw new \Exception('You must log-in to a service first!');
     }
 }