1<?php
2
3class GitLabApi {
4    const version = '0.1.1';
5
6    public $client;
7    public $data;
8
9    function __construct($dw_data) {
10        if ($dw_data['server'] == null) {
11            echo "GitLabApi: ERROR - NO SERVER DEFINED!";
12            die();
13        }
14        if($dw_data['token'] == null) {
15            echo "GitLabApi: ERROR - NO TOKEN GIVEN!";
16            die();
17        }
18        $this->dw_data = $dw_data;
19        $this->client = curl_init();
20    }
21
22    function getAPIUrl() { return $this->dw_data['server'] . '/api/v4/'; }
23
24    function closeClient() { curl_close($this->client); }
25
26    function gitlabRequest($url) {
27        curl_setopt($this->client, CURLOPT_URL, $url);
28        curl_setopt($this->client, CURLOPT_HTTPHEADER, array(
29            'PRIVATE-TOKEN: '.$this->dw_data['token']
30        ));
31        curl_setopt($this->client, CURLOPT_SSL_VERIFYHOST, '1');
32        curl_setopt($this->client, CURLOPT_SSL_VERIFYPEER, '1');
33        curl_setopt($this->client, CURLOPT_RETURNTRANSFER, true);
34        header('Content-Type: application/json');
35
36        $answer = curl_exec($this->client);
37        return json_decode($answer, true);
38    }
39
40    function getProject() {
41        $project_name = str_replace("/", "%2F", $this->dw_data['project-path']);
42        $url_request = $this->getAPIUrl().'projects/'.$project_name;
43        $project = $this->gitlabRequest($url_request);
44
45        if(empty($project)) { return; }
46
47        if (array_key_exists('message', $project)) {
48            echo "ERROR: ", $project['message'];
49            return;
50        }
51
52        return $project;
53    }
54
55    function getCommits($id) {
56        $url_request = $this->getAPIUrl().'projects/'.$id.'/repository/commits';
57        return $this->gitlabRequest($url_request);
58    }
59
60    function getIssues($id) {
61        $url_request = $this->getAPIUrl().'projects/'.$id.'/issues';
62        return $this->gitlabRequest($url_request);
63    }
64
65    function getMilestones($id) {
66        $url_request = $this->getAPIUrl().'projects/'.$id.'/milestones';
67        return $this->gitlabRequest($url_request);
68    }
69
70    function getPipelines($id) {
71        $url_request = $this->getAPIUrl().'projects/'.$id.'/pipelines';
72        return $this->gitlabRequest($url_request);
73    }
74}
75
76
77