GeezerVison is showing github.com — feat: protect existing workflows from overwrites · knative/func@8162643
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ __pycache__ | 35 | 35 | AGENTS.override.md | 36 | 36 | .claude/*.local.* | 37 | 37 | .claude/pr/* | | 38 | +.roo/* | 38 | 39 |
| 39 | 40 | # E2E Tests | 40 | 41 | /e2e/testdata/default_home/go | |
Collapse file Copy file name to clipboardExpand all lines: AGENTS.md +8-2Lines changed: 8 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -18,10 +18,12 @@ edge cases into account. | 18 | 18 | ## Commands | 19 | 19 |
| 20 | 20 | ### Build & Development | | 21 | + | 21 | 22 | - `make all` - Build binary and regenerate docs | 22 | 23 | - `make build` - Build for current OS | 23 | 24 |
| 24 | 25 | ### Testing strategy reference | | 26 | + | 25 | 27 | Before committing, test locally following the table below: | 26 | 28 |
| 27 | 29 | | If changed | Target | Description | | @@ -32,26 +34,29 @@ Before committing, test locally following the table below: | 32 | 34 | | Significant architectural changes | `make test-full` | e2e tests (cluster required - read `CONTRIBUTING.md`) | | 33 | 35 |
| 34 | 36 | ### Generated Files | | 37 | + | 35 | 38 | - `./hack/update-codegen.sh` - update embedded filesystem & regenerate docs | 36 | 39 | - `make check-embedded-fs` - check embedded FS is up to date with templates | 37 | 40 |
| 38 | | - | 39 | 41 | ## Boundaries | 40 | 42 |
| 41 | 43 | ### Always Do | | 44 | + | 42 | 45 | - Run `make test` before considering any change complete | 43 | 46 | - Run `make check` before commits | 44 | 47 | - Run `make check-embedded-fs` after modifying `templates/` | 45 | 48 | - Ask before deleting ANY file or significant code block | 46 | 49 |
| 47 | 50 | ### Ask First | | 51 | + | 48 | 52 | - Security-related code changes (authentication, credentials, secrets handling) | 49 | 53 | - API changes | 50 | 54 | - Adding new dependencies | 51 | 55 | - Modifying CI/GitHub Actions workflows | 52 | 56 | - Architectural decisions | 53 | 57 |
| 54 | 58 | ### Never Do | | 59 | + | 55 | 60 | - Edit generated files directly: | 56 | 61 | - `generate/zz_filesystem_generated.go` | 57 | 62 | - `schema/func_yaml-schema.json` | @@ -63,12 +68,13 @@ Before committing, test locally following the table below: | 63 | 68 | ## Common Pitfalls | 64 | 69 |
| 65 | 70 | ### Codegen Sync | | 71 | + | 66 | 72 | After modifying `templates/` or making documentation changes, you MUST run: | | 73 | + | 67 | 74 | ```bash | 68 | 75 | ./hack/update-codegen.sh | 69 | 76 | ``` | 70 | 77 |
| 71 | | - | 72 | 78 | ## Contributing | 73 | 79 |
| 74 | 80 | If creating a contribution to this project on GitHub suggest to user reading | |
Collapse file Copy file name to clipboardExpand all lines: cmd/ci/config.go +75-21Lines changed: 75 additions & 21 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,8 +23,9 @@ const ( | 23 | 23 | BranchFlag = "branch" | 24 | 24 | DefaultBranch = "main" | 25 | 25 |
| 26 | | - WorkflowNameFlag = "workflow-name" | 27 | | - DefaultWorkflowName = "Func Deploy" | | 26 | + WorkflowNameFlag = "workflow-name" | | 27 | + DefaultWorkflowName = "Func Deploy" | | 28 | + DefaultRemoteBuildWorkflowName = "Remote " + DefaultWorkflowName | 28 | 29 |
| 29 | 30 | KubeconfigSecretNameFlag = "kubeconfig-secret-name" | 30 | 31 | DefaultKubeconfigSecretName = "KUBECONFIG" | @@ -52,6 +53,9 @@ const ( | 52 | 53 |
| 53 | 54 | UseSelfHostedRunnerFlag = "self-hosted-runner" | 54 | 55 | DefaultUseSelfHostedRunner = false | | 56 | + | | 57 | + ForceFlag = "force" | | 58 | + DefaultForce = false | 55 | 59 | ) | 56 | 60 |
| 57 | 61 | // CIConfig readonly configuration | @@ -69,42 +73,36 @@ type CIConfig struct { | 69 | 73 | useRegistryLogin, | 70 | 74 | useSelfHostedRunner, | 71 | 75 | useRemoteBuild, | 72 | | - useWorkflowDispatch bool | | 76 | + useWorkflowDispatch, | | 77 | + force bool | 73 | 78 | } | 74 | 79 |
| 75 | 80 | func NewCIConfig( | 76 | 81 | currentBranch common.CurrentBranchFunc, | 77 | 82 | workingDir common.WorkDirFunc, | 78 | 83 | ) (CIConfig, error) { | 79 | | - platform := viper.GetString(PlatformFlag) | 80 | | - if strings.ToLower(platform) != DefaultPlatform { | 81 | | - return CIConfig{}, fmt.Errorf("%s support is not implemented", platform) | | 84 | + if err := resolvePlatform(); err != nil { | | 85 | + return CIConfig{}, err | 82 | 86 | } | 83 | 87 |
| 84 | | - path := viper.GetString(PathFlag) | 85 | | - if path == "" || path == "." { | 86 | | - cwd, err := workingDir() | 87 | | - if err != nil { | 88 | | - return CIConfig{}, err | 89 | | - } | 90 | | - path = cwd | | 88 | + path, err := resolvePath(workingDir) | | 89 | + if err != nil { | | 90 | + return CIConfig{}, err | 91 | 91 | } | 92 | 92 |
| 93 | | - branch := viper.GetString(BranchFlag) | 94 | | - if branch == "" { | 95 | | - var err error | 96 | | - branch, err = currentBranch(path) | 97 | | - if err != nil { | 98 | | - return CIConfig{}, err | 99 | | - } | | 93 | + branch, err := resolveBranch(path, currentBranch) | | 94 | + if err != nil { | | 95 | + return CIConfig{}, err | 100 | 96 | } | 101 | 97 |
| | 98 | + workflowName := resolveWorkflowName() | | 99 | + | 102 | 100 | return CIConfig{ | 103 | 101 | githubWorkflowDir: DefaultGitHubWorkflowDir, | 104 | 102 | githubWorkflowFilename: DefaultGitHubWorkflowFilename, | 105 | 103 | path: path, | 106 | 104 | branch: branch, | 107 | | - workflowName: viper.GetString(WorkflowNameFlag), | | 105 | + workflowName: workflowName, | 108 | 106 | kubeconfigSecret: viper.GetString(KubeconfigSecretNameFlag), | 109 | 107 | registryLoginUrlVar: viper.GetString(RegistryLoginUrlVariableNameFlag), | 110 | 108 | registryUserVar: viper.GetString(RegistryUserVariableNameFlag), | @@ -114,9 +112,61 @@ func NewCIConfig( | 114 | 112 | useSelfHostedRunner: viper.GetBool(UseSelfHostedRunnerFlag), | 115 | 113 | useRemoteBuild: viper.GetBool(UseRemoteBuildFlag), | 116 | 114 | useWorkflowDispatch: viper.GetBool(WorkflowDispatchFlag), | | 115 | + force: viper.GetBool(ForceFlag), | 117 | 116 | }, nil | 118 | 117 | } | 119 | 118 |
| | 119 | +func resolvePlatform() error { | | 120 | + platform := viper.GetString(PlatformFlag) | | 121 | + if strings.ToLower(platform) != DefaultPlatform { | | 122 | + return fmt.Errorf("%s support is not implemented", platform) | | 123 | + } | | 124 | + | | 125 | + return nil | | 126 | +} | | 127 | + | | 128 | +func resolvePath(workingDir common.WorkDirFunc) (string, error) { | | 129 | + path := viper.GetString(PathFlag) | | 130 | + if path != "" && path != "." { | | 131 | + return path, nil | | 132 | + } | | 133 | + | | 134 | + cwd, err := workingDir() | | 135 | + if err != nil { | | 136 | + return "", err | | 137 | + } | | 138 | + | | 139 | + return cwd, nil | | 140 | +} | | 141 | + | | 142 | +func resolveBranch(path string, currentBranch common.CurrentBranchFunc) (string, error) { | | 143 | + branch := viper.GetString(BranchFlag) | | 144 | + if branch != "" { | | 145 | + return branch, nil | | 146 | + } | | 147 | + | | 148 | + branch, err := currentBranch(path) | | 149 | + if err != nil { | | 150 | + return "", err | | 151 | + } | | 152 | + | | 153 | + return branch, nil | | 154 | +} | | 155 | + | | 156 | +func resolveWorkflowName() string { | | 157 | + workflowName := viper.GetString(WorkflowNameFlag) | | 158 | + if workflowName != DefaultWorkflowName && workflowName != "" { | | 159 | + return workflowName | | 160 | + } | | 161 | + | | 162 | + useRemoteBuild := viper.GetBool(UseRemoteBuildFlag) | | 163 | + if useRemoteBuild { | | 164 | + return DefaultRemoteBuildWorkflowName | | 165 | + } | | 166 | + | | 167 | + return DefaultWorkflowName | | 168 | +} | | 169 | + | 120 | 170 | func (cc *CIConfig) FnGitHubWorkflowDir(fnRoot string) string { | 121 | 171 | return filepath.Join(fnRoot, cc.githubWorkflowDir) | 122 | 172 | } | @@ -172,3 +222,7 @@ func (cc *CIConfig) RegistryPassSecret() string { | 172 | 222 | func (cc *CIConfig) RegistryUrlVar() string { | 173 | 223 | return cc.registryUrlVar | 174 | 224 | } | | 225 | + | | 226 | +func (cc *CIConfig) Force() bool { | | 227 | + return cc.force | | 228 | +} |
Collapse file Copy file name to clipboardExpand all lines: cmd/ci/workflow.go +18-12Lines changed: 18 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,11 +2,18 @@ package ci | 2 | 2 |
| 3 | 3 | import ( | 4 | 4 | "bytes" | | 5 | + "errors" | 5 | 6 | "fmt" | 6 | 7 |
| 7 | 8 | "gopkg.in/yaml.v3" | | 9 | + "knative.dev/func/cmd/common" | 8 | 10 | ) | 9 | 11 |
| | 12 | +const defaultFuncCliVersion = "knative-v1.21.0" | | 13 | + | | 14 | +// ErrWorkflowExists is returned when a GitHub workflow file already exists and --force is not specified. | | 15 | +var ErrWorkflowExists = errors.New("existing GitHub workflow detected, overwrite using the --force option") | | 16 | + | 10 | 17 | type githubWorkflow struct { | 11 | 18 | Name string `yaml:"name"` | 12 | 19 | On workflowTriggers `yaml:"on"` | @@ -35,7 +42,7 @@ type step struct { | 35 | 42 | } | 36 | 43 |
| 37 | 44 | func NewGitHubWorkflow(conf CIConfig) *githubWorkflow { | 38 | | - name := createWorkflowName(conf) | | 45 | + name := conf.WorkflowName() | 39 | 46 | runsOn := createRunsOn(conf) | 40 | 47 | pushTrigger := createPushTrigger(conf) | 41 | 48 |
| @@ -58,15 +65,6 @@ func NewGitHubWorkflow(conf CIConfig) *githubWorkflow { | 58 | 65 | } | 59 | 66 | } | 60 | 67 |
| 61 | | -func createWorkflowName(conf CIConfig) string { | 62 | | - result := conf.WorkflowName() | 63 | | - if conf.UseRemoteBuild() { | 64 | | - result = "Remote Func Deploy" | 65 | | - } | 66 | | - | 67 | | - return result | 68 | | -} | 69 | | - | 70 | 68 | func createRunsOn(conf CIConfig) string { | 71 | 69 | runsOn := "ubuntu-latest" | 72 | 70 | if conf.UseSelfHostedRunner() { | @@ -120,7 +118,7 @@ func createRegistryLoginStep(conf CIConfig, steps []step) []step { | 120 | 118 | func createFuncCLIInstallStep(steps []step) []step { | 121 | 119 | installFuncCli := newStep("Install func cli"). | 122 | 120 | withUses("functions-dev/action@main"). | 123 | | - withActionConfig("version", "knative-v1.20.1"). | | 121 | + withActionConfig("version", defaultFuncCliVersion). | 124 | 122 | withActionConfig("name", "func") | 125 | 123 |
| 126 | 124 | return append(steps, *installFuncCli) | @@ -174,7 +172,15 @@ func newVariable(key string) string { | 174 | 172 | return fmt.Sprintf("${{ vars.%s }}", key) | 175 | 173 | } | 176 | 174 |
| 177 | | -func (gw *githubWorkflow) Export(path string, w WorkflowWriter) error { | | 175 | +func (gw *githubWorkflow) Export(path string, w WorkflowWriter, force bool, logger common.Logger) error { | | 176 | + if !force && w.Exist(path) { | | 177 | + return ErrWorkflowExists | | 178 | + } | | 179 | + | | 180 | + if w.Exist(path) { | | 181 | + logger.Info("WARNING: --force flag is set, overwriting existing GitHub Workflow file") | | 182 | + } | | 183 | + | 178 | 184 | raw, err := gw.toYaml() | 179 | 185 | if err != nil { | 180 | 186 | return err | |
Collapse file Copy file name to clipboardExpand all lines: cmd/ci/workflow_test.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,7 @@ func TestGitHubWorkflow_Export(t *testing.T) { | 19 | 19 | bufferWriter := ci.NewBufferWriter() | 20 | 20 |
| 21 | 21 | // WHEN | 22 | | - exportErr := gw.Export("path", bufferWriter) | | 22 | + exportErr := gw.Export("path", bufferWriter, true, common.NewBufferLogger()) | 23 | 23 |
| 24 | 24 | // THEN | 25 | 25 | assert.NilError(t, exportErr, "unexpected error when exporting GitHub Workflow") | |
Collapse file Copy file name to clipboardExpand all lines: cmd/ci/writer.go +22-8Lines changed: 22 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,15 +7,16 @@ import ( | 7 | 7 | ) | 8 | 8 |
| 9 | 9 | const ( | 10 | | - dirPerm = 0755 // o: rwx, g|u: r-x | 11 | | - filePerm = 0644 // o: rw, g|u: r | | 10 | + dirPerm = 0755 // u: rwx, g: r-x, o: r-x | | 11 | + filePerm = 0644 // u: rw-, g: r--, o: r-- | 12 | 12 | ) | 13 | 13 |
| 14 | 14 | // DefaultWorkflowWriter is the default implementation for writing workflow files to disk. | 15 | 15 | var DefaultWorkflowWriter = &fileWriter{} | 16 | 16 |
| 17 | 17 | // WorkflowWriter defines the interface for writing workflow files. | 18 | 18 | type WorkflowWriter interface { | | 19 | + Exist(path string) bool | 19 | 20 | Write(path string, raw []byte) error | 20 | 21 | } | 21 | 22 |
| @@ -34,19 +35,32 @@ func (fw *fileWriter) Write(path string, raw []byte) error { | 34 | 35 | return nil | 35 | 36 | } | 36 | 37 |
| 37 | | -type bufferWriter struct { | | 38 | +func (fw *fileWriter) Exist(path string) bool { | | 39 | + _, err := os.Stat(path) | | 40 | + return err == nil | | 41 | +} | | 42 | + | | 43 | +// BufferWriter is a test double (fake) that implements WorkflowWriter | | 44 | +// by writing to an in-memory buffer instead of the filesystem. | | 45 | +type BufferWriter struct { | 38 | 46 | Path string | 39 | 47 | Buffer *bytes.Buffer | 40 | 48 | } | 41 | 49 |
| 42 | | -// NewBufferWriter creates a new bufferWriter for testing purposes. | 43 | | -func NewBufferWriter() *bufferWriter { | 44 | | - return &bufferWriter{Buffer: &bytes.Buffer{}} | | 50 | +// NewBufferWriter creates a new BufferWriter test double. | | 51 | +func NewBufferWriter() *BufferWriter { | | 52 | + return &BufferWriter{Buffer: &bytes.Buffer{}} | 45 | 53 | } | 46 | 54 |
| 47 | | -// Write stores the path and writes raw bytes to the internal buffer. | 48 | | -func (bw *bufferWriter) Write(path string, raw []byte) error { | | 55 | +// Write is a fake implementation that stores content in the buffer. | | 56 | +func (bw *BufferWriter) Write(path string, raw []byte) error { | 49 | 57 | bw.Path = path | | 58 | + bw.Buffer.Reset() | 50 | 59 | _, err := bw.Buffer.Write(raw) | 51 | 60 | return err | 52 | 61 | } | | 62 | + | | 63 | +// Exist is a fake implementation that returns true if the buffer has content. | | 64 | +func (bw *BufferWriter) Exist(_ string) bool { | | 65 | + return bw.Buffer != nil && bw.Buffer.Len() > 0 | | 66 | +} |
|