-
Notifications
You must be signed in to change notification settings - Fork 173
/
azdo_provider.go
810 lines (713 loc) · 23.3 KB
/
azdo_provider.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package pipeline
import (
"context"
"errors"
"fmt"
"log"
"regexp"
"strings"
"github.com/azure/azure-dev/cli/azd/pkg/azdo"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
"github.com/azure/azure-dev/cli/azd/pkg/environment/azdcontext"
"github.com/azure/azure-dev/cli/azd/pkg/exec"
"github.com/azure/azure-dev/cli/azd/pkg/graphsdk"
"github.com/azure/azure-dev/cli/azd/pkg/infra/provisioning"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
"github.com/azure/azure-dev/cli/azd/pkg/tools"
"github.com/azure/azure-dev/cli/azd/pkg/tools/azcli"
"github.com/azure/azure-dev/cli/azd/pkg/tools/git"
"github.com/microsoft/azure-devops-go-api/azuredevops"
"github.com/microsoft/azure-devops-go-api/azuredevops/build"
azdoGit "github.com/microsoft/azure-devops-go-api/azuredevops/git"
)
// AzdoScmProvider implements ScmProvider using Azure DevOps as the provider
// for source control manager.
type AzdoScmProvider struct {
envManager environment.Manager
repoDetails *AzdoRepositoryDetails
env *environment.Environment
azdContext *azdcontext.AzdContext
azdoConnection *azuredevops.Connection
commandRunner exec.CommandRunner
console input.Console
gitCli git.GitCli
}
func NewAzdoScmProvider(
envManager environment.Manager,
env *environment.Environment,
azdContext *azdcontext.AzdContext,
commandRunner exec.CommandRunner,
console input.Console,
gitCli git.GitCli,
) ScmProvider {
return &AzdoScmProvider{
envManager: envManager,
env: env,
azdContext: azdContext,
commandRunner: commandRunner,
console: console,
gitCli: gitCli,
}
}
// AzdoRepositoryDetails provides extra state needed for the AzDo provider.
// this is stored as the details property in repoDetails
type AzdoRepositoryDetails struct {
projectName string
projectId string
repoId string
orgName string
repoName string
repoWebUrl string
remoteUrl string
sshUrl string
buildDefinition *build.BuildDefinition
}
// *** subareaProvider implementation ******
// requiredTools return the list of external tools required by
// Azure DevOps provider during its execution.
func (p *AzdoScmProvider) requiredTools(_ context.Context) ([]tools.ExternalTool, error) {
return []tools.ExternalTool{}, nil
}
// preConfigureCheck check the current state of external tools and any
// other dependency to be as expected for execution.
func (p *AzdoScmProvider) preConfigureCheck(
ctx context.Context,
pipelineManagerArgs PipelineManagerArgs,
infraOptions provisioning.Options,
projectPath string,
) (bool, error) {
_, updatedPat, err := azdo.EnsurePatExists(ctx, p.env, p.console)
if err != nil {
return updatedPat, err
}
_, updatedOrg, err := azdo.EnsureOrgNameExists(ctx, p.envManager, p.env, p.console)
return (updatedPat || updatedOrg), err
}
// helper function to save configuration values to .env file
func (p *AzdoScmProvider) saveEnvironmentConfig(ctx context.Context, key string, value string) error {
p.env.DotenvSet(key, value)
err := p.envManager.Save(ctx, p.env)
if err != nil {
return err
}
return nil
}
// name returns the name of the provider
func (p *AzdoScmProvider) Name() string {
return "Azure DevOps"
}
// *** scmProvider implementation ******
// stores repo details in state for use in other functions. Also saves AzDo project details to .env
func (p *AzdoScmProvider) StoreRepoDetails(ctx context.Context, repo *azdoGit.GitRepository) error {
repoDetails := p.getRepoDetails()
repoDetails.repoName = *repo.Name
repoDetails.remoteUrl = *repo.RemoteUrl
repoDetails.repoWebUrl = *repo.WebUrl
repoDetails.sshUrl = *repo.SshUrl
repoDetails.repoId = repo.Id.String()
err := p.saveEnvironmentConfig(ctx, azdo.AzDoEnvironmentRepoIdName, p.repoDetails.repoId)
if err != nil {
return fmt.Errorf("error saving repo id to environment %w", err)
}
err = p.saveEnvironmentConfig(ctx, azdo.AzDoEnvironmentRepoName, p.repoDetails.repoName)
if err != nil {
return fmt.Errorf("error saving repo name to environment %w", err)
}
err = p.saveEnvironmentConfig(ctx, azdo.AzDoEnvironmentRepoWebUrl, p.repoDetails.repoWebUrl)
if err != nil {
return fmt.Errorf("error saving repo web url to environment %w", err)
}
return nil
}
// prompts the user for a new AzDo Git repo and creates the repo
func (p *AzdoScmProvider) createNewGitRepositoryFromInput(ctx context.Context, console input.Console) (string, error) {
connection, err := p.getAzdoConnection(ctx)
if err != nil {
return "", err
}
var repo *azdoGit.GitRepository
for {
name, err := console.Prompt(ctx, input.ConsoleOptions{
Message: "Enter the name for your new Azure DevOps Repository OR Hit enter to use this name:",
DefaultValue: p.repoDetails.projectName,
})
if err != nil {
return "", fmt.Errorf("asking for new project name: %w", err)
}
var message string
newRepo, err := azdo.CreateRepository(ctx, p.repoDetails.projectId, name, connection)
if err != nil {
message = err.Error()
}
if strings.Contains(message, fmt.Sprintf("A Git repository with the name %s already exists.", name)) {
console.Message(ctx, fmt.Sprintf("error: the repo name '%s' is already in use\n", name))
continue // try again
} else if strings.Contains(message, "TF401025: 'repoName' is not a valid name for a Git repository.") {
console.Message(ctx, fmt.Sprintf(
"error: '%s' is not a valid Azure DevOps repo name. "+
"See https://aka.ms/azure-dev/azdo-repo-naming\n", name))
continue // try again
} else if err != nil {
return "", fmt.Errorf("creating repository: %w", err)
} else {
repo = newRepo
break
}
}
err = p.StoreRepoDetails(ctx, repo)
if err != nil {
return "", err
}
return *repo.RemoteUrl, nil
}
// verifies that a repo exists or prompts the user to select from a list of existing AzDo repos
func (p *AzdoScmProvider) ensureGitRepositoryExists(ctx context.Context, console input.Console) (string, error) {
if p.repoDetails != nil && p.repoDetails.repoName != "" {
return p.repoDetails.remoteUrl, nil
}
connection, err := p.getAzdoConnection(ctx)
if err != nil {
return "", err
}
repo, err := azdo.GetGitRepositoriesInProject(ctx, p.repoDetails.projectName, p.repoDetails.orgName, connection, console)
if err != nil {
return "", err
}
err = p.StoreRepoDetails(ctx, repo)
if err != nil {
return "", err
}
return *repo.RemoteUrl, nil
}
// helper function to return repoDetails from state
func (p *AzdoScmProvider) getRepoDetails() *AzdoRepositoryDetails {
if p.repoDetails != nil {
return p.repoDetails
}
repoDetails := &AzdoRepositoryDetails{}
p.repoDetails = repoDetails
return p.repoDetails
}
// helper function to return an azuredevops.Connection for use with AzDo Go SDK
func (p *AzdoScmProvider) getAzdoConnection(ctx context.Context) (*azuredevops.Connection, error) {
if p.azdoConnection != nil {
return p.azdoConnection, nil
}
org, _, err := azdo.EnsureOrgNameExists(ctx, p.envManager, p.env, p.console)
if err != nil {
return nil, err
}
repoDetails := p.getRepoDetails()
repoDetails.orgName = org
pat, _, err := azdo.EnsurePatExists(ctx, p.env, p.console)
if err != nil {
return nil, err
}
connection, err := azdo.GetConnection(ctx, org, pat)
if err != nil {
return nil, err
}
return connection, nil
}
// returns an existing project or prompts the user to either select a project or a create a new AzDo project
func (p *AzdoScmProvider) ensureProjectExists(ctx context.Context, console input.Console) (string, string, bool, error) {
if p.repoDetails != nil && p.repoDetails.projectName != "" {
return p.repoDetails.projectName, p.repoDetails.projectId, false, nil
}
idx, err := console.Select(ctx, input.ConsoleOptions{
Message: "How would you like to configure your git remote to Azure DevOps?",
Options: []string{
"Select an existing Azure DevOps project",
"Create a new Azure DevOps Project",
},
DefaultValue: "Create a new Azure DevOps Project",
})
if err != nil {
return "", "", false, fmt.Errorf("prompting for azdo project type: %w", err)
}
connection, err := p.getAzdoConnection(ctx)
if err != nil {
return "", "", false, err
}
var projectName string
var projectId string
var newProject bool = false
switch idx {
// Select from an existing AzDo project
case 0:
projectName, projectId, err = azdo.GetProjectFromExisting(ctx, connection, console)
if err != nil {
return "", "", false, err
}
// Create a new AzDo project
case 1:
projectName, projectId, err = azdo.GetProjectFromNew(
ctx,
p.azdContext.ProjectDirectory(),
connection,
p.env,
console,
)
newProject = true
if err != nil {
return "", "", false, err
}
default:
panic(fmt.Sprintf("unexpected selection index %d", idx))
}
return projectName, projectId, newProject, nil
}
// configureGitRemote set up or create the git project and git remote
func (p *AzdoScmProvider) configureGitRemote(
ctx context.Context,
repoPath string,
remoteName string,
) (string, error) {
projectName, projectId, newProject, err := p.ensureProjectExists(ctx, p.console)
if err != nil {
return "", err
}
repoDetails := p.getRepoDetails()
repoDetails.projectName = projectName
repoDetails.projectId = projectId
err = p.saveEnvironmentConfig(ctx, azdo.AzDoEnvironmentProjectIdName, projectId)
if err != nil {
return "", fmt.Errorf("error saving project id to environment %w", err)
}
err = p.saveEnvironmentConfig(ctx, azdo.AzDoEnvironmentProjectName, projectName)
if err != nil {
return "", fmt.Errorf("error saving project name to environment %w", err)
}
var remoteUrl string
if !newProject {
remoteUrl, err = p.promptForAzdoRepository(ctx, p.console)
if err != nil {
return "", err
}
} else {
remoteUrl, err = p.getDefaultRepoRemote(ctx, projectName, projectId, p.console)
if err != nil {
return "", err
}
}
branch, err := p.getCurrentGitBranch(ctx, repoPath)
if err != nil {
return "", err
}
azdo.DefaultBranch = branch
return remoteUrl, nil
}
func (p *AzdoScmProvider) getCurrentGitBranch(ctx context.Context, repoPath string) (string, error) {
branch, err := p.gitCli.GetCurrentBranch(ctx, repoPath)
if err != nil {
return "", err
}
return branch, nil
}
// returns the git remote for a newly created repo that is part of a newly created AzDo project
func (p *AzdoScmProvider) getDefaultRepoRemote(
ctx context.Context,
projectName string,
projectId string,
console input.Console,
) (string, error) {
connection, err := p.getAzdoConnection(ctx)
if err != nil {
return "", err
}
repo, err := azdo.GetDefaultGitRepositoriesInProject(ctx, projectName, connection)
if err != nil {
return "", err
}
err = p.StoreRepoDetails(ctx, repo)
if err != nil {
return "", err
}
return *repo.RemoteUrl, nil
}
// prompt the user to select azdo repo or create a new one
func (p *AzdoScmProvider) promptForAzdoRepository(ctx context.Context, console input.Console) (string, error) {
var remoteUrl string
// There are a few ways to configure the remote so offer a choice to the user.
idx, err := console.Select(ctx, input.ConsoleOptions{
Message: fmt.Sprintf("How would you like to configure your remote? (Organization: %s)", p.repoDetails.projectName),
Options: []string{
"Select an existing Azure DevOps Repository",
"Create a new private Azure DevOps Repository",
},
DefaultValue: "Create a new private Azure DevOps Repository",
})
if err != nil {
return "", fmt.Errorf("prompting for remote configuration type: %w", err)
}
switch idx {
// Select from an existing Azure DevOps project
case 0:
remoteUrl, err = p.ensureGitRepositoryExists(ctx, console)
if err != nil {
return "", err
}
// Create a new project
case 1:
remoteUrl, err = p.createNewGitRepositoryFromInput(ctx, console)
if err != nil {
return "", err
}
default:
panic(fmt.Sprintf("unexpected selection index %d", idx))
}
return remoteUrl, nil
}
// defines the structure of an ssl git remote
var azdoRemoteGitUrlRegex = regexp.MustCompile(`^git@ssh.dev.azure\.com:(.*?)(?:\.git)?$`)
// defines the structure of an HTTPS git remote
var azdoRemoteHttpsUrlRegex = regexp.MustCompile(`^https://[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*:*.+@dev.azure\.com/(.*?)$`)
// ErrRemoteHostIsNotAzDo the error used when a non Azure DevOps remote is found
var ErrRemoteHostIsNotAzDo = errors.New("existing remote is not an Azure DevOps host")
// ErrSSHNotSupported the error used when ssh git remote is detected
var ErrSSHNotSupported = errors.New("ssh git remote is not supported. " +
"Use HTTPS git remote to connect the remote repository")
// helper function to determine if the provided remoteUrl is an azure devops repo.
// currently supports AzDo PaaS
func isAzDoRemote(remoteUrl string) error {
if azdoRemoteGitUrlRegex.MatchString(remoteUrl) {
return ErrSSHNotSupported
}
slug := ""
for _, r := range []*regexp.Regexp{azdoRemoteGitUrlRegex, azdoRemoteHttpsUrlRegex} {
captures := r.FindStringSubmatch(remoteUrl)
if captures != nil {
slug = captures[1]
}
}
if slug == "" {
return ErrRemoteHostIsNotAzDo
}
return nil
}
func parseAzDoRemote(remoteUrl string) (string, error) {
for _, r := range []*regexp.Regexp{azdoRemoteGitUrlRegex, azdoRemoteHttpsUrlRegex} {
captures := r.FindStringSubmatch(remoteUrl)
if captures != nil {
return captures[1], nil
}
}
return "", nil
}
// gitRepoDetails extracts the information from an Azure DevOps remote url into general scm concepts
// like owner, name and path
func (p *AzdoScmProvider) gitRepoDetails(ctx context.Context, remoteUrl string) (*gitRepositoryDetails, error) {
err := isAzDoRemote(remoteUrl)
if err != nil {
return nil, err
}
repoDetails := p.getRepoDetails()
// Try getting values from the env.
// This is a quick shortcut to avoid parsing the remote in detail.
// While using the same .env file, the outputs from creating a project and repository
// are memorized in .env file
if repoDetails.orgName == "" {
repoDetails.orgName = p.env.Getenv(azdo.AzDoEnvironmentOrgName)
}
if repoDetails.projectName == "" {
repoDetails.projectName = p.env.Getenv(azdo.AzDoEnvironmentProjectName)
}
if repoDetails.projectId == "" {
repoDetails.projectId = p.env.Getenv(azdo.AzDoEnvironmentProjectIdName)
}
if repoDetails.repoName == "" {
repoDetails.repoName = p.env.Getenv(azdo.AzDoEnvironmentRepoName)
}
if repoDetails.repoId == "" {
repoDetails.repoId = p.env.Getenv(azdo.AzDoEnvironmentRepoIdName)
}
if repoDetails.repoWebUrl == "" {
repoDetails.repoWebUrl = p.env.Getenv(azdo.AzDoEnvironmentRepoWebUrl)
}
if repoDetails.remoteUrl == "" {
repoDetails.remoteUrl = remoteUrl
}
if repoDetails.projectId == "" || repoDetails.repoId == "" {
// Removing environment or creating a new one would remove any memory fro project
// and repo. In that case, it needs to be calculated from the remote url
azdoSlug, err := parseAzDoRemote(remoteUrl)
if err != nil {
return nil, fmt.Errorf("parsing Azure DevOps remote url: %s: %w", remoteUrl, err)
}
// azdoSlug => Org/Project/_git/repoName
parts := strings.Split(azdoSlug, "_git/")
repoDetails.projectName = strings.Split(parts[0], "/")[1]
p.env.DotenvSet(azdo.AzDoEnvironmentProjectName, repoDetails.projectName)
repoDetails.repoName = parts[1]
p.env.DotenvSet(azdo.AzDoEnvironmentRepoName, repoDetails.repoName)
connection, err := p.getAzdoConnection(ctx)
if err != nil {
return nil, fmt.Errorf("Getting azdo connection: %w", err)
}
repo, err := azdo.GetGitRepository(ctx, repoDetails.projectName, repoDetails.repoName, connection)
if err != nil {
return nil, fmt.Errorf("Looking for repository: %w", err)
}
repoDetails.repoId = repo.Id.String()
p.env.DotenvSet(azdo.AzDoEnvironmentRepoIdName, repoDetails.repoId)
repoDetails.repoWebUrl = *repo.WebUrl
p.env.DotenvSet(azdo.AzDoEnvironmentRepoWebUrl, repoDetails.repoWebUrl)
proj, err := azdo.GetProjectByName(ctx, connection, repoDetails.projectName)
if err != nil {
return nil, fmt.Errorf("Looking for project: %w", err)
}
repoDetails.projectId = proj.Id.String()
p.env.DotenvSet(azdo.AzDoEnvironmentProjectIdName, repoDetails.projectId)
if err := p.envManager.Save(ctx, p.env); err != nil {
return nil, fmt.Errorf("saving environment: %w", err)
}
}
return &gitRepositoryDetails{
owner: p.repoDetails.orgName,
repoName: p.repoDetails.repoName,
details: repoDetails,
remote: repoDetails.remoteUrl,
url: repoDetails.repoWebUrl,
}, nil
}
// preventGitPush is nil for Azure DevOps
func (p *AzdoScmProvider) preventGitPush(
ctx context.Context,
gitRepo *gitRepositoryDetails,
remoteName string,
branchName string) (bool, error) {
return false, nil
}
func azdoPat(ctx context.Context, env *environment.Environment, console input.Console) string {
pat, _, err := azdo.EnsurePatExists(ctx, env, console)
if err != nil {
log.Printf("Error getting PAT when it should be found: %s", err.Error())
}
return pat
}
func gitInsteadOfConfig(
ctx context.Context,
pat string,
gitRepo *gitRepositoryDetails) (string, string) {
azdoRepoDetails := gitRepo.details.(*AzdoRepositoryDetails)
remoteAndPatUrl := fmt.Sprintf("url.https://%s@%s/", pat, azdo.AzDoHostName)
originalUrl := fmt.Sprintf("https://%s@%s/", azdoRepoDetails.orgName, azdo.AzDoHostName)
return remoteAndPatUrl, originalUrl
}
// Push code and queue pipeline
func (p *AzdoScmProvider) GitPush(
ctx context.Context,
gitRepo *gitRepositoryDetails,
remoteName string,
branchName string) error {
// ** Push code with PAT
// This is the same as gitCli.PushUpstream(), but it adds `-c url.PAT+HostName.insteadOf=HostName` to execute
// git push with the PAT to authenticate
pat := azdoPat(ctx, p.env, p.console)
remoteAndPatUrl, originalUrl := gitInsteadOfConfig(ctx, pat, gitRepo)
runArgs := exec.NewRunArgsWithSensitiveData("git",
[]string{
"-C",
gitRepo.gitProjectPath,
"-c",
fmt.Sprintf("%s.insteadOf=%s", remoteAndPatUrl, originalUrl),
"push",
"--set-upstream",
"--quiet",
remoteName,
branchName,
},
[]string{
pat,
},
).WithInteractive(true)
if _, err := p.commandRunner.Run(ctx, runArgs); err != nil {
// this error should not fail the operation
log.Printf("Error setting git config: insteadOf url: %s", err.Error())
}
// *** Queue pipeline
connection, err := p.getAzdoConnection(ctx)
if err != nil {
return err
}
err = azdo.CreateBuildPolicy(
ctx,
connection,
p.repoDetails.projectId,
p.repoDetails.repoId,
p.repoDetails.buildDefinition,
p.env,
)
if err != nil {
return err
}
err = azdo.QueueBuild(ctx, connection, p.repoDetails.projectId, p.repoDetails.buildDefinition)
if err != nil {
return err
}
return nil
}
// AzdoCiProvider implements a CiProvider using Azure DevOps to manage CI with azdo pipelines.
type AzdoCiProvider struct {
envManager environment.Manager
Env *environment.Environment
AzdContext *azdcontext.AzdContext
credentials *azcli.AzureCredentials
console input.Console
commandRunner exec.CommandRunner
}
func NewAzdoCiProvider(
envManager environment.Manager,
env *environment.Environment,
azdContext *azdcontext.AzdContext,
console input.Console,
commandRunner exec.CommandRunner,
) CiProvider {
return &AzdoCiProvider{
envManager: envManager,
Env: env,
AzdContext: azdContext,
console: console,
commandRunner: commandRunner,
}
}
// *** subareaProvider implementation ******
// requiredTools defines the requires tools for GitHub to be used as CI manager
func (p *AzdoCiProvider) requiredTools(_ context.Context) ([]tools.ExternalTool, error) {
return []tools.ExternalTool{}, nil
}
// preConfigureCheck nil for Azdo
func (p *AzdoCiProvider) preConfigureCheck(
ctx context.Context,
pipelineManagerArgs PipelineManagerArgs,
infraOptions provisioning.Options,
projectPath string,
) (bool, error) {
authType := PipelineAuthType(pipelineManagerArgs.PipelineAuthTypeName)
if authType == AuthTypeFederated {
return false, fmt.Errorf(
//nolint:lll
"Azure DevOps does not support federated authentication. To explicitly use client credentials set the %s flag. %w",
output.WithBackticks("--auth-type client-credentials"),
ErrAuthNotSupported,
)
}
_, updatedPat, err := azdo.EnsurePatExists(ctx, p.Env, p.console)
if err != nil {
return updatedPat, err
}
_, updatedOrg, err := azdo.EnsureOrgNameExists(ctx, p.envManager, p.Env, p.console)
return (updatedPat || updatedOrg), err
}
// name returns the name of the provider.
func (p *AzdoCiProvider) Name() string {
return "Azure DevOps"
}
// *** ciProvider implementation ******
func (p *AzdoCiProvider) credentialOptions(
ctx context.Context,
repoDetails *gitRepositoryDetails,
infraOptions provisioning.Options,
authType PipelineAuthType,
) *CredentialOptions {
if authType == "" || authType == AuthTypeClientCredentials {
return &CredentialOptions{
EnableClientCredentials: true,
}
}
return &CredentialOptions{
EnableClientCredentials: false,
EnableFederatedCredentials: false,
}
}
// configureConnection set up Azure DevOps with the Azure credential
func (p *AzdoCiProvider) configureConnection(
ctx context.Context,
repoDetails *gitRepositoryDetails,
provisioningProvider provisioning.Options,
servicePrincipal *graphsdk.ServicePrincipal,
authType PipelineAuthType,
credentials *azcli.AzureCredentials,
) error {
p.credentials = credentials
details := repoDetails.details.(*AzdoRepositoryDetails)
org, _, err := azdo.EnsureOrgNameExists(ctx, p.envManager, p.Env, p.console)
if err != nil {
return err
}
pat, _, err := azdo.EnsurePatExists(ctx, p.Env, p.console)
if err != nil {
return err
}
connection, err := azdo.GetConnection(ctx, org, pat)
if err != nil {
return err
}
err = azdo.CreateServiceConnection(ctx, connection, details.projectId, *p.Env, p.credentials, p.console)
if err != nil {
return err
}
p.console.MessageUxItem(ctx, &ux.MultilineMessage{
Lines: []string{
"",
"Azure DevOps project and connection is now configured.",
""},
})
return nil
}
// configurePipeline create Azdo pipeline
func (p *AzdoCiProvider) configurePipeline(
ctx context.Context,
repoDetails *gitRepositoryDetails,
options *configurePipelineOptions,
) (CiPipeline, error) {
details := repoDetails.details.(*AzdoRepositoryDetails)
org, _, err := azdo.EnsureOrgNameExists(ctx, p.envManager, p.Env, p.console)
if err != nil {
return nil, err
}
pat, _, err := azdo.EnsurePatExists(ctx, p.Env, p.console)
if err != nil {
return nil, err
}
connection, err := azdo.GetConnection(ctx, org, pat)
if err != nil {
return nil, err
}
buildDefinition, err := azdo.CreatePipeline(
ctx,
details.projectId,
azdo.AzurePipelineName,
details.repoName,
connection,
p.credentials,
p.Env,
p.console,
*options.provisioningProvider,
options.secrets,
options.variables,
)
if err != nil {
return nil, err
}
details.buildDefinition = buildDefinition
return &pipeline{
repoDetails: details,
}, nil
}
// pipeline is the implementation for a CiPipeline for Azure DevOps
type pipeline struct {
repoDetails *AzdoRepositoryDetails
}
func (p *pipeline) name() string {
return *p.repoDetails.buildDefinition.Name
}
func (p *pipeline) url() string {
repoUrl := p.repoDetails.repoWebUrl
repoPrefix := strings.Split(repoUrl, "_git")[0]
return fmt.Sprintf("%s_build?definitionId=%d", repoPrefix, *p.repoDetails.buildDefinition.Id)
}