-
Notifications
You must be signed in to change notification settings - Fork 173
/
github_provider.go
898 lines (790 loc) · 29 KB
/
github_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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package pipeline
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"net/url"
"path/filepath"
"regexp"
"strings"
"github.com/azure/azure-dev/cli/azd/pkg/account"
"github.com/azure/azure-dev/cli/azd/pkg/convert"
"github.com/azure/azure-dev/cli/azd/pkg/environment"
githubRemote "github.com/azure/azure-dev/cli/azd/pkg/github"
"github.com/azure/azure-dev/cli/azd/pkg/graphsdk"
"github.com/azure/azure-dev/cli/azd/pkg/httputil"
"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/azure/azure-dev/cli/azd/pkg/tools/github"
"golang.org/x/exp/slices"
)
// GitHubScmProvider implements ScmProvider using GitHub as the provider
// for source control manager.
type GitHubScmProvider struct {
newGitHubRepoCreated bool
console input.Console
ghCli github.GitHubCli
gitCli git.GitCli
}
func NewGitHubScmProvider(
console input.Console,
ghCli github.GitHubCli,
gitCli git.GitCli,
) ScmProvider {
return &GitHubScmProvider{
console: console,
ghCli: ghCli,
gitCli: gitCli,
}
}
// *** subareaProvider implementation ******
// requiredTools return the list of external tools required by
// GitHub provider during its execution.
func (p *GitHubScmProvider) requiredTools(ctx context.Context) ([]tools.ExternalTool, error) {
return []tools.ExternalTool{p.ghCli}, nil
}
// preConfigureCheck check the current state of external tools and any
// other dependency to be as expected for execution.
func (p *GitHubScmProvider) preConfigureCheck(
ctx context.Context,
pipelineManagerArgs PipelineManagerArgs,
infraOptions provisioning.Options,
projectPath string,
) (bool, error) {
return ensureGitHubLogin(ctx, projectPath, p.ghCli, p.gitCli, github.GitHubHostName, p.console)
}
// name returns the name of the provider
func (p *GitHubScmProvider) Name() string {
return "GitHub"
}
// *** scmProvider implementation ******
// configureGitRemote uses GitHub cli to guide user on setting a remote url
// for the local git project
func (p *GitHubScmProvider) configureGitRemote(
ctx context.Context,
repoPath string,
remoteName string,
) (string, error) {
// used to detect when the GitHub has created a new repo
p.newGitHubRepoCreated = false
// There are a few ways to configure the remote so offer a choice to the user.
idx, err := p.console.Select(ctx, input.ConsoleOptions{
Message: "How would you like to configure your git remote to GitHub?",
Options: []string{
"Select an existing GitHub project",
"Create a new private GitHub repository",
"Enter a remote URL directly",
},
DefaultValue: "Create a new private GitHub repository",
})
if err != nil {
return "", fmt.Errorf("prompting for remote configuration type: %w", err)
}
var remoteUrl string
switch idx {
// Select from an existing GitHub project
case 0:
remoteUrl, err = getRemoteUrlFromExisting(ctx, p.ghCli, p.console)
if err != nil {
return "", fmt.Errorf("getting remote from existing repository: %w", err)
}
// Create a new project
case 1:
remoteUrl, err = getRemoteUrlFromNewRepository(ctx, p.ghCli, repoPath, p.console)
if err != nil {
return "", fmt.Errorf("getting remote from new repository: %w", err)
}
p.newGitHubRepoCreated = true
// Enter a URL directly.
case 2:
remoteUrl, err = getRemoteUrlFromPrompt(ctx, remoteName, p.console)
if err != nil {
return "", fmt.Errorf("getting remote from prompt: %w", err)
}
default:
panic(fmt.Sprintf("unexpected selection index %d", idx))
}
return remoteUrl, nil
}
// defines the structure of an ssl git remote
var gitHubRemoteGitUrlRegex = regexp.MustCompile(`^git@github\.com:(.*?)(?:\.git)?$`)
// defines the structure of an HTTPS git remote
var gitHubRemoteHttpsUrlRegex = regexp.MustCompile(`^https://(?:www\.)?github\.com/(.*?)(?:\.git)?$`)
// ErrRemoteHostIsNotGitHub the error used when a non GitHub remote is found
var ErrRemoteHostIsNotGitHub = errors.New("not a github host")
// gitRepoDetails extracts the information from a GitHub remote url into general scm concepts
// like owner, name and path
func (p *GitHubScmProvider) gitRepoDetails(ctx context.Context, remoteUrl string) (*gitRepositoryDetails, error) {
slug := ""
for _, r := range []*regexp.Regexp{gitHubRemoteGitUrlRegex, gitHubRemoteHttpsUrlRegex} {
captures := r.FindStringSubmatch(remoteUrl)
if captures != nil {
slug = captures[1]
}
}
if slug == "" {
return nil, ErrRemoteHostIsNotGitHub
}
slugParts := strings.Split(slug, "/")
repoDetails := &gitRepositoryDetails{
owner: slugParts[0],
repoName: slugParts[1],
remote: remoteUrl,
}
repoDetails.url = fmt.Sprintf(
"https://github.com/%s/%s",
repoDetails.owner,
repoDetails.repoName)
return repoDetails, nil
}
// preventGitPush validate if GitHub actions are disabled and won't work before pushing
// changes to upstream.
func (p *GitHubScmProvider) preventGitPush(
ctx context.Context,
gitRepo *gitRepositoryDetails,
remoteName string,
branchName string) (bool, error) {
// Don't need to check for preventing push on new created repos
// Only check when using an existing repo in case github actions are disabled
if !p.newGitHubRepoCreated {
slug := gitRepo.owner + "/" + gitRepo.repoName
return p.notifyWhenGitHubActionsAreDisabled(ctx, gitRepo.gitProjectPath, slug, remoteName, branchName)
}
return false, nil
}
func (p *GitHubScmProvider) GitPush(
ctx context.Context,
gitRepo *gitRepositoryDetails,
remoteName string,
branchName string) error {
return p.gitCli.PushUpstream(ctx, gitRepo.gitProjectPath, remoteName, branchName)
}
// enum type for taking a choice after finding GitHub actions disabled.
type gitHubActionsEnablingChoice int
// defines the options upon detecting GitHub actions disabled.
const (
manualChoice gitHubActionsEnablingChoice = iota
cancelChoice
)
// enables gitHubActionsEnablingChoice to produce a string value.
func (selection gitHubActionsEnablingChoice) String() string {
switch selection {
case manualChoice:
return "I have manually enabled GitHub Actions. Continue with pushing my changes."
case cancelChoice:
return "Exit without pushing my changes. I don't need to run GitHub actions right now."
}
panic("Tried to convert invalid input gitHubActionsEnablingChoice to string")
}
// notifyWhenGitHubActionsAreDisabled uses GitHub cli to check if actions are disabled
// or if at least one workflow is not listed. Returns true after interacting with user
// and if user decides to stop a current petition to push changes to upstream.
func (p *GitHubScmProvider) notifyWhenGitHubActionsAreDisabled(
ctx context.Context,
gitProjectPath,
repoSlug string,
origin string,
branch string,
) (bool, error) {
ghActionsInUpstreamRepo, err := p.ghCli.GitHubActionsExists(ctx, repoSlug)
if err != nil {
return false, err
}
if ghActionsInUpstreamRepo {
// upstream is already listing GitHub actions.
// There's no need to check if there are local workflows
return false, nil
}
// Upstream has no GitHub actions listed.
// See if there's at least one workflow file within .github/workflows
ghLocalWorkflowFiles := false
defaultGitHubWorkflowPathLocation := filepath.Join(
gitProjectPath,
".github",
"workflows")
err = filepath.WalkDir(defaultGitHubWorkflowPathLocation,
func(folderName string, file fs.DirEntry, e error) error {
if e != nil {
return e
}
fileName := file.Name()
fileExtension := filepath.Ext(fileName)
if fileExtension == ".yml" || fileExtension == ".yaml" {
// ** workflow file found.
// Now check if this file is already tracked by git.
// If the file is not tracked, it means this is a new file (never pushed to mainstream)
// A git untracked file should not be considered as GitHub workflow until it is pushed.
newFile, err := p.gitCli.IsUntrackedFile(ctx, gitProjectPath, folderName)
if err != nil {
return fmt.Errorf("checking workflow file %w", err)
}
if !newFile {
ghLocalWorkflowFiles = true
}
}
return nil
})
if err != nil {
return false, fmt.Errorf("Getting GitHub local workflow files %w", err)
}
if ghLocalWorkflowFiles {
message := fmt.Sprintf("\n%s\n"+
" - If you forked and cloned a template, enable actions here: %s.\n"+
" - Otherwise, check the GitHub Actions permissions here: %s.\n",
output.WithHighLightFormat("GitHub actions are currently disabled for your repository."),
output.WithHighLightFormat("https://github.com/%s/actions", repoSlug),
output.WithHighLightFormat("https://github.com/%s/settings/actions", repoSlug))
p.console.Message(ctx, message)
rawSelection, err := p.console.Select(ctx, input.ConsoleOptions{
Message: "What would you like to do now?",
Options: []string{
manualChoice.String(),
cancelChoice.String(),
},
DefaultValue: manualChoice.String(),
})
if err != nil {
return false, fmt.Errorf("prompting to enable github actions: %w", err)
}
choice := gitHubActionsEnablingChoice(rawSelection)
if choice == manualChoice {
return false, nil
}
if choice == cancelChoice {
return true, nil
}
}
return false, nil
}
const (
federatedIdentityIssuer = "https://token.actions.githubusercontent.com"
federatedIdentityAudience = "api://AzureADTokenExchange"
)
// GitHubCiProvider implements a CiProvider using GitHub to manage CI pipelines as
// GitHub actions.
type GitHubCiProvider struct {
env *environment.Environment
credentialProvider account.SubscriptionCredentialProvider
adService azcli.AdService
ghCli github.GitHubCli
gitCli git.GitCli
console input.Console
httpClient httputil.HttpClient
}
func NewGitHubCiProvider(
env *environment.Environment,
credentialProvider account.SubscriptionCredentialProvider,
adService azcli.AdService,
ghCli github.GitHubCli,
gitCli git.GitCli,
console input.Console,
httpClient httputil.HttpClient) CiProvider {
return &GitHubCiProvider{
env: env,
credentialProvider: credentialProvider,
adService: adService,
ghCli: ghCli,
gitCli: gitCli,
console: console,
httpClient: httpClient,
}
}
// *** subareaProvider implementation ******
// requiredTools defines the requires tools for GitHub to be used as CI manager
func (p *GitHubCiProvider) requiredTools(ctx context.Context) ([]tools.ExternalTool, error) {
return []tools.ExternalTool{p.ghCli}, nil
}
// preConfigureCheck validates that current state of tools and GitHub is as expected to
// execute.
func (p *GitHubCiProvider) preConfigureCheck(
ctx context.Context,
pipelineManagerArgs PipelineManagerArgs,
infraOptions provisioning.Options,
projectPath string,
) (bool, error) {
updated, err := ensureGitHubLogin(ctx, projectPath, p.ghCli, p.gitCli, github.GitHubHostName, p.console)
if err != nil {
return updated, err
}
authType := PipelineAuthType(pipelineManagerArgs.PipelineAuthTypeName)
// Federated Auth + Terraform is not a supported combination
if infraOptions.Provider == provisioning.Terraform {
// Throw error if Federated auth is explicitly requested
if authType == AuthTypeFederated {
return false, fmt.Errorf(
//nolint:lll
"Terraform does not support federated authentication. To explicitly use client credentials set the %s flag. %w",
output.WithBackticks("--auth-type client-credentials"),
ErrAuthNotSupported,
)
} else if authType == "" {
// If not explicitly set, show warning
p.console.MessageUxItem(
ctx,
&ux.WarningMessage{
//nolint:lll
Description: "Terraform provisioning does not support federated authentication, defaulting to Service Principal with client ID and client secret.\n",
},
)
}
}
return updated, nil
}
// name returns the name of the provider.
func (p *GitHubCiProvider) Name() string {
return "GitHub"
}
func (p *GitHubCiProvider) credentialOptions(
ctx context.Context,
repoDetails *gitRepositoryDetails,
infraOptions provisioning.Options,
authType PipelineAuthType,
) *CredentialOptions {
// Default auth type to client-credentials for terraform
if infraOptions.Provider == provisioning.Terraform && authType == "" {
authType = AuthTypeClientCredentials
}
if authType == AuthTypeClientCredentials {
return &CredentialOptions{
EnableClientCredentials: true,
}
}
// If not specified default to federated credentials
if authType == "" || authType == AuthTypeFederated {
// Configure federated auth for both main branch and current branch
branches := []string{repoDetails.branch}
if !slices.Contains(branches, "main") {
branches = append(branches, "main")
}
repoSlug := repoDetails.owner + "/" + repoDetails.repoName
credentialSafeName := strings.ReplaceAll(repoSlug, "/", "-")
federatedCredentials := []*graphsdk.FederatedIdentityCredential{
{
Name: url.PathEscape(fmt.Sprintf("%s-pull_request", credentialSafeName)),
Issuer: federatedIdentityIssuer,
Subject: fmt.Sprintf("repo:%s:pull_request", repoSlug),
Description: convert.RefOf("Created by Azure Developer CLI"),
Audiences: []string{federatedIdentityAudience},
},
}
for _, branch := range branches {
branchCredentials := &graphsdk.FederatedIdentityCredential{
Name: url.PathEscape(fmt.Sprintf("%s-%s", credentialSafeName, branch)),
Issuer: federatedIdentityIssuer,
Subject: fmt.Sprintf("repo:%s:ref:refs/heads/%s", repoSlug, branch),
Description: convert.RefOf("Created by Azure Developer CLI"),
Audiences: []string{federatedIdentityAudience},
}
federatedCredentials = append(federatedCredentials, branchCredentials)
}
return &CredentialOptions{
EnableFederatedCredentials: true,
FederatedCredentialOptions: federatedCredentials,
}
}
return &CredentialOptions{
EnableClientCredentials: false,
EnableFederatedCredentials: false,
}
}
// *** ciProvider implementation ******
// configureConnection set up GitHub account with Azure Credentials for
// GitHub actions to use a service principal account to log in to Azure
// and make changes on behalf of a user.
func (p *GitHubCiProvider) configureConnection(
ctx context.Context,
repoDetails *gitRepositoryDetails,
infraOptions provisioning.Options,
servicePrincipal *graphsdk.ServicePrincipal,
authType PipelineAuthType,
credentials *azcli.AzureCredentials,
) error {
// Default auth type to client-credentials for terraform
if infraOptions.Provider == provisioning.Terraform && authType == "" {
authType = AuthTypeClientCredentials
}
repoSlug := repoDetails.owner + "/" + repoDetails.repoName
if authType == AuthTypeClientCredentials {
err := p.configureClientCredentialsAuth(ctx, infraOptions, repoSlug, servicePrincipal, credentials)
if err != nil {
return fmt.Errorf("configuring client credentials auth: %w", err)
}
}
if err := p.setPipelineVariables(ctx, repoSlug, infraOptions, servicePrincipal); err != nil {
return fmt.Errorf("failed setting pipeline variables: %w", err)
}
return nil
}
// setPipelineVariables sets all the pipeline variables required for the pipeline to run. This includes the environment
// variables that the core of AZD uses (AZURE_ENV_NAME) as well as the variables that the provisioning system needs to run
// (AZURE_SUBSCRIPTION_ID, AZURE_LOCATION) as well as scenario specific variables (AZURE_RESOURCE_GROUP for resource group
// scoped deployments, a series of RS_ variables for terraform remote state)
func (p *GitHubCiProvider) setPipelineVariables(
ctx context.Context,
repoSlug string,
infraOptions provisioning.Options,
servicePrincipal *graphsdk.ServicePrincipal,
) error {
for name, value := range map[string]string{
environment.EnvNameEnvVarName: p.env.Name(),
environment.LocationEnvVarName: p.env.GetLocation(),
environment.SubscriptionIdEnvVarName: p.env.GetSubscriptionId(),
environment.TenantIdEnvVarName: *servicePrincipal.AppOwnerOrganizationId,
"AZURE_CLIENT_ID": servicePrincipal.AppId,
} {
if err := p.ghCli.SetVariable(ctx, repoSlug, name, value); err != nil {
return fmt.Errorf("failed setting %s variable: %w", name, err)
}
p.console.MessageUxItem(ctx, &ux.CreatedRepoValue{
Name: name,
Kind: ux.GitHubVariable,
})
}
if infraOptions.Provider == provisioning.Terraform {
remoteStateKeys := []string{"RS_RESOURCE_GROUP", "RS_STORAGE_ACCOUNT", "RS_CONTAINER_NAME"}
for _, key := range remoteStateKeys {
value, ok := p.env.LookupEnv(key)
if !ok || strings.TrimSpace(value) == "" {
p.console.StopSpinner(ctx, "Configuring terraform", input.StepWarning)
p.console.MessageUxItem(ctx, &ux.WarningMessage{
Description: "Terraform Remote State configuration is invalid",
HidePrefix: true,
})
p.console.Message(
ctx,
fmt.Sprintf(
"Visit %s for more information on configuring Terraform remote state",
output.WithLinkFormat("https://aka.ms/azure-dev/terraform"),
),
)
p.console.Message(ctx, "")
return errors.New("terraform remote state is not correctly configured")
}
// env var was found
if err := p.ghCli.SetVariable(ctx, repoSlug, key, value); err != nil {
return fmt.Errorf("setting terraform remote state variables: %w", err)
}
p.console.MessageUxItem(ctx, &ux.CreatedRepoValue{
Name: key,
Kind: ux.GitHubVariable,
})
}
}
if infraOptions.Provider == provisioning.Bicep {
if rgName, has := p.env.LookupEnv(environment.ResourceGroupEnvVarName); has {
if err := p.ghCli.SetVariable(ctx, repoSlug, environment.ResourceGroupEnvVarName, rgName); err != nil {
return fmt.Errorf("failed setting %s variable: %w", environment.ResourceGroupEnvVarName, err)
}
}
}
return nil
}
// Configures Github for standard Service Principal authentication with client id & secret
func (p *GitHubCiProvider) configureClientCredentialsAuth(
ctx context.Context,
infraOptions provisioning.Options,
repoSlug string,
servicePrincipal *graphsdk.ServicePrincipal,
credentials *azcli.AzureCredentials,
) error {
/* #nosec G101 - Potential hardcoded credentials - false positive */
secretName := "AZURE_CREDENTIALS"
credsJson, err := json.Marshal(credentials)
if err != nil {
return fmt.Errorf("failed marshalling azure credentials: %w", err)
}
if err := p.ghCli.SetSecret(ctx, repoSlug, secretName, string(credsJson)); err != nil {
return fmt.Errorf("failed setting %s secret: %w", secretName, err)
}
p.console.MessageUxItem(ctx, &ux.CreatedRepoValue{
Name: secretName,
Kind: ux.GitHubSecret,
})
if infraOptions.Provider == provisioning.Terraform {
for key, info := range map[string]struct {
value string
secret bool
}{
"ARM_TENANT_ID": {credentials.TenantId, false},
"ARM_CLIENT_ID": {credentials.ClientId, false},
"ARM_CLIENT_SECRET": {credentials.ClientSecret, true},
} {
if !info.secret {
if err := p.ghCli.SetVariable(ctx, repoSlug, key, info.value); err != nil {
return fmt.Errorf("setting github variable %s:: %w", key, err)
}
p.console.MessageUxItem(ctx, &ux.CreatedRepoValue{
Name: key,
Kind: ux.GitHubVariable,
})
} else {
if err := p.ghCli.SetSecret(ctx, repoSlug, key, info.value); err != nil {
return fmt.Errorf("setting github secret %s:: %w", key, err)
}
p.console.MessageUxItem(ctx, &ux.CreatedRepoValue{
Name: key,
Kind: ux.GitHubSecret,
})
}
}
}
return nil
}
// configurePipeline is a no-op for GitHub, as the pipeline is automatically
// created by creating the workflow files in .github folder.
func (p *GitHubCiProvider) configurePipeline(
ctx context.Context,
repoDetails *gitRepositoryDetails,
options *configurePipelineOptions,
) (CiPipeline, error) {
repoSlug := repoDetails.owner + "/" + repoDetails.repoName
// Variables and Secrets for a gh-actions are independent from the gh-action. They are set on the repository level.
// We need to clean up the previous values before setting the new ones.
// By doing this, we are handling:
// - When a secret is moved to be a variable (or vice versa). Don't leak the previous value on the pipeline.
// - When there was a previous additional variable/secret set and then it was updated to empty string or unset from .env.
msg := ""
var procErr error
ciSecrets, ciVariables := []string{}, []string{}
if len(options.projectVariables) > 0 {
msg = "Setting up project's variables to be used in the pipeline"
ciSecretsInstance, err := p.ghCli.ListSecrets(ctx, repoSlug)
if err != nil {
return nil, fmt.Errorf("unable to get list of repository secrets: %w", err)
}
ciVariablesInstance, err := p.ghCli.ListVariables(ctx, repoSlug)
if err != nil {
return nil, fmt.Errorf("unable to get list of repository variables: %w", err)
}
ciSecrets = ciSecretsInstance
ciVariables = ciVariablesInstance
p.console.ShowSpinner(ctx, msg, input.Step)
}
defer func() {
if msg != "" {
p.console.StopSpinner(ctx, msg, input.GetStepResultFormat(procErr))
}
if procErr == nil {
p.console.MessageUxItem(ctx, &ux.MultilineMessage{
Lines: []string{
"",
"GitHub Action secrets are now configured. You can view GitHub action secrets that were created at this link:",
output.WithLinkFormat("https://github.com/%s/settings/secrets/actions", repoSlug),
""},
})
}
}()
// create map of variables for O(1) lookup during clean up
variablesAndSecretsMap := make(map[string]string, len(options.projectVariables)+len(options.projectSecrets))
for _, value := range options.projectVariables {
variablesAndSecretsMap[value] = value
}
for _, value := range options.projectSecrets {
variablesAndSecretsMap[value] = value
}
// iterate the existing secrets on the pipeline and remove the ones matching the project's secrets or variables
for _, existingSecret := range ciSecrets {
if _, willBeUpdated := options.secrets[existingSecret]; willBeUpdated {
// if the secret will be updated, we don't need to delete it
continue
}
// only delete if the secret is defined in the project's secrets or variables (azure.yaml)
if _, exists := variablesAndSecretsMap[existingSecret]; exists {
deleteErr := p.ghCli.DeleteSecret(ctx, repoSlug, existingSecret)
if deleteErr != nil {
procErr = fmt.Errorf("failed deleting %s secret: %w", existingSecret, deleteErr)
return nil, procErr
}
}
}
// iterate the existing variables on the pipeline and remove the ones matching the project's secrets or variables
for _, existingVariable := range ciVariables {
if _, willBeUpdated := options.variables[existingVariable]; willBeUpdated {
// if the variable will be updated, we don't need to delete it
continue
}
// only delete if the variable is defined in the project's secrets or variables (azure.yaml)
if _, exists := variablesAndSecretsMap[existingVariable]; exists {
deleteErr := p.ghCli.DeleteVariable(ctx, repoSlug, existingVariable)
if deleteErr != nil {
procErr = fmt.Errorf("failed deleting %s variable: %w", existingVariable, deleteErr)
return nil, procErr
}
}
}
// set the new variables and secrets
for key, value := range options.secrets {
if err := p.ghCli.SetSecret(ctx, repoSlug, key, value); err != nil {
procErr = fmt.Errorf("failed setting %s secret: %w", key, err)
return nil, procErr
}
}
for key, value := range options.variables {
if err := p.ghCli.SetVariable(ctx, repoSlug, key, value); err != nil {
procErr = fmt.Errorf("failed setting %s secret: %w", key, err)
return nil, procErr
}
}
return &workflow{
repoDetails: repoDetails,
}, nil
}
// workflow is the implementation for a CiPipeline for GitHub
type workflow struct {
repoDetails *gitRepositoryDetails
}
func (w *workflow) name() string {
return "actions"
}
func (w *workflow) url() string {
return w.repoDetails.url + "/actions"
}
// ensureGitHubLogin ensures the user is logged into the GitHub CLI. If not, it prompt the user
// if they would like to log in and if so runs `gh auth login` interactively.
func ensureGitHubLogin(
ctx context.Context,
projectPath string,
ghCli github.GitHubCli,
gitCli git.GitCli,
hostname string,
console input.Console) (bool, error) {
authResult, err := ghCli.GetAuthStatus(ctx, hostname)
if err != nil {
return false, err
}
if authResult.LoggedIn {
return false, nil
}
for {
var accept bool
accept, err := console.Confirm(ctx, input.ConsoleOptions{
Message: "This command requires you to be logged into GitHub. Log in using the GitHub CLI?",
DefaultValue: true,
})
if err != nil {
return false, fmt.Errorf("prompting to log in to github: %w", err)
}
if !accept {
return false, errors.New("interactive GitHub login declined; use `gh auth login` to log into GitHub")
}
ghGitProtocol, err := ghCli.GetGitProtocolType(ctx)
if err != nil {
return false, err
}
if err := ghCli.Login(ctx, hostname); err == nil {
if github.RunningOnCodespaces() && projectPath != "" && ghGitProtocol == github.GitHttpsProtocolType {
// For HTTPS, using gh as credential helper will avoid git asking for password
// Credential helper is only set for codespaces to improve the experience,
// see more about this here: https://github.com/Azure/azure-dev/issues/2451
if err := gitCli.SetGitHubAuthForRepo(
ctx, projectPath, fmt.Sprintf("https://%s", hostname), ghCli.BinaryPath()); err != nil {
return false, err
}
}
return true, nil
}
fmt.Fprintln(console.Handles().Stdout, "There was an issue logging into GitHub.")
}
}
// getRemoteUrlFromExisting let user to select an existing repository from his/her account and
// returns the remote url for that repository.
func getRemoteUrlFromExisting(ctx context.Context, ghCli github.GitHubCli, console input.Console) (string, error) {
repos, err := ghCli.ListRepositories(ctx)
if err != nil {
return "", fmt.Errorf("listing existing repositories: %w", err)
}
options := make([]string, 0, len(repos))
for _, repo := range repos {
options = append(options, repo.NameWithOwner)
}
if len(options) == 0 {
return "", errors.New("no existing GitHub repositories found")
}
repoIdx, err := console.Select(ctx, input.ConsoleOptions{
Message: "Choose an existing GitHub repository",
Options: options,
})
if err != nil {
return "", fmt.Errorf("prompting for repository: %w", err)
}
return selectRemoteUrl(ctx, ghCli, repos[repoIdx])
}
// selectRemoteUrl let user to type and enter the url from an existing GitHub repo.
// If the url is valid, the remote url is returned. Otherwise an error is returned.
func selectRemoteUrl(ctx context.Context, ghCli github.GitHubCli, repo github.GhCliRepository) (string, error) {
protocolType, err := ghCli.GetGitProtocolType(ctx)
if err != nil {
return "", fmt.Errorf("detecting default protocol: %w", err)
}
switch protocolType {
case github.GitHttpsProtocolType:
return repo.HttpsUrl, nil
case github.GitSshProtocolType:
return repo.SshUrl, nil
default:
panic(fmt.Sprintf("unexpected protocol type: %s", protocolType))
}
}
// getRemoteUrlFromNewRepository creates a new repository on GitHub and returns its remote url
func getRemoteUrlFromNewRepository(
ctx context.Context,
ghCli github.GitHubCli,
currentPathName string,
console input.Console,
) (string, error) {
var repoName string
currentFolderName := filepath.Base(currentPathName)
for {
name, err := console.Prompt(ctx, input.ConsoleOptions{
Message: "Enter the name for your new repository OR Hit enter to use this name:",
DefaultValue: currentFolderName,
})
if err != nil {
return "", fmt.Errorf("asking for new repository name: %w", err)
}
err = ghCli.CreatePrivateRepository(ctx, name)
if errors.Is(err, github.ErrRepositoryNameInUse) {
console.Message(ctx, fmt.Sprintf("error: the repository name '%s' is already in use\n", name))
continue // try again
} else if err != nil {
return "", fmt.Errorf("creating repository: %w", err)
} else {
repoName = name
break
}
}
repo, err := ghCli.ViewRepository(ctx, repoName)
if err != nil {
return "", fmt.Errorf("fetching repository info: %w", err)
}
return selectRemoteUrl(ctx, ghCli, repo)
}
// getRemoteUrlFromPrompt interactively prompts the user for a URL for a GitHub repository. It validates
// that the URL is well formed and is in the correct format for a GitHub repository.
func getRemoteUrlFromPrompt(ctx context.Context, remoteName string, console input.Console) (string, error) {
remoteUrl := ""
for remoteUrl == "" {
promptValue, err := console.Prompt(ctx, input.ConsoleOptions{
Message: fmt.Sprintf("Enter the url to use for remote %s:", remoteName),
})
if err != nil {
return "", fmt.Errorf("prompting for remote url: %w", err)
}
remoteUrl = promptValue
if _, err := githubRemote.GetSlugForRemote(remoteUrl); errors.Is(err, githubRemote.ErrRemoteHostIsNotGitHub) {
fmt.Fprintf(console.Handles().Stdout, "error: \"%s\" is not a valid GitHub URL.\n", remoteUrl)
// So we retry from the loop.
remoteUrl = ""
}
}
return remoteUrl, nil
}