github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/services/pull_request/gitlab.go (about)

     1  package pull_request
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/argoproj/argo-cd/v2/applicationset/utils"
    10  	"github.com/hashicorp/go-retryablehttp"
    11  	gitlab "github.com/xanzy/go-gitlab"
    12  )
    13  
    14  type GitLabService struct {
    15  	client           *gitlab.Client
    16  	project          string
    17  	labels           []string
    18  	pullRequestState string
    19  }
    20  
    21  var _ PullRequestService = (*GitLabService)(nil)
    22  
    23  func NewGitLabService(ctx context.Context, token, url, project string, labels []string, pullRequestState string, scmRootCAPath string, insecure bool) (PullRequestService, error) {
    24  	var clientOptionFns []gitlab.ClientOptionFunc
    25  
    26  	// Set a custom Gitlab base URL if one is provided
    27  	if url != "" {
    28  		clientOptionFns = append(clientOptionFns, gitlab.WithBaseURL(url))
    29  	}
    30  
    31  	if token == "" {
    32  		token = os.Getenv("GITLAB_TOKEN")
    33  	}
    34  
    35  	tr := http.DefaultTransport.(*http.Transport).Clone()
    36  	tr.TLSClientConfig = utils.GetTlsConfig(scmRootCAPath, insecure)
    37  
    38  	retryClient := retryablehttp.NewClient()
    39  	retryClient.HTTPClient.Transport = tr
    40  
    41  	clientOptionFns = append(clientOptionFns, gitlab.WithHTTPClient(retryClient.HTTPClient))
    42  
    43  	client, err := gitlab.NewClient(token, clientOptionFns...)
    44  	if err != nil {
    45  		return nil, fmt.Errorf("error creating Gitlab client: %v", err)
    46  	}
    47  
    48  	return &GitLabService{
    49  		client:           client,
    50  		project:          project,
    51  		labels:           labels,
    52  		pullRequestState: pullRequestState,
    53  	}, nil
    54  }
    55  
    56  func (g *GitLabService) List(ctx context.Context) ([]*PullRequest, error) {
    57  
    58  	// Filter the merge requests on labels, if they are specified.
    59  	var labels *gitlab.Labels
    60  	if len(g.labels) > 0 {
    61  		labels = (*gitlab.Labels)(&g.labels)
    62  	}
    63  
    64  	opts := &gitlab.ListProjectMergeRequestsOptions{
    65  		ListOptions: gitlab.ListOptions{
    66  			PerPage: 100,
    67  		},
    68  		Labels: labels,
    69  	}
    70  
    71  	if g.pullRequestState != "" {
    72  		opts.State = &g.pullRequestState
    73  	}
    74  
    75  	pullRequests := []*PullRequest{}
    76  	for {
    77  		mrs, resp, err := g.client.MergeRequests.ListProjectMergeRequests(g.project, opts)
    78  		if err != nil {
    79  			return nil, fmt.Errorf("error listing merge requests for project '%s': %v", g.project, err)
    80  		}
    81  		for _, mr := range mrs {
    82  			pullRequests = append(pullRequests, &PullRequest{
    83  				Number:       mr.IID,
    84  				Branch:       mr.SourceBranch,
    85  				TargetBranch: mr.TargetBranch,
    86  				HeadSHA:      mr.SHA,
    87  				Labels:       mr.Labels,
    88  			})
    89  		}
    90  		if resp.NextPage == 0 {
    91  			break
    92  		}
    93  		opts.Page = resp.NextPage
    94  	}
    95  	return pullRequests, nil
    96  }