github.com/argoproj/argo-cd/v2@v2.10.9/applicationset/services/pull_request/bitbucket_server.go (about) 1 package pull_request 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/argoproj/argo-cd/v2/applicationset/utils" 8 bitbucketv1 "github.com/gfleury/go-bitbucket-v1" 9 log "github.com/sirupsen/logrus" 10 ) 11 12 type BitbucketService struct { 13 client *bitbucketv1.APIClient 14 projectKey string 15 repositorySlug string 16 // Not supported for PRs by Bitbucket Server 17 // labels []string 18 } 19 20 var _ PullRequestService = (*BitbucketService)(nil) 21 22 func NewBitbucketServiceBasicAuth(ctx context.Context, username, password, url, projectKey, repositorySlug string) (PullRequestService, error) { 23 bitbucketConfig := bitbucketv1.NewConfiguration(url) 24 // Avoid the XSRF check 25 bitbucketConfig.AddDefaultHeader("x-atlassian-token", "no-check") 26 bitbucketConfig.AddDefaultHeader("x-requested-with", "XMLHttpRequest") 27 28 ctx = context.WithValue(ctx, bitbucketv1.ContextBasicAuth, bitbucketv1.BasicAuth{ 29 UserName: username, 30 Password: password, 31 }) 32 return newBitbucketService(ctx, bitbucketConfig, projectKey, repositorySlug) 33 } 34 35 func NewBitbucketServiceNoAuth(ctx context.Context, url, projectKey, repositorySlug string) (PullRequestService, error) { 36 return newBitbucketService(ctx, bitbucketv1.NewConfiguration(url), projectKey, repositorySlug) 37 } 38 39 func newBitbucketService(ctx context.Context, bitbucketConfig *bitbucketv1.Configuration, projectKey, repositorySlug string) (PullRequestService, error) { 40 bitbucketConfig.BasePath = utils.NormalizeBitbucketBasePath(bitbucketConfig.BasePath) 41 bitbucketClient := bitbucketv1.NewAPIClient(ctx, bitbucketConfig) 42 43 return &BitbucketService{ 44 client: bitbucketClient, 45 projectKey: projectKey, 46 repositorySlug: repositorySlug, 47 }, nil 48 } 49 50 func (b *BitbucketService) List(_ context.Context) ([]*PullRequest, error) { 51 paged := map[string]interface{}{ 52 "limit": 100, 53 } 54 55 pullRequests := []*PullRequest{} 56 for { 57 response, err := b.client.DefaultApi.GetPullRequestsPage(b.projectKey, b.repositorySlug, paged) 58 if err != nil { 59 return nil, fmt.Errorf("error listing pull requests for %s/%s: %v", b.projectKey, b.repositorySlug, err) 60 } 61 pulls, err := bitbucketv1.GetPullRequestsResponse(response) 62 if err != nil { 63 log.Errorf("error parsing pull request response '%v'", response.Values) 64 return nil, fmt.Errorf("error parsing pull request response for %s/%s: %v", b.projectKey, b.repositorySlug, err) 65 } 66 67 for _, pull := range pulls { 68 pullRequests = append(pullRequests, &PullRequest{ 69 Number: pull.ID, 70 Branch: pull.FromRef.DisplayID, // ID: refs/heads/main DisplayID: main 71 TargetBranch: pull.ToRef.DisplayID, 72 HeadSHA: pull.FromRef.LatestCommit, // This is not defined in the official docs, but works in practice 73 Labels: []string{}, // Not supported by library 74 }) 75 } 76 77 hasNextPage, nextPageStart := bitbucketv1.HasNextPage(response) 78 if !hasNextPage { 79 break 80 } 81 paged["start"] = nextPageStart 82 } 83 return pullRequests, nil 84 }