Skip to content

Commit 8a1f474

Browse files
committed
improve github summary feature
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent e37c2c3 commit 8a1f474

File tree

6 files changed

+76
-38
lines changed

6 files changed

+76
-38
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,5 +357,10 @@ or
357357
358358
```
359359

360+
## Experimental features
361+
362+
| Env var | Description |
363+
|-----------------------------------|---------------------------------|
364+
| `AZURETPL_EXPERIMENTAL_SUMMARY=1` | Post CICD summary (only GitHub) |
360365

361366
PS: some code is borrowed from [Helm](https://github.com/helm/helm)

azuretpl/command.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"github.com/webdevops/go-common/azuresdk/armclient"
1919
"github.com/webdevops/go-common/msgraphsdk/msgraphclient"
2020
"go.uber.org/zap"
21+
22+
"github.com/webdevops/helm-azure-tpl/azuretpl/models"
2123
)
2224

2325
type (
@@ -28,7 +30,7 @@ type (
2830
cache *cache.Cache
2931
cacheTtl time.Duration
3032

31-
opts Opts
33+
opts models.Opts
3234

3335
UserAgent string
3436

@@ -40,21 +42,6 @@ type (
4042
LintMode bool
4143

4244
azureCliAccountInfo map[string]interface{}
43-
44-
summary map[string][]string
45-
}
46-
47-
Opts struct {
48-
Keyvault struct {
49-
ExpiryWarning time.Duration `long:"keyvault.expiry.warningduration" env:"AZURETPL_KEYVAULT_EXPIRY_WARNING_DURATION" description:"warn before soon expiring Azure KeyVault entries" default:"168h"`
50-
IgnoreExpiry bool `long:"keyvault.expiry.ignore" env:"AZURETPL_KEYVAULT_EXPIRY_IGNORE" description:"ignore expiry date of Azure KeyVault entries and don't fail'"`
51-
}
52-
53-
ValuesFiles []string `long:"values" env:"AZURETPL_VALUES" env-delim:":" description:"path to yaml files for .Values"`
54-
JSONValues []string `long:"set-json" description:"set JSON values on the command line (can specify multiple or separate values with commas: key1=jsonval1,key2=jsonval2)"`
55-
Values []string `long:"set" description:"set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)"`
56-
StringValues []string `long:"set-string" description:"set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)"`
57-
FileValues []string `long:"set-file" description:"set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)"`
5845
}
5946
)
6047

@@ -63,14 +50,13 @@ var (
6350
msGraphClient *msgraphclient.MsGraphClient
6451
)
6552

66-
func New(ctx context.Context, opts Opts, logger *zap.SugaredLogger) *AzureTemplateExecutor {
53+
func New(ctx context.Context, opts models.Opts, logger *zap.SugaredLogger) *AzureTemplateExecutor {
6754
e := &AzureTemplateExecutor{
6855
ctx: ctx,
6956
logger: logger,
7057
opts: opts,
7158

7259
cacheTtl: 15 * time.Minute,
73-
summary: map[string][]string{},
7460
}
7561
e.init()
7662
return e
@@ -338,8 +324,6 @@ func (e *AzureTemplateExecutor) Parse(path string, templateData interface{}, buf
338324
return fmt.Errorf(`unable to process template: '%w'`, err)
339325
}
340326

341-
e.postSummary()
342-
343327
if err = os.Chdir(oldPwd); err != nil {
344328
return e.handleCicdError(err)
345329
}

azuretpl/models/opts.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package models
2+
3+
import (
4+
"time"
5+
)
6+
7+
type (
8+
Opts struct {
9+
Keyvault struct {
10+
ExpiryWarning time.Duration `long:"keyvault.expiry.warningduration" env:"AZURETPL_KEYVAULT_EXPIRY_WARNING_DURATION" description:"warn before soon expiring Azure KeyVault entries" default:"168h"`
11+
IgnoreExpiry bool `long:"keyvault.expiry.ignore" env:"AZURETPL_KEYVAULT_EXPIRY_IGNORE" description:"ignore expiry date of Azure KeyVault entries and don't fail'"`
12+
}
13+
14+
ValuesFiles []string `long:"values" env:"AZURETPL_VALUES" env-delim:":" description:"path to yaml files for .Values"`
15+
JSONValues []string `long:"set-json" description:"set JSON values on the command line (can specify multiple or separate values with commas: key1=jsonval1,key2=jsonval2)"`
16+
Values []string `long:"set" description:"set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)"`
17+
StringValues []string `long:"set-string" description:"set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)"`
18+
FileValues []string `long:"set-file" description:"set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)"`
19+
}
20+
)

azuretpl/summary.go

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,74 +8,99 @@ import (
88

99
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets"
1010
"github.com/webdevops/go-common/utils/to"
11+
"go.uber.org/zap"
12+
13+
"github.com/webdevops/helm-azure-tpl/config"
14+
)
15+
16+
const (
17+
SummaryHeader = "# helm-azure-tpl summary\n"
18+
SummaryValueNotSet = "<n/a>"
19+
)
20+
21+
var (
22+
summary map[string][]string = map[string][]string{}
1123
)
1224

1325
func (e *AzureTemplateExecutor) addSummaryLine(section, val string) {
14-
if _, ok := e.summary[section]; !ok {
15-
e.summary[section] = []string{}
26+
if _, ok := summary[section]; !ok {
27+
summary[section] = []string{}
1628
}
1729

18-
e.summary[section] = append(e.summary[section], val)
30+
summary[section] = append(summary[section], val)
1931
}
2032

2133
func (e *AzureTemplateExecutor) addSummaryKeyvaultSecret(vaultUrl string, secret azsecrets.GetSecretResponse) {
2234
section := "Azure Keyvault Secrets"
23-
if _, ok := e.summary[section]; !ok {
24-
e.summary[section] = []string{
35+
if _, ok := summary[section]; !ok {
36+
summary[section] = []string{
2537
"| KeyVault | Secret | Version | ContentType | Expiry |",
2638
"|----------|--------|---------|-------------|--------|",
2739
}
2840
}
2941

30-
expiryDate := "<not set>"
42+
expiryDate := SummaryValueNotSet
3143
if secret.Attributes != nil && secret.Attributes.Expires != nil {
3244
expiryDate = secret.Attributes.Expires.Format(time.RFC3339)
3345
}
3446

47+
contentType := to.String(secret.ContentType)
48+
if contentType == "" {
49+
contentType = SummaryValueNotSet
50+
}
51+
3552
val := fmt.Sprintf(
3653
"| %s | %s | %s | %s | %s |",
3754
vaultUrl,
3855
secret.ID.Name(),
3956
secret.ID.Version(),
40-
to.String(secret.ContentType),
57+
contentType,
4158
expiryDate,
4259
)
4360

44-
e.summary[section] = append(e.summary[section], val)
61+
summary[section] = append(summary[section], val)
4562
}
4663

47-
func (e *AzureTemplateExecutor) buildSummary() string {
48-
output := []string{
49-
"# helm-azure-tpl summary",
64+
func buildSummary(opts config.Opts) string {
65+
output := []string{SummaryHeader}
66+
67+
output = append(output, "templates:\n")
68+
for _, file := range opts.Args.Files {
69+
output = append(output, fmt.Sprintf("- %s", file))
5070
}
5171

52-
for section, rows := range e.summary {
72+
for section, rows := range summary {
5373
output = append(output, fmt.Sprintf("\n### %s\n", section))
5474
output = append(output, strings.Join(rows, "\n"))
5575
}
5676

5777
return "\n" + strings.Join(output, "\n") + "\n"
5878
}
5979

60-
func (e *AzureTemplateExecutor) postSummary() {
80+
func PostSummary(logger *zap.SugaredLogger, opts config.Opts) {
6181
if val := os.Getenv("AZURETPL_EXPERIMENTAL_SUMMARY"); val != "true" && val != "1" {
6282
return
6383
}
6484

85+
// skip empty summary
86+
if len(summary) == 0 {
87+
return
88+
}
89+
6590
// github summary
6691
if summaryPath := os.Getenv("GITHUB_STEP_SUMMARY"); summaryPath != "" {
67-
content := e.buildSummary()
92+
content := buildSummary(opts)
6893

6994
// If the file doesn't exist, create it, or append to the file
7095
f, err := os.OpenFile(summaryPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
7196
if err != nil {
72-
e.logger.Warnf(`unable to post GITHUB step summary: %w`, err)
97+
logger.Warnf(`unable to post GITHUB step summary: %w`, err)
7398
return
7499
}
75100
defer f.Close() // nolint: errcheck
76101

77102
if _, err := f.Write([]byte(content)); err != nil {
78-
e.logger.Warnf(`unable to post GITHUB step summary: %w`, err)
103+
logger.Warnf(`unable to post GITHUB step summary: %w`, err)
79104
}
80105
}
81106
}

config/opts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package config
33
import (
44
"encoding/json"
55

6-
"github.com/webdevops/helm-azure-tpl/azuretpl"
6+
"github.com/webdevops/helm-azure-tpl/azuretpl/models"
77
)
88

99
type (
@@ -34,7 +34,7 @@ type (
3434
FileExt *string `long:"target.fileext" env:"AZURETPL_TARGET_FILEEXT" description:"replaces file extension (or adds if empty) with this value (eg. '.yaml')"`
3535
}
3636

37-
AzureTpl azuretpl.Opts
37+
AzureTpl models.Opts
3838

3939
Args struct {
4040
Command string `positional-arg-name:"command" description:"specifies what to do (help, version, lint, apply)" choice:"help" choice:"version" choice:"lint" choice:"apply" required:"yes"` // nolint:staticcheck

process.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"go.uber.org/zap"
1515
yaml "gopkg.in/yaml.v3"
1616
"helm.sh/helm/v3/pkg/strvals"
17+
18+
"github.com/webdevops/helm-azure-tpl/azuretpl"
1719
)
1820

1921
const (
@@ -91,6 +93,8 @@ func run() {
9193
}
9294
}
9395

96+
azuretpl.PostSummary(logger, opts)
97+
9498
logger.With(zap.Duration("duration", time.Since(startTime))).Info("finished")
9599
default:
96100
fmt.Printf("invalid command '%v'\n", opts.Args.Command)

0 commit comments

Comments
 (0)