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

kubernetes-sigs / kubebuilder / 25008864153

27 Apr 2026 05:07PM UTC coverage: 81.815%. First build
25008864153

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 64 new or added lines in 2 files covered. (0.0%)

7707 of 9420 relevant lines covered (81.82%)

73.23 hits per line

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

13.13
/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
        flagSet     *pflag.FlagSet
39

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

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

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

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

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

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

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

71
func (p *editSubcommand) InjectConfig(c config.Config) error {
2✔
72
        p.config = c
2✔
73
        return nil
2✔
74
}
2✔
75

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

87
func (p *editSubcommand) Scaffold(fs machinery.Filesystem) error {
×
NEW
88
        // Merge with existing config: only update fields for flags that were explicitly set
×
NEW
89
        cfg := PluginConfig{
×
NEW
90
                // Set defaults for new projects
×
NEW
91
                OpenGHIssue: true,
×
NEW
92
                OpenGHPR:    true,
×
NEW
93
        }
×
NEW
94

×
NEW
95
        // Read existing config if it exists
×
NEW
96
        key := plugin.GetPluginKeyForConfig(p.config.GetPluginChain(), Plugin{})
×
NEW
97
        _ = p.config.DecodePluginConfig(key, &cfg)
×
NEW
98

×
NEW
99
        // Scaffold() may be invoked without BindFlags(). In that case, fall back to
×
NEW
100
        // applying the current values on the subcommand directly.
×
NEW
101
        useGHModelsChanged := false
×
NEW
102
        openGHIssueChanged := false
×
NEW
103
        openGHPRChanged := false
×
NEW
104
        if p.flagSet != nil {
×
NEW
105
                useGHModelsChanged = p.flagSet.Changed("use-gh-models")
×
NEW
106
                openGHIssueChanged = p.flagSet.Changed("open-gh-issue")
×
NEW
107
                openGHPRChanged = p.flagSet.Changed("open-gh-pr")
×
NEW
108
                if useGHModelsChanged {
×
NEW
109
                        cfg.UseGHModels = p.useGHModels
×
NEW
110
                }
×
NEW
111
                if openGHIssueChanged {
×
NEW
112
                        cfg.OpenGHIssue = p.openGHIssue
×
NEW
113
                }
×
NEW
114
                if openGHPRChanged {
×
NEW
115
                        cfg.OpenGHPR = p.openGHPR
×
NEW
116
                }
×
NEW
117
        } else {
×
NEW
118
                // No flag set, apply all current values directly
×
NEW
119
                cfg.UseGHModels = p.useGHModels
×
NEW
120
                cfg.OpenGHIssue = p.openGHIssue
×
NEW
121
                cfg.OpenGHPR = p.openGHPR
×
NEW
122
        }
×
123

124
        // Validate the merged config: --use-gh-models requires --open-gh-pr
125
        // AI summaries only work with PRs
NEW
126
        if cfg.UseGHModels && !cfg.OpenGHPR {
×
NEW
127
                return fmt.Errorf(
×
NEW
128
                        "the --use-gh-models flag requires --open-gh-pr=true " +
×
NEW
129
                                "(AI summaries only work with Pull Requests)")
×
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
        // Always overwrite the workflow file to keep it in sync with configuration
×
NEW
140
        scaffolder := scaffolds.NewInitScaffolder(cfg.UseGHModels, cfg.OpenGHIssue, cfg.OpenGHPR)
×
141
        scaffolder.InjectFS(fs)
×
142
        if err := scaffolder.Scaffold(); err != nil {
×
143
                return fmt.Errorf("error scaffolding edit subcommand: %w", err)
×
144
        }
×
145

146
        return nil
×
147
}
148

149
func (p *editSubcommand) PostScaffold() error {
×
150
        // Inform users about GitHub Models if they didn't enable it
×
NEW
151
        // Note: AI summaries only work when PRs are enabled
×
NEW
152
        if !p.mergedConfig.UseGHModels && p.mergedConfig.OpenGHPR {
×
NEW
153
                log.Info("Consider enabling GitHub Models to get an AI summary in PRs")
×
154
                log.Info("Use the --use-gh-models flag if your project/organization has permission to use GitHub Models")
×
155
        }
×
156
        return nil
×
157
}
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