-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_devops.go
145 lines (123 loc) · 3.74 KB
/
azure_devops.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package pull_request
import (
"context"
"fmt"
"strings"
"github.com/microsoft/azure-devops-go-api/azuredevops"
core "github.com/microsoft/azure-devops-go-api/azuredevops/core"
git "github.com/microsoft/azure-devops-go-api/azuredevops/git"
)
const AZURE_DEVOPS_DEFAULT_URL = "https://dev.azure.com"
type AzureDevOpsClientFactory interface {
// Returns an Azure Devops Client interface.
GetClient(ctx context.Context) (git.Client, error)
}
type devopsFactoryImpl struct {
connection *azuredevops.Connection
}
func (factory *devopsFactoryImpl) GetClient(ctx context.Context) (git.Client, error) {
gitClient, err := git.NewClient(ctx, factory.connection)
if err != nil {
return nil, fmt.Errorf("failed to get new Azure DevOps git client for pull request generator: %w", err)
}
return gitClient, nil
}
type AzureDevOpsService struct {
clientFactory AzureDevOpsClientFactory
project string
repo string
labels []string
}
var _ PullRequestService = (*AzureDevOpsService)(nil)
var _ AzureDevOpsClientFactory = &devopsFactoryImpl{}
func NewAzureDevOpsService(ctx context.Context, token, url, organization, project, repo string, labels []string) (PullRequestService, error) {
organizationUrl := buildURL(url, organization)
var connection *azuredevops.Connection
if token == "" {
connection = azuredevops.NewAnonymousConnection(organizationUrl)
} else {
connection = azuredevops.NewPatConnection(organizationUrl, token)
}
return &AzureDevOpsService{
clientFactory: &devopsFactoryImpl{connection: connection},
project: project,
repo: repo,
labels: labels,
}, nil
}
func (a *AzureDevOpsService) List(ctx context.Context) ([]*PullRequest, error) {
client, err := a.clientFactory.GetClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get Azure DevOps client: %w", err)
}
args := git.GetPullRequestsByProjectArgs{
Project: &a.project,
SearchCriteria: &git.GitPullRequestSearchCriteria{},
}
azurePullRequests, err := client.GetPullRequestsByProject(ctx, args)
if err != nil {
return nil, fmt.Errorf("failed to get pull requests by project: %w", err)
}
pullRequests := []*PullRequest{}
for _, pr := range *azurePullRequests {
if pr.Repository == nil ||
pr.Repository.Name == nil ||
pr.PullRequestId == nil ||
pr.SourceRefName == nil ||
pr.LastMergeSourceCommit == nil ||
pr.LastMergeSourceCommit.CommitId == nil {
continue
}
azureDevOpsLabels := convertLabels(pr.Labels)
if !containAzureDevOpsLabels(a.labels, azureDevOpsLabels) {
continue
}
if *pr.Repository.Name == a.repo {
pullRequests = append(pullRequests, &PullRequest{
Number: *pr.PullRequestId,
Branch: strings.Replace(*pr.SourceRefName, "refs/heads/", "", 1),
HeadSHA: *pr.LastMergeSourceCommit.CommitId,
Labels: azureDevOpsLabels,
})
}
}
return pullRequests, nil
}
// convertLabels converts WebApiTagDefinitions to strings
func convertLabels(tags *[]core.WebApiTagDefinition) []string {
if tags == nil {
return []string{}
}
labelStrings := make([]string, len(*tags))
for i, label := range *tags {
labelStrings[i] = *label.Name
}
return labelStrings
}
// containAzureDevOpsLabels returns true if gotLabels contains expectedLabels
func containAzureDevOpsLabels(expectedLabels []string, gotLabels []string) bool {
for _, expected := range expectedLabels {
found := false
for _, got := range gotLabels {
if expected == got {
found = true
break
}
}
if !found {
return false
}
}
return true
}
func buildURL(url, organization string) string {
if url == "" {
url = AZURE_DEVOPS_DEFAULT_URL
}
separator := ""
if !strings.HasSuffix(url, "/") {
separator = "/"
}
devOpsURL := fmt.Sprintf("%s%s%s", url, separator, organization)
return devOpsURL
}