REST API Tutorial

How To Create Gitlab Project Using GIT API And Lumen

Summary of learning

This tutorial help to create gitlab project using git api v4 and lumen micro rest framework. This rest tutorial is help to create project under the specific groups using api.The gitlab group can be contains number of projects.We just add one new project into that project using API.

This tutorial help to create gitlab project using git api v4 and lumen micro rest framework. This rest tutorial is help to create project under the specific groups using api.The gitlab group can be contains number of projects.We just add one new project into that project using API.

GITLAB is providing Rest interface to create project, add groups,create issue etc.It gives us the possibility to manage all gitlab work using HTTP requests.I am using one them is '/project' call to create gitlab project using git api v4.

Create Project Using GIT API V4

We will create POST call to send data to gitlab rest API host server. The project post call will required project_name, group_id and some optional parameters.

Create Route Entry Into Routes.php File

Step 1: Create route entry into routes.php file.

$app->post('create_gitlab_project', 'GitlabController@createGitlabProject');

Create Handler Method Into Controller File

Step 2: Created handler method into GitController.php file.

public function createGitlabProject(Request $request) {
     $parameters = array();
     $array_data = $request->json()->all();
     
      if(isset($array_data['gitlab_project_id']) && !empty($array_data['gitlab_project_id']) && !empty($array_data['gitlab_project_name'])) {
        try {
            $parameters['namespace_id'] = $array_data['gitlab_project_id'];
            $parameters['name'] = $array_data['gitlab_project_name'];
            $parameters['description'] = isset($array_data['gitlab_project_desc']) ? $array_data['gitlab_project_desc'] : '';
            $parameters['visibility'] = isset($array_data['gitlab_project_visibility']) ? $array_data['gitlab_project_visibility'] : 'public';
            $response = $this->gitlab->createGitlabProject($parameters);
         
            return json_encode($response);
         } catch(Exception $ex) {
            return 'Unable to create gitlab project';
         } 
      } else {
         return 'Missing! gitlab params to create repo';
      }
   }

Create Service Method Into Service File

Step 3: Created service method into GitService.php file.

var $gitlabAPI = "https://gitlab-hostname/api/v4/";

   private function _client($endpoint) {

   	  $token = 'your gitlab token';
      $client = new Client([
          'base_uri' => $endpoint,
          'timeout' => 300,
		  'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json", "PRIVATE-TOKEN" => $token],
          'http_errors' => false,
          'verify' => false
      ]);
      return $client;
   }
   /**
    * Method to create project into gitlab
    *
    * @author parvez.alam
    */
   public function createGitlabProject($parameters) {
      $client = $this->_client($this->gitlabAPI);
      try {
         $response = $client->post("projects", ['json' => $parameters])->getBody()->getContents();
         return json_decode($response);
      } catch(Exception $ex) {
         Log::info($ex->getMessage());
         return 'Unable to create gitlab project';
         
      }
      
      return 'Unable to create gitlab project';
   }

Above code, Created _client method that will take $token and $gitlabAPI params, this method return client object of git api.

createGitlabProject() method use to create client and send HTTP post call to gitlab API server.The server will response success with project_id or send error message ‘name has been taken’.

If you have requirements that needs our attentioN

Please feel free to contact us

We would love to hear from you

Share with your network

Facebook
Twitter
LinkedIn
WhatsApp
Email

This tutorial is intended to cover all the essentials steps and insights to understand How To Create Gitlab Project Using GIT API And Lumen

Genislab Technologies

Learn further