1
0
mirror of https://codeberg.org/crowci/crow.git synced 2025-08-06 09:22:46 +03:00
Files
crow/cli/repo/repo_update.go
pat-s 901a614246 feat: add log purge setting as repo setting (#34)
follow-up #10 (which was a git hickup)

![image](/attachments/a7655406-b492-4912-873b-d3d6f692be9e)

![image](/attachments/fe365dce-65dd-40cc-aeb8-3d9ca95a52ad)

## Behaviour

Log purging is attempted in an asynchronous process before a new pipeline of a specific repo is started.
It does so by

1. Getting all existing pipelines
2. Filtering by `keepMin` and `keepDuration` settings
3. Calling `LogDelete` for all remaining pipelines

Deleting only logs instead of the full pipeline (which `crow-cli pipeline purge` does) is preferred to keep historic pipeline information. Storing this in the DB is just a single line and doesn't contain much content (in contrast to logs).

## Defaults

- No minimum count is kept (`CROW_DEFAULT_LOGS_PIPELINES_KEEP_MIN`)
- All pipelines of the last 90 days (per repo) are kept (`CROW_DEFAULT_LOGS_KEEP_DURATION`)

## Todo

- [x] implement purge call during pipeline start
- [x] add settings to DB column and repo settings
- [x] tests
- [x] think about defaults
- [x] Currently the purge happens on all pipelines in scope, including ones which have already been cleared. To avoid these unnecessary calls, which also will add up for repos with many pipelines, an indicator is needed which allows filtering these pipelines out.

fix #9

Co-authored-by: crowci-bot <admin@crowci.dev>
Reviewed-on: https://codeberg.org/crowci/crow/pulls/34
Co-authored-by: pat-s <patrick.schratz@gmail.com>
Co-committed-by: pat-s <patrick.schratz@gmail.com>
2025-02-19 21:19:13 +00:00

152 lines
3.9 KiB
Go

// Copyright 2022 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package repo
import (
"context"
"fmt"
"time"
"github.com/urfave/cli/v3"
"codeberg.org/crowci/crow/v3/cli/internal"
crow "codeberg.org/crowci/crow/v3/crow-go/crow"
)
var repoUpdateCmd = &cli.Command{
Name: "update",
Usage: "update a repository",
ArgsUsage: "<repo-id|repo-full-name>",
Action: repoUpdate,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "trusted",
Usage: "repository is trusted",
},
&cli.BoolFlag{
Name: "gated", // TODO: remove in next release
Hidden: true,
},
&cli.StringFlag{
Name: "require-approval",
Usage: "repository requires approval for",
},
&cli.DurationFlag{
Name: "timeout",
Usage: "repository timeout",
},
&cli.StringFlag{
Name: "visibility",
Usage: "repository visibility",
},
&cli.StringFlag{
Name: "config",
Usage: "repository configuration path. Example: .woodpecker.yml",
},
&cli.IntFlag{
Name: "pipeline-counter",
Usage: "repository starting pipeline number",
},
&cli.BoolFlag{
Name: "unsafe",
Usage: "allow unsafe operations",
},
&cli.IntFlag{
Name: "logs-pipelines-keep-min",
Usage: "minimum amount of pipelines to keep logs for",
},
&cli.StringFlag{
Name: "logs-duration-keep",
Usage: "maximum age of pipeline logs to keep",
},
},
}
func repoUpdate(ctx context.Context, c *cli.Command) error {
repoIDOrFullName := c.Args().First()
client, err := internal.NewClient(ctx, c)
if err != nil {
return err
}
repoID, err := internal.ParseRepo(client, repoIDOrFullName)
if err != nil {
return err
}
var (
visibility = c.String("visibility")
config = c.String("config")
timeout = c.Duration("timeout")
LogsPipelinesKeepMin = int(c.Int("logs-pipelines-keep-min"))
LogsDurationKeep = c.String("logs-duration-keep")
trusted = c.Bool("trusted")
requireApproval = c.String("require-approval")
pipelineCounter = int(c.Int("pipeline-counter"))
unsafe = c.Bool("unsafe")
)
patch := new(crow.RepoPatch)
if c.IsSet("trusted") {
patch.IsTrusted = &trusted
}
// TODO: remove in next release
if c.IsSet("gated") {
return fmt.Errorf("'gated' option has been set in version 2.8, use 'require-approval' in >= 3.0")
}
if c.IsSet("require-approval") {
if mode := crow.ApprovalMode(requireApproval); mode.Valid() {
patch.RequireApproval = &mode
} else {
return fmt.Errorf("update approval mode failed: '%s' is no valid mode", mode)
}
}
if c.IsSet("timeout") {
v := int64(timeout / time.Minute)
patch.Timeout = &v
}
if c.IsSet("LogsPipelinesKeepMin") {
v := int64(LogsPipelinesKeepMin)
patch.LogsPipelinesKeepMin = &v
}
if c.IsSet("LogsDurationKeep") {
patch.LogsDurationKeep = &LogsDurationKeep
}
if c.IsSet("config") {
patch.Config = &config
}
if c.IsSet("visibility") {
switch visibility {
case "public", "private", "internal":
patch.Visibility = &visibility
}
}
if c.IsSet("pipeline-counter") && !unsafe {
fmt.Printf("Setting the pipeline counter is an unsafe operation that could put your repository in an inconsistent state. Please use --unsafe to proceed")
}
if c.IsSet("pipeline-counter") && unsafe {
patch.PipelineCounter = &pipelineCounter
}
repo, err := client.RepoPatch(repoID, patch)
if err != nil {
return err
}
fmt.Printf("Successfully updated repository %s\n", repo.FullName)
return nil
}