from gitcd.git.server import GitServer
from gitcd.git.branch import Branch

from gitcd.exceptions import GitcdGithubApiException

import requests


class Gitlab(GitServer):

    tokenSpace = 'gitlab'
    baseUrl = 'https://gitlab.com/api/v4'

    def open(
        self,
        title: str,
        body: str,
        fromBranch: Branch,
        toBranch: Branch
    ) -> bool:
        token = self.configPersonal.getToken(self.tokenSpace)
        if token is not None:

            projectId = '%s%s%s' % (
                self.remote.getUsername(),
                '%2F',
                self.remote.getRepositoryName()
            )
            url = '%s/projects/%s/merge_requests' % (
                self.baseUrl,
                projectId
            )

            data = {
                'source_branch': fromBranch.getName(),
                'target_branch': toBranch.getName(),
                'title': title,
                'description': body
            }
            headers = {'Private-Token': token}
            response = requests.post(
                url,