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

kubernetes-sigs / kubebuilder / 17197426363

25 Aug 2025 02:29AM UTC coverage: 65.092% (-5.0%) from 70.093%
17197426363

push

github

web-flow
Merge pull request #5040 from camilamacedo86/add-gh-model

✨ (alpha update) Add Option to use AI with alpha update

53 of 414 new or added lines in 4 files covered. (12.8%)

1 existing line in 1 file now uncovered.

3196 of 4910 relevant lines covered (65.09%)

12.62 hits per line

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

75.86
/pkg/cli/alpha/update.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 alpha
18

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

24
        "github.com/spf13/cobra"
25

26
        "sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal/update"
27
        "sigs.k8s.io/kubebuilder/v4/pkg/logging"
28
)
29

30
func init() {
2✔
31
        // Initialize consistent logging for alpha commands
2✔
32
        opts := logging.HandlerOptions{
2✔
33
                SlogOpts: slog.HandlerOptions{
2✔
34
                        Level: slog.LevelInfo,
2✔
35
                },
2✔
36
        }
2✔
37
        handler := logging.NewHandler(os.Stdout, opts)
2✔
38
        logger := slog.New(handler)
2✔
39
        slog.SetDefault(logger)
2✔
40
}
2✔
41

42
// NewUpdateCommand creates and returns a new Cobra command for updating Kubebuilder projects.
43
func NewUpdateCommand() *cobra.Command {
2✔
44
        opts := update.Update{}
2✔
45
        var gitCfg []string
2✔
46
        updateCmd := &cobra.Command{
2✔
47
                Use:   "update",
2✔
48
                Short: "Update your project to a newer version (3-way merge; squash by default)",
2✔
49
                Long: `Upgrade your project scaffold using a 3-way merge while preserving your code.
2✔
50

2✔
51
The updater uses four temporary branches during the run:
2✔
52
  • ancestor : clean scaffold from the starting version (--from-version)
2✔
53
  • original : snapshot of your current project (--from-branch)
2✔
54
  • upgrade  : scaffold generated with the target version (--to-version)
2✔
55
  • merge    : result of merging original into upgrade (conflicts possible)
2✔
56

2✔
57
Output branch & history:
2✔
58
  • Default: SQUASH the merge result into ONE commit on:
2✔
59
        kubebuilder-update-from-<from-version>-to-<to-version>
2✔
60
  • --show-commits: keep full history (not compatible with --restore-path).
2✔
61

2✔
62
Conflicts:
2✔
63
  • Default: stop on conflicts and leave the merge branch for manual resolution.
2✔
64
  • --force: commit with conflict markers so automation can proceed.
2✔
65

2✔
66
Other options:
2✔
67
  • --restore-path: restore paths from base when squashing (e.g., CI configs).
2✔
68
  • --output-branch: override the output branch name.
2✔
69
  • --push: push the output branch to 'origin' after the update.
2✔
70
  • --git-config: pass per-invocation Git config as -c key=value (repeatable). When not set,
2✔
71
      defaults are set to improve detection during merges.
2✔
72

2✔
73
Defaults:
2✔
74
  • --from-version / --to-version: resolved from PROJECT and the latest release if unset.
2✔
75
  • --from-branch: defaults to 'main' if not specified.`,
2✔
76
                Example: `
2✔
77
  # Update from the version in PROJECT to the latest, stop on conflicts
2✔
78
  kubebuilder alpha update
2✔
79

2✔
80
  # Update from a specific version to latest
2✔
81
  kubebuilder alpha update --from-version v4.6.0
2✔
82

2✔
83
  # Update from v4.5.0 to v4.7.0 and keep conflict markers (automation-friendly)
2✔
84
  kubebuilder alpha update --from-version v4.5.0 --to-version v4.7.0 --force
2✔
85

2✔
86
  # Keep full commit history instead of squashing
2✔
87
  kubebuilder alpha update --from-version v4.5.0 --to-version v4.7.0 --force --show-commits
2✔
88

2✔
89
  # Squash while preserving CI workflows from base (e.g., main)
2✔
90
  kubebuilder alpha update --force --restore-path .github/workflows
2✔
91

2✔
92
  # Show commits into a custom output branch name
2✔
93
  kubebuilder alpha update --force --show-commits --output-branch my-update-branch
2✔
94

2✔
95
  # Run update and push the output branch to origin (works with or without --show-commits)
2✔
96
  kubebuilder alpha update --from-version v4.6.0 --to-version v4.7.0 --force --push
2✔
97

2✔
98
  # Create an issue and add an AI overview comment
2✔
99
  kubebuilder alpha update --open-gh-issue --use-gh-models
2✔
100

2✔
101
  # Add extra Git configs (no need to re-specify defaults)
2✔
102
  kubebuilder alpha update --git-config merge.conflictStyle=diff3 --git-config rerere.enabled=true
2✔
103
                                          
2✔
104
  # Disable Git config defaults completely, use only custom configs
2✔
105
  kubebuilder alpha update --git-config disable --git-config rerere.enabled=true`,
2✔
106
                PreRunE: func(_ *cobra.Command, _ []string) error {
2✔
107
                        if opts.ShowCommits && len(opts.RestorePath) > 0 {
×
108
                                return fmt.Errorf("the --restore-path flag is not supported with --show-commits")
×
109
                        }
×
110

NEW
111
                        if opts.UseGhModels && !opts.OpenGhIssue {
×
NEW
112
                                return fmt.Errorf("the --use-gh-models requires --open-gh-issue to be set")
×
NEW
113
                        }
×
114

115
                        // Defaults always on unless "disable" is present anywhere
116
                        defaults := []string{
×
117
                                "merge.renameLimit=999999",
×
118
                                "diff.renameLimit=999999",
×
119
                                "merge.conflictStyle=merge",
×
120
                        }
×
121

×
122
                        hasDisable := false
×
123
                        filtered := make([]string, 0, len(gitCfg))
×
124
                        for _, v := range gitCfg {
×
125
                                if v == "disable" {
×
126
                                        hasDisable = true
×
127
                                        continue
×
128
                                }
129
                                filtered = append(filtered, v)
×
130
                        }
131

132
                        if hasDisable {
×
133
                                // no defaults; only user-provided configs (excluding "disable")
×
134
                                opts.GitConfig = filtered
×
135
                        } else {
×
136
                                // defaults + user configs (user can override by repeating keys)
×
137
                                opts.GitConfig = append(defaults, filtered...)
×
138
                        }
×
139

140
                        if err := opts.Prepare(); err != nil {
×
141
                                return fmt.Errorf("failed to prepare update: %w", err)
×
142
                        }
×
143
                        return opts.Validate()
×
144
                },
145
                Run: func(_ *cobra.Command, _ []string) {
×
146
                        if err := opts.Update(); err != nil {
×
147
                                slog.Error("Update failed", "error", err)
×
148
                                os.Exit(1)
×
149
                        }
×
150
                },
151
        }
152

153
        updateCmd.Flags().StringVar(&opts.FromVersion, "from-version", "",
2✔
154
                "binary release version to upgrade from. Should match the version used to init the project and be "+
2✔
155
                        "a valid release version, e.g., v4.6.0. If not set, it defaults to the version specified in the PROJECT file.")
2✔
156
        updateCmd.Flags().StringVar(&opts.ToVersion, "to-version", "",
2✔
157
                "binary release version to upgrade to. Should be a valid release version, e.g., v4.7.0. "+
2✔
158
                        "If not set, it defaults to the latest release version available in the project repository.")
2✔
159
        updateCmd.Flags().StringVar(&opts.FromBranch, "from-branch", "",
2✔
160
                "Git branch to use as current state of the project for the update.")
2✔
161
        updateCmd.Flags().BoolVar(&opts.Force, "force", false,
2✔
162
                "Force the update even if conflicts occur. Conflicted files will include conflict markers, and a "+
2✔
163
                        "commit will be created automatically. Ideal for automation (e.g., cronjobs, CI).")
2✔
164
        updateCmd.Flags().BoolVar(&opts.ShowCommits, "show-commits", false,
2✔
165
                "If set, the update will keep the full history instead of squashing into a single commit.")
2✔
166
        updateCmd.Flags().StringArrayVar(&opts.RestorePath, "restore-path", nil,
2✔
167
                "Paths to preserve from the base branch (repeatable). Not supported with --show-commits.")
2✔
168
        updateCmd.Flags().StringVar(&opts.OutputBranch, "output-branch", "",
2✔
169
                "Override the default output branch name (default: kubebuilder-update-from-<from-version>-to-<to-version>).")
2✔
170
        updateCmd.Flags().BoolVar(&opts.Push, "push", false,
2✔
171
                "Push the output branch to the remote repository after the update.")
2✔
172
        updateCmd.Flags().BoolVar(&opts.OpenGhIssue, "open-gh-issue", false,
2✔
173
                "Create a GitHub issue with a pre-filled checklist and compare link after the update completes (requires `gh`).")
2✔
174
        updateCmd.Flags().BoolVar(
2✔
175
                &opts.UseGhModels,
2✔
176
                "use-gh-models",
2✔
177
                false,
2✔
178
                "Generate and post an AI summary comment to the GitHub Issue using `gh models run`. "+
2✔
179
                        "Requires --open-gh-issue and GitHub CLI (`gh`) with the `gh-models` extension.")
2✔
180
        updateCmd.Flags().StringArrayVar(
2✔
181
                &gitCfg,
2✔
182
                "git-config",
2✔
183
                nil,
2✔
184
                "Per-invocation Git config (repeatable). "+
2✔
185
                        "Defaults: -c merge.renameLimit=999999 -c diff.renameLimit=999999 -c merge.conflictStyle=merge. "+
2✔
186
                        "Your configs are applied on top. To disable defaults, include `--git-config disable`")
2✔
187
        return updateCmd
2✔
188
}
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