github.com/emmahsax/go-git-helper@v0.0.8-0.20240519163017-907b9de0fa52/internal/gitlab/gitlab.go (about)

     1  package gitlab
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/emmahsax/go-git-helper/internal/configfile"
     7  	"github.com/emmahsax/go-git-helper/internal/utils"
     8  	"github.com/xanzy/go-gitlab"
     9  )
    10  
    11  type GitLab struct {
    12  	Debug  bool
    13  	Client *gitlab.Client
    14  }
    15  
    16  func NewGitLab(debugB bool) *GitLab {
    17  	cf := configfile.NewConfigFile(debugB)
    18  	c, err := newGitLabClient(cf.GitLabToken(), debugB)
    19  	if err != nil {
    20  		customErr := errors.New("could not create GitLab client: " + err.Error())
    21  		utils.HandleError(customErr, debugB, nil)
    22  		return nil
    23  	}
    24  
    25  	return &GitLab{
    26  		Debug:  debugB,
    27  		Client: c,
    28  	}
    29  }
    30  
    31  func (c *GitLab) CreateMergeRequest(projectName string, options *gitlab.CreateMergeRequestOptions) (*gitlab.MergeRequest, error) {
    32  	mr, _, err := c.Client.MergeRequests.CreateMergeRequest(projectName, options)
    33  	if err != nil {
    34  		utils.HandleError(err, c.Debug, nil)
    35  		return nil, err
    36  	}
    37  
    38  	return mr, nil
    39  }
    40  
    41  func newGitLabClient(token string, debugB bool) (*gitlab.Client, error) {
    42  	git, err := gitlab.NewClient(token)
    43  	if err != nil {
    44  		utils.HandleError(err, debugB, nil)
    45  		return nil, err
    46  	}
    47  	return git, nil
    48  }