/
azure_devops.go
208 lines (170 loc) · 5.83 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package checks
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/microsoft/azure-devops-go-api/azuredevops/v7"
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/core"
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/pipelines"
"github.com/flanksource/canary-checker/api/context"
v1 "github.com/flanksource/canary-checker/api/v1"
"github.com/flanksource/canary-checker/pkg"
"github.com/flanksource/duty/models"
)
type AzureDevopsChecker struct {
}
func (t *AzureDevopsChecker) Run(ctx *context.Context) pkg.Results {
var results pkg.Results
for _, conf := range ctx.Canary.Spec.AzureDevops {
results = append(results, t.check(ctx, conf)...)
}
return results
}
func (t *AzureDevopsChecker) Type() string {
return "azuredevops"
}
func (t *AzureDevopsChecker) check(ctx *context.Context, check v1.AzureDevopsCheck) pkg.Results {
result := pkg.Success(check, ctx.Canary)
var results pkg.Results
results = append(results, result)
var err error
var c *models.Connection
if check.PersonalAccessToken.ValueStatic != "" {
c = &models.Connection{Password: check.PersonalAccessToken.ValueStatic}
} else if c, err = ctx.HydrateConnectionByURL(check.ConnectionName); err != nil {
return results.Failf("failed to hydrate connection: %v", err)
} else if c != nil {
if c, err = c.Merge(ctx, check); err != nil {
return results.Failf("failed to merge connection: %v", err)
}
}
connection := azuredevops.NewPatConnection(fmt.Sprintf("https://dev.azure.com/%s", check.Organization), c.Password)
coreClient, err := core.NewClient(ctx, connection)
if err != nil {
return results.ErrorMessage(fmt.Errorf("failed to create core client: %w", err))
}
project, err := coreClient.GetProject(ctx, core.GetProjectArgs{ProjectId: &check.Project})
if err != nil {
return results.ErrorMessage(fmt.Errorf("failed to get project (name=%s): %w", check.Project, err))
}
projectID := project.Id.String()
pipelineRegexp, err := regexp.Compile(check.Pipeline)
isRegexp := nil == err
// regexp compilation failed means we assume that it's a literal string.
// However, even literal strings can be valid regexp, like "Hello world".
pipelineClient := pipelines.NewClient(ctx, connection)
allPipelines, err := pipelineClient.ListPipelines(ctx, pipelines.ListPipelinesArgs{Project: &projectID})
if err != nil {
return results.ErrorMessage(fmt.Errorf("failed to get pipeline (project=%s): %w", check.Project, err))
}
for _, pipeline := range *allPipelines {
if isRegexp {
matched := pipelineRegexp.MatchString(*pipeline.Name)
if !matched {
continue
}
} else if *pipeline.Name != check.Pipeline {
continue
}
// This fetches top 10,000 runs for a particular pipeline.
// Unfortunately, there's no way to fetch the latest run for a pipeline.
// Additionally, this endpoint doesn't support the filters we require
// - fetching only X amount of runs
// - fetching only those runs that have completed
// https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/list?view=azure-devops-rest-7.1
runs, err := pipelineClient.ListRuns(ctx, pipelines.ListRunsArgs{PipelineId: pipeline.Id, Project: &projectID})
if err != nil {
return results.ErrorMessage(fmt.Errorf("failed to get runs (pipeline=%s): %w", check.Pipeline, err))
}
latestRun := getLatestCompletedRun(*runs)
if latestRun == nil {
continue
}
if !matchPipelineVariables(check.Variables, latestRun.Variables) {
continue
}
// Need to query the Run API to get more details about it
// because the ListRuns API doesn't return Resources.
latestRun, err = pipelineClient.GetRun(ctx, pipelines.GetRunArgs{Project: &projectID, PipelineId: pipeline.Id, RunId: (*runs)[0].Id})
if err != nil {
return results.ErrorMessage(fmt.Errorf("failed to get run (pipeline=%s): %w", check.Pipeline, err))
}
if !matchBranchNames(check.Branches, latestRun.Resources) {
continue
}
if check.ThresholdMillis != nil {
runDuration := latestRun.FinishedDate.Time.Sub(latestRun.CreatedDate.Time)
threhold := time.Duration(*check.ThresholdMillis) * time.Millisecond
if runDuration > threhold {
return results.Failf("Runtime:%v was over the threshold:%v", runDuration, threhold)
}
}
if *latestRun.Result != pipelines.RunResultValues.Succeeded {
return results.Failf("Runtime completed with unsuccessful result: %s", *latestRun.Result)
}
}
return results
}
// getLatestCompletedRun returns the latest completed pipeline run.
// It assumes that the runs are ordered by completion date in descending order.
func getLatestCompletedRun(runs []pipelines.Run) *pipelines.Run {
if len(runs) == 0 {
return nil
}
for _, run := range runs {
if *run.State != pipelines.RunStateValues.Completed {
continue
}
return &run
}
return nil
}
func matchPipelineVariables(want map[string]string, got *map[string]pipelines.Variable) bool {
if len(want) == 0 {
return true
}
if len(want) != 0 && len(*got) == 0 {
return false
}
for k, v := range want {
if _, ok := (*got)[k]; !ok {
return false
}
if *(*got)[k].Value != v {
return false
}
}
return true
}
func matchBranchNames(branches []string, resources *pipelines.RunResources) bool {
if len(branches) == 0 {
return true
}
if len(branches) != 0 && resources == nil {
return false
}
branchName := extractBranchName(resources)
for _, w := range branches {
if branchName == w {
return true
}
}
return false
}
// extractBranchName extracts the name of the branch from a RunResources object.
// The branch name is extracted from the refname which is of the form "refs/heads/2pm".
func extractBranchName(got *pipelines.RunResources) string {
repo, ok := (*got.Repositories)["self"]
if !ok {
return ""
}
if repo.RefName == nil {
return ""
}
sections := strings.Split(*repo.RefName, "/")
if len(sections) == 0 {
return ""
}
return sections[len(sections)-1]
}