-
Notifications
You must be signed in to change notification settings - Fork 584
/
ado.go
109 lines (89 loc) · 2.82 KB
/
ado.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
package ado
import (
"context"
"fmt"
"github.com/microsoft/azure-devops-go-api/azuredevops"
"github.com/microsoft/azure-devops-go-api/azuredevops/build"
"github.com/pkg/errors"
)
const azureUrl = "https://dev.azure.com"
type BuildClient interface {
UpdateVariables(variables []Variable) error
}
type BuildClientImpl struct {
ctx context.Context
buildClient build.Client
project string
pipelineID int
}
type Variable struct {
Name string
Value string
IsSecret bool
AllowOverride bool
}
// UpdateVariables updates variables in build definition or creates them if they are missing
func (bc *BuildClientImpl) UpdateVariables(variables []Variable) error {
if len(variables) == 0 {
return errors.New("error: slice variables must not be empty")
}
getDefinitionArgs := build.GetDefinitionArgs{
Project: &bc.project,
DefinitionId: &bc.pipelineID,
}
// Get a build definition
buildDefinition, err := bc.buildClient.GetDefinition(bc.ctx, getDefinitionArgs)
if err != nil {
return errors.Wrapf(err, "error: get definition failed")
}
buildDefinitionVars := map[string]build.BuildDefinitionVariable{}
if buildDefinition.Variables != nil {
buildDefinitionVars = *buildDefinition.Variables
}
for _, variable := range variables {
buildDefinitionVars[variable.Name] = build.BuildDefinitionVariable{
Value: &variable.Value,
IsSecret: &variable.IsSecret,
AllowOverride: &variable.AllowOverride,
}
}
buildDefinition.Variables = &buildDefinitionVars
updateDefinitionArgs := build.UpdateDefinitionArgs{
Definition: buildDefinition,
Project: &bc.project,
DefinitionId: &bc.pipelineID,
}
_, err = bc.buildClient.UpdateDefinition(bc.ctx, updateDefinitionArgs)
if err != nil {
return errors.Wrapf(err, "error: update definition failed")
}
return nil
}
// NewBuildClient Create a client to interact with the Build area
func NewBuildClient(organization string, personalAccessToken string, project string, pipelineID int) (BuildClient, error) {
if organization == "" {
return nil, errors.New("error: organization must not be empty")
}
if personalAccessToken == "" {
return nil, errors.New("error: personal access token must not be empty")
}
if project == "" {
return nil, errors.New("error: project must not be empty")
}
organizationUrl := fmt.Sprintf("%s/%s", azureUrl, organization)
// Create a connection to your organization
connection := azuredevops.NewPatConnection(organizationUrl, personalAccessToken)
ctx := context.Background()
// Create a client to interact with the Core area
buildClient, err := build.NewClient(ctx, connection)
if err != nil {
return nil, err
}
buildClientImpl := &BuildClientImpl{
ctx: ctx,
buildClient: buildClient,
project: project,
pipelineID: pipelineID,
}
return buildClientImpl, nil
}