• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / kubebuilder / 25006393222

27 Apr 2026 04:14PM UTC coverage: 81.841%. First build
25006393222

Pull #5584

github

camilamacedo86
chore(alpha update/AutoUpdate): Changes to ensure better usage and allow granular workflow and permissions
Pull Request #5584: WIP: chore(alpha update/AutoUpdate): Changes to ensure better usage and allow granular workflow and permissions

0 of 63 new or added lines in 2 files covered. (0.0%)

7707 of 9417 relevant lines covered (81.84%)

73.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.54
/pkg/plugins/optional/autoupdate/v1alpha/edit.go
1
/*
2
Copyright 2025 The Kubernetes Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package v1alpha
18

19
import (
20
        "fmt"
21
        log "log/slog"
22

23
        "github.com/spf13/pflag"
24

25
        "sigs.k8s.io/kubebuilder/v4/pkg/config"
26
        "sigs.k8s.io/kubebuilder/v4/pkg/machinery"
27
        "sigs.k8s.io/kubebuilder/v4/pkg/plugin"
28
        "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/autoupdate/v1alpha/scaffolds"
29
)
30

31
var _ plugin.EditSubcommand = &editSubcommand{}
32

33
type editSubcommand struct {
34
        config      config.Config
35
        useGHModels bool
36
        openGHIssue bool
37
        openGHPR    bool
38
        force       bool
39
        flagSet     *pflag.FlagSet
40

41
        // Merged config after Scaffold() - used in PostScaffold()
42
        mergedConfig PluginConfig
43
}
44

45
func (p *editSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
×
46
        subcmdMeta.Description = metaDataDescription
×
47

×
NEW
48
        subcmdMeta.Examples = fmt.Sprintf(`  # Edit a common project with this plugin (default: creates both Issues and PRs)
×
NEW
49
  %[1]s edit --plugins=%[2]s --force
×
50

×
51
  # Edit a common project with GitHub Models enabled (requires repo permissions)
×
NEW
52
  %[1]s edit --plugins=%[2]s --use-gh-models --force
×
NEW
53

×
NEW
54
  # Edit to create only PRs (no issue notifications)
×
NEW
55
  %[1]s edit --plugins=%[2]s --open-gh-issue=false --force
×
NEW
56

×
NEW
57
  # Edit to create only Issues (no PRs)
×
NEW
58
  %[1]s edit --plugins=%[2]s --open-gh-pr=false --force
×
59
`, cliMeta.CommandName, plugin.KeyFor(Plugin{}))
×
60
}
×
61

62
func (p *editSubcommand) BindFlags(fs *pflag.FlagSet) {
×
NEW
63
        p.flagSet = fs
×
64
        fs.BoolVar(&p.useGHModels, "use-gh-models", false,
×
65
                "If set, enable GitHub Models AI summary in the scaffolded workflow (requires GitHub Models permissions)")
×
NEW
66
        fs.BoolVar(&p.openGHIssue, "open-gh-issue", true,
×
NEW
67
                "By default, create GitHub Issues to notify about updates. Disable with --open-gh-issue=false")
×
NEW
68
        fs.BoolVar(&p.openGHPR, "open-gh-pr", true,
×
NEW
69
                "By default, create GitHub Pull Requests with the update changes. Disable with --open-gh-pr=false")
×
NEW
70
        fs.BoolVar(&p.force, "force", false,
×
NEW
71
                "If set, overwrite existing workflow file if present (manual edits will be lost)")
×
72
}
×
73

74
func (p *editSubcommand) InjectConfig(c config.Config) error {
2✔
75
        p.config = c
2✔
76
        return nil
2✔
77
}
2✔
78

79
func (p *editSubcommand) PreScaffold(machinery.Filesystem) error {
2✔
80
        if len(p.config.GetCliVersion()) == 0 {
3✔
81
                return fmt.Errorf(
1✔
82
                        "you must manually upgrade your project to a version that records the CLI version in PROJECT (`cliVersion`) " +
1✔
83
                                "to allow the `alpha update` command to work properly before using this plugin.\n" +
1✔
84
                                "More info: https://book.kubebuilder.io/migrations",
1✔
85
                )
1✔
86
        }
1✔
87
        return nil
1✔
88
}
89

90
func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
×
NEW
91
        // Validate that --use-gh-models requires --open-gh-pr
×
NEW
92
        // AI summaries only work with PRs
×
NEW
93
        if p.useGHModels && !p.openGHPR {
×
NEW
94
                return fmt.Errorf(
×
NEW
95
                        "the --use-gh-models flag requires --open-gh-pr=true " +
×
NEW
96
                                "(AI summaries only work with Pull Requests)")
×
NEW
97
        }
×
98

99
        // Merge with existing config: only update fields for flags that were explicitly set
NEW
100
        cfg := PluginConfig{
×
NEW
101
                // Set defaults for new projects
×
NEW
102
                OpenGHIssue: true,
×
NEW
103
                OpenGHPR:    true,
×
NEW
104
        }
×
NEW
105

×
NEW
106
        // Read existing config if it exists
×
NEW
107
        key := plugin.GetPluginKeyForConfig(p.config.GetPluginChain(), Plugin{})
×
NEW
108
        _ = p.config.DecodePluginConfig(key, &cfg)
×
NEW
109

×
NEW
110
        // Only update fields for flags that were explicitly set by the user.
×
NEW
111
        // Scaffold() may be invoked without BindFlags(), so guard access to p.flagSet.
×
NEW
112
        useGHModelsChanged := p.flagSet != nil && p.flagSet.Changed("use-gh-models")
×
NEW
113
        openGHIssueChanged := p.flagSet != nil && p.flagSet.Changed("open-gh-issue")
×
NEW
114
        openGHPRChanged := p.flagSet != nil && p.flagSet.Changed("open-gh-pr")
×
NEW
115
        if useGHModelsChanged {
×
NEW
116
                cfg.UseGHModels = p.useGHModels
×
NEW
117
        }
×
NEW
118
        if openGHIssueChanged {
×
NEW
119
                cfg.OpenGHIssue = p.openGHIssue
×
NEW
120
        }
×
NEW
121
        if openGHPRChanged {
×
NEW
122
                cfg.OpenGHPR = p.openGHPR
×
NEW
123
        }
×
124

125
        // Warn if user changed flags without --force (workflow won't be updated)
NEW
126
        if !p.force && (useGHModelsChanged || openGHIssueChanged || openGHPRChanged) {
×
NEW
127
                return fmt.Errorf(
×
NEW
128
                        "workflow file will not be updated without --force flag. " +
×
NEW
129
                                "Re-run with --force to regenerate .github/workflows/auto_update.yml with the new settings")
×
NEW
130
        }
×
131

NEW
132
        if err := insertPluginMetaToConfig(p.config, cfg); err != nil {
×
133
                return fmt.Errorf("error inserting project plugin meta to configuration: %w", err)
×
134
        }
×
135

136
        // Store merged config for PostScaffold()
NEW
137
        p.mergedConfig = cfg
×
NEW
138

×
NEW
139
        scaffolder := scaffolds.NewInitScaffolder(cfg.UseGHModels, cfg.OpenGHIssue, cfg.OpenGHPR, p.force)
×
140
        scaffolder.InjectFS(fs)
×
141
        if err := scaffolder.Scaffold(); err != nil {
×
142
                return fmt.Errorf("error scaffolding edit subcommand: %w", err)
×
143
        }
×
144

145
        return nil
×
146
}
147

148
func (p *editSubcommand) PostScaffold() error {
×
149
        // Inform users about GitHub Models if they didn't enable it
×
NEW
150
        // Note: AI summaries only work when PRs are enabled
×
NEW
151
        if !p.mergedConfig.UseGHModels && p.mergedConfig.OpenGHPR {
×
NEW
152
                log.Info("Consider enabling GitHub Models to get an AI summary in PRs")
×
153
                log.Info("Use the --use-gh-models flag if your project/organization has permission to use GitHub Models")
×
154
        }
×
155
        return nil
×
156
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc