From afe63d8f324484b23fab3489a84179d33d1c3753 Mon Sep 17 00:00:00 2001 From: Marius Niveri Date: Mon, 14 Apr 2025 12:19:19 +0200 Subject: [PATCH 1/4] nix-module: Add option "allowForcePushMain" --- nix/module-options.nix | 5 +++++ nix/module.nix | 1 + 2 files changed, 6 insertions(+) diff --git a/nix/module-options.nix b/nix/module-options.nix index 5336e582..0b428535 100644 --- a/nix/module-options.nix +++ b/nix/module-options.nix @@ -174,6 +174,11 @@ type = listOf str; default = []; }; + allowForcePushMain = mkOption { + description = "Switch to configuration even when a force-push was detected on main."; + type = bool; + default = false; + }; }; }; } diff --git a/nix/module.nix b/nix/module.nix index 697d4dc9..5aca8803 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -12,6 +12,7 @@ let port = cfg.services.comin.exporter.port; }; gpg_public_key_paths = cfg.services.comin.gpgPublicKeyPaths; + allow_force_push_main = cfg.services.comin.allowForcePushMain; }; cominConfigYaml = yaml.generate "comin.yaml" cominConfig; From c862f3949d12259f6169abbd4e93f0403921d898 Mon Sep 17 00:00:00 2001 From: Marius Niveri Date: Mon, 14 Apr 2025 12:19:45 +0200 Subject: [PATCH 2/4] internal: ignore force-push when option "allowForcePushMain" is true --- internal/config/config.go | 9 +++++---- internal/repository/git.go | 6 +++++- internal/types/types.go | 24 +++++++++++++----------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 861ee4ca..f58b7751 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -58,9 +58,10 @@ func Read(path string) (config types.Configuration, err error) { func MkGitConfig(config types.Configuration) types.GitConfig { return types.GitConfig{ - Path: filepath.Join(config.StateDir, "repository"), - Dir: config.FlakeSubdirectory, - Remotes: config.Remotes, - GpgPublicKeyPaths: config.GpgPublicKeyPaths, + Path: filepath.Join(config.StateDir, "repository"), + Dir: config.FlakeSubdirectory, + Remotes: config.Remotes, + GpgPublicKeyPaths: config.GpgPublicKeyPaths, + AllowForcePushMain: config.AllowForcePushMain, } } diff --git a/internal/repository/git.go b/internal/repository/git.go index 4c20ba1e..69661539 100644 --- a/internal/repository/git.go +++ b/internal/repository/git.go @@ -85,7 +85,11 @@ func getHeadFromRemoteAndBranch(r repository, remoteName, branchName, currentMai } if err = hasNotBeenHardReset(r, branchName, currentMainHash, head); err != nil { - return + if r.GitConfig.AllowForcePushMain { + logrus.Infof("Force-push detected but ignored due to 'allowForcePushMain' being set") + } else { + return + } } commitObject, err := r.Repository.CommitObject(*head) diff --git a/internal/types/types.go b/internal/types/types.go index df84c7af..46a794f0 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -18,9 +18,10 @@ type GitConfig struct { // The repository Path Path string // The directory in the repository - Dir string - Remotes []Remote - GpgPublicKeyPaths []string + Dir string + Remotes []Remote + GpgPublicKeyPaths []string + AllowForcePushMain bool } type Auth struct { @@ -45,12 +46,13 @@ type HttpServer struct { } type Configuration struct { - Hostname string `yaml:"hostname"` - StateDir string `yaml:"state_dir"` - StateFilepath string `yaml:"state_filepath"` - FlakeSubdirectory string `yaml:"flake_subdirectory"` - Remotes []Remote `yaml:"remotes"` - ApiServer HttpServer `yaml:"api_server"` - Exporter HttpServer `yaml:"exporter"` - GpgPublicKeyPaths []string `yaml:"gpg_public_key_paths"` + Hostname string `yaml:"hostname"` + StateDir string `yaml:"state_dir"` + StateFilepath string `yaml:"state_filepath"` + FlakeSubdirectory string `yaml:"flake_subdirectory"` + Remotes []Remote `yaml:"remotes"` + ApiServer HttpServer `yaml:"api_server"` + Exporter HttpServer `yaml:"exporter"` + GpgPublicKeyPaths []string `yaml:"gpg_public_key_paths"` + AllowForcePushMain bool `yaml:"allow_force_push_main"` } From 547c068aa0c059510b40de45058e27292a9ae254 Mon Sep 17 00:00:00 2001 From: Marius Niveri Date: Mon, 14 Apr 2025 12:19:19 +0200 Subject: [PATCH 3/4] nix-module: Add option "allowForcePushMain" --- nix/module-options.nix | 23 ++++++++++++++++++----- nix/module.nix | 16 ++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/nix/module-options.nix b/nix/module-options.nix index e7b7cb74..3e8a36be 100644 --- a/nix/module-options.nix +++ b/nix/module-options.nix @@ -1,5 +1,11 @@ -{ config, pkgs, lib, ... }: { - options = with lib; with types; { +{ + config, + pkgs, + lib, + ... +}: { + options = with lib; + with types; { services.comin = { enable = mkOption { type = types.bool; @@ -8,9 +14,11 @@ Whether to run the comin service. ''; }; - package = lib.mkPackageOption pkgs "comin" { nullable = true; } // { - defaultText = "pkgs.comin or comin.packages.\${system}.default or null"; - }; + package = + lib.mkPackageOption pkgs "comin" {nullable = true;} + // { + defaultText = "pkgs.comin or comin.packages.\${system}.default or null"; + }; hostname = mkOption { type = str; default = config.networking.hostName; @@ -189,6 +197,11 @@ pkgs.writers.writeBash "post" "echo $COMIN_GIT_SHA"; ''; }; + allowForcePushMain = mkOption { + description = "Switch to configuration even when a force-push was detected on main."; + type = bool; + default = false; + }; }; }; } diff --git a/nix/module.nix b/nix/module.nix index 05988655..e09ac660 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -1,8 +1,20 @@ { self }: { config, pkgs, lib, ... }: let cfg = config; - cominConfigLib = import ./comin-config.nix { inherit config pkgs lib; }; - inherit (cominConfigLib) cominConfig cominConfigYaml; + yaml = pkgs.formats.yaml { }; + cominConfig = { + hostname = cfg.services.comin.hostname; + state_dir = "/var/lib/comin"; + flake_subdirectory = cfg.services.comin.flakeSubdirectory; + remotes = cfg.services.comin.remotes; + exporter = { + listen_address = cfg.services.comin.exporter.listen_address; + port = cfg.services.comin.exporter.port; + }; + gpg_public_key_paths = cfg.services.comin.gpgPublicKeyPaths; + allow_force_push_main = cfg.services.comin.allowForcePushMain; + }; + cominConfigYaml = yaml.generate "comin.yaml" cominConfig; inherit (pkgs.stdenv.hostPlatform) system; inherit (cfg.services.comin) package; From e8a7abe21258cae6a5f572451d60d06af026c535 Mon Sep 17 00:00:00 2001 From: Marius Niveri Date: Mon, 14 Apr 2025 12:19:45 +0200 Subject: [PATCH 4/4] internal: ignore force-push when option "allowForcePushMain" is true --- internal/config/config.go | 9 +++++---- internal/repository/git.go | 6 +++++- internal/types/types.go | 8 +++++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index a110d82d..496e68fd 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -61,9 +61,10 @@ func Read(path string) (config types.Configuration, err error) { func MkGitConfig(config types.Configuration) types.GitConfig { return types.GitConfig{ - Path: filepath.Join(config.StateDir, "repository"), - Dir: config.FlakeSubdirectory, - Remotes: config.Remotes, - GpgPublicKeyPaths: config.GpgPublicKeyPaths, + Path: filepath.Join(config.StateDir, "repository"), + Dir: config.FlakeSubdirectory, + Remotes: config.Remotes, + GpgPublicKeyPaths: config.GpgPublicKeyPaths, + AllowForcePushMain: config.AllowForcePushMain, } } diff --git a/internal/repository/git.go b/internal/repository/git.go index 4c20ba1e..69661539 100644 --- a/internal/repository/git.go +++ b/internal/repository/git.go @@ -85,7 +85,11 @@ func getHeadFromRemoteAndBranch(r repository, remoteName, branchName, currentMai } if err = hasNotBeenHardReset(r, branchName, currentMainHash, head); err != nil { - return + if r.GitConfig.AllowForcePushMain { + logrus.Infof("Force-push detected but ignored due to 'allowForcePushMain' being set") + } else { + return + } } commitObject, err := r.Repository.CommitObject(*head) diff --git a/internal/types/types.go b/internal/types/types.go index 804ea58a..93681cd8 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -18,9 +18,10 @@ type GitConfig struct { // The repository Path Path string // The directory in the repository - Dir string - Remotes []Remote - GpgPublicKeyPaths []string + Dir string + Remotes []Remote + GpgPublicKeyPaths []string + AllowForcePushMain bool } type Auth struct { @@ -59,4 +60,5 @@ type Configuration struct { Exporter HttpServer `yaml:"exporter"` GpgPublicKeyPaths []string `yaml:"gpg_public_key_paths"` PostDeploymentCommand string `yaml:"post_deployment_command"` + AllowForcePushMain bool `yaml:"allow_force_push_main"` }