swarm-controller: config repos require the swarm-builder status and default to fast-forward-only merges
This commit is contained in:
parent
70e5de5571
commit
7a7041452e
2 changed files with 212 additions and 14 deletions
|
|
@ -393,8 +393,13 @@ one per hive. It ensures them at start and every five minutes after
|
|||
owner org;
|
||||
- the empty `operators` merge-gate team in `agents` and `agent-configs`;
|
||||
- the `main` merge gate on every `agent-configs` repo: merge and approval
|
||||
whitelists = the `operators` team, and no user. The controller leaves a
|
||||
repo with no `main` rule alone;
|
||||
whitelists = the `operators` team, and no user, and the `swarm-builder`
|
||||
commit status must have passed (it is the only required status). The
|
||||
controller leaves a repo with no `main` rule alone;
|
||||
- the merge settings of every `agent-configs` repo: fast-forward-only is
|
||||
the default merge style, and rebase is allowed so an out-of-date PR
|
||||
branch can be brought onto `main`. The other merge styles stay as the
|
||||
repo has them;
|
||||
- the pull-mirrors from `deploy.forgejo.mirrors` on the controller's
|
||||
host (with the `actions/checkout` and `actions/upload-artifact` ones
|
||||
`deploy.forgejo.actionsFromSelf` adds, and `hyperhive/hyperhive` from
|
||||
|
|
|
|||
|
|
@ -25,9 +25,10 @@ use anyhow::{Context, Result};
|
|||
use base64::Engine as _;
|
||||
use forgejo_api::structs::{
|
||||
BranchProtection, ChangeFileOperation, ChangeFileOperationOperation, ChangeFilesOptions,
|
||||
CreateOrgOption, CreateTeamOption, CreateTeamOptionPermission, EditBranchProtectionOption,
|
||||
EditRepoOption, EditTeamOption, EditTeamOptionPermission, MigrateRepoOptions,
|
||||
MigrateRepoOptionsService, Team, TeamPermission, UpdateUserAvatarOption,
|
||||
CreateOrgOption, CreateTeamOption, CreateTeamOptionPermission, DefaultMergeStyle,
|
||||
EditBranchProtectionOption, EditRepoOption, EditTeamOption, EditTeamOptionPermission,
|
||||
MigrateRepoOptions, MigrateRepoOptionsService, Repository, Team, TeamPermission,
|
||||
UpdateUserAvatarOption,
|
||||
};
|
||||
use forgejo_api::{ApiErrorKind, ForgejoError};
|
||||
use reqwest::StatusCode;
|
||||
|
|
@ -327,6 +328,9 @@ struct Observed {
|
|||
/// Per config repo, whether its `main` rule already matches
|
||||
/// [`config_rule_edit`]. `Absent` is a repo with no `main` rule.
|
||||
config_rules: BTreeMap<String, Seen<bool>>,
|
||||
/// Per config repo, whether its merge settings already match
|
||||
/// [`config_merge_edit`].
|
||||
config_merge: BTreeMap<String, bool>,
|
||||
/// The config org's repo list could not be read, so `config_rules` is
|
||||
/// empty without meaning every rule matches.
|
||||
config_repos_unread: bool,
|
||||
|
|
@ -383,6 +387,10 @@ enum Action {
|
|||
ConvergeConfigRule {
|
||||
repo: String,
|
||||
},
|
||||
/// PATCH a config repo's merge settings to [`config_merge_edit`].
|
||||
ConvergeConfigMerge {
|
||||
repo: String,
|
||||
},
|
||||
CreateRepo {
|
||||
owner: String,
|
||||
name: String,
|
||||
|
|
@ -424,7 +432,9 @@ impl Action {
|
|||
| Self::SeedReadme { owner, .. }
|
||||
| Self::CreateMirror { owner, .. }
|
||||
| Self::SetMirrorInterval { owner, .. } => Some(owner),
|
||||
Self::ConvergeConfigRule { .. } | Self::SetConfigOrgAvatar { .. } => Some(CONFIG_ORG),
|
||||
Self::ConvergeConfigRule { .. }
|
||||
| Self::ConvergeConfigMerge { .. }
|
||||
| Self::SetConfigOrgAvatar { .. } => Some(CONFIG_ORG),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -457,6 +467,11 @@ fn plan(desired: &Desired, observed: &Observed) -> Vec<Action> {
|
|||
actions.push(Action::ConvergeConfigRule { repo: repo.clone() });
|
||||
}
|
||||
}
|
||||
for (repo, matches) in &observed.config_merge {
|
||||
if !matches {
|
||||
actions.push(Action::ConvergeConfigMerge { repo: repo.clone() });
|
||||
}
|
||||
}
|
||||
for r in &desired.repos {
|
||||
let (owner, name) = (r.owner.to_owned(), r.name.to_owned());
|
||||
let (create, set_public, seed) = match observed.repo(r.owner, r.name) {
|
||||
|
|
@ -525,8 +540,9 @@ fn team_matches(t: &Team) -> bool {
|
|||
}
|
||||
|
||||
/// The merge gate on every config repo's `main`: the `operators` team approves
|
||||
/// and merges, and no user does. The empty user list removes any user an
|
||||
/// older rule carries.
|
||||
/// and merges, no user does, and the swarm builder's commit status must have
|
||||
/// passed. The empty user list removes any user an older rule carries, and the
|
||||
/// status list replaces any other required check.
|
||||
///
|
||||
/// Forgejo replaces each list this sends wholesale and keeps every field left
|
||||
/// `None` as the rule has it, `required_approvals` included.
|
||||
|
|
@ -543,7 +559,7 @@ fn config_rule_edit() -> EditBranchProtectionOption {
|
|||
enable_merge_whitelist: Some(true),
|
||||
enable_push: None,
|
||||
enable_push_whitelist: None,
|
||||
enable_status_check: None,
|
||||
enable_status_check: Some(true),
|
||||
ignore_stale_approvals: None,
|
||||
merge_whitelist_teams: Some(vec![OPERATORS_TEAM.to_owned()]),
|
||||
merge_whitelist_usernames: Some(Vec::new()),
|
||||
|
|
@ -553,7 +569,7 @@ fn config_rule_edit() -> EditBranchProtectionOption {
|
|||
push_whitelist_usernames: None,
|
||||
require_signed_commits: None,
|
||||
required_approvals: None,
|
||||
status_check_contexts: None,
|
||||
status_check_contexts: Some(vec![crate::builder::CONTEXT.to_owned()]),
|
||||
unprotected_file_patterns: None,
|
||||
}
|
||||
}
|
||||
|
|
@ -569,6 +585,32 @@ fn config_rule_matches(rule: &BranchProtection) -> bool {
|
|||
.is_none_or(<[String]>::is_empty)
|
||||
&& rule.enable_approvals_whitelist == Some(true)
|
||||
&& only(&rule.approvals_whitelist_teams, OPERATORS_TEAM)
|
||||
&& rule.enable_status_check == Some(true)
|
||||
&& only(&rule.status_check_contexts, crate::builder::CONTEXT)
|
||||
}
|
||||
|
||||
/// The merge style every config repo defaults to: the history of a config
|
||||
/// repo is what gets deployed, so a merge is a fast-forward and carries no
|
||||
/// merge commit. Rebase stays allowed, which is how an out-of-date PR branch
|
||||
/// is brought onto `main`; the other styles are left as the repo has them.
|
||||
const CONFIG_MERGE_STYLE: DefaultMergeStyle = DefaultMergeStyle::FastForwardOnly;
|
||||
|
||||
/// The repo settings that make [`CONFIG_MERGE_STYLE`] the default: fast-forward
|
||||
/// merges allowed and preselected, and rebase allowed. Every other field is
|
||||
/// left as the repo has it.
|
||||
fn config_merge_edit() -> EditRepoOption {
|
||||
let mut edit = sparse_edit_repo_option();
|
||||
edit.allow_fast_forward_only_merge = Some(true);
|
||||
edit.allow_rebase = Some(true);
|
||||
edit.default_merge_style = Some(CONFIG_MERGE_STYLE);
|
||||
edit
|
||||
}
|
||||
|
||||
/// Whether `repo` already has every setting [`config_merge_edit`] sets.
|
||||
fn config_merge_matches(repo: &Repository) -> bool {
|
||||
repo.allow_fast_forward_only_merge == Some(true)
|
||||
&& repo.allow_rebase == Some(true)
|
||||
&& repo.default_merge_style == Some(CONFIG_MERGE_STYLE)
|
||||
}
|
||||
|
||||
/// Whether an error is Forgejo saying 404: the object is absent, as opposed
|
||||
|
|
@ -731,11 +773,18 @@ impl Client {
|
|||
};
|
||||
// An archived repo is a destroyed agent's, and refuses every write, so
|
||||
// converging its rule would fail on each pass for as long as it stays.
|
||||
for name in repos
|
||||
// Only the name and whether the merge settings match are kept: a whole
|
||||
// `Repository` held across the reads below would make this future large.
|
||||
let repos: Vec<(String, bool)> = repos
|
||||
.into_iter()
|
||||
.filter(|r| !r.archived.unwrap_or(false))
|
||||
.filter_map(|r| r.name)
|
||||
{
|
||||
.filter_map(|r| {
|
||||
let matches = config_merge_matches(&r);
|
||||
r.name.map(|name| (name, matches))
|
||||
})
|
||||
.collect();
|
||||
for (name, matches) in repos {
|
||||
observed.config_merge.insert(name.clone(), matches);
|
||||
let rule = seen(
|
||||
self.api
|
||||
.call(|api| {
|
||||
|
|
@ -799,6 +848,9 @@ impl Client {
|
|||
tracing::info!(%repo, "swarm forge objects: config repo merge gate converged");
|
||||
Ok(())
|
||||
}
|
||||
Action::ConvergeConfigMerge { repo } => {
|
||||
Box::pin(self.converge_config_merge(repo)).await
|
||||
}
|
||||
Action::CreateRepo {
|
||||
owner,
|
||||
name,
|
||||
|
|
@ -837,6 +889,17 @@ impl Client {
|
|||
}
|
||||
}
|
||||
|
||||
/// PATCH a config repo's merge settings to [`config_merge_edit`].
|
||||
async fn converge_config_merge(&self, repo: &str) -> Result<()> {
|
||||
let edit = config_merge_edit();
|
||||
self.api
|
||||
.call(|api| api.repo_edit(CONFIG_ORG, repo, edit.clone()).into_future())
|
||||
.await
|
||||
.with_context(|| format!("converge {CONFIG_ORG}/{repo} merge settings"))?;
|
||||
tracing::info!(%repo, "swarm forge objects: config repo merge style converged");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Create `org`. A 409, or a 422 with the org confirmed present, is a
|
||||
/// race with another writer and counts as done.
|
||||
async fn create_org(&self, org: &str) -> Result<()> {
|
||||
|
|
@ -1418,6 +1481,8 @@ mod tests {
|
|||
"merge_whitelist_usernames": merge_users,
|
||||
"enable_approvals_whitelist": true,
|
||||
"approvals_whitelist_teams": [OPERATORS_TEAM],
|
||||
"enable_status_check": true,
|
||||
"status_check_contexts": [crate::builder::CONTEXT],
|
||||
}))
|
||||
.expect("branch protection json")
|
||||
}
|
||||
|
|
@ -1437,6 +1502,23 @@ mod tests {
|
|||
assert!(!config_rule_matches(&approvals_off));
|
||||
}
|
||||
|
||||
/// The builder's status is required, and it is the only required one.
|
||||
#[test]
|
||||
fn a_config_rule_must_require_the_swarm_builder_status() {
|
||||
let mut off = rule(&[]);
|
||||
off.enable_status_check = Some(false);
|
||||
assert!(!config_rule_matches(&off));
|
||||
let mut none = rule(&[]);
|
||||
none.status_check_contexts = None;
|
||||
assert!(!config_rule_matches(&none));
|
||||
let mut other = rule(&[]);
|
||||
other.status_check_contexts = Some(vec![s("ci/other")]);
|
||||
assert!(!config_rule_matches(&other));
|
||||
let mut extra = rule(&[]);
|
||||
extra.status_check_contexts = Some(vec![s(crate::builder::CONTEXT), s("ci/other")]);
|
||||
assert!(!config_rule_matches(&extra));
|
||||
}
|
||||
|
||||
/// The edit is what the match checks for, so one PATCH converges a rule.
|
||||
#[test]
|
||||
fn the_config_rule_edit_produces_a_matching_rule() {
|
||||
|
|
@ -1447,11 +1529,79 @@ mod tests {
|
|||
"merge_whitelist_usernames": edit.merge_whitelist_usernames,
|
||||
"enable_approvals_whitelist": edit.enable_approvals_whitelist,
|
||||
"approvals_whitelist_teams": edit.approvals_whitelist_teams,
|
||||
"enable_status_check": edit.enable_status_check,
|
||||
"status_check_contexts": edit.status_check_contexts,
|
||||
}))
|
||||
.expect("branch protection json");
|
||||
assert!(config_rule_matches(&applied));
|
||||
}
|
||||
|
||||
fn config_repo(
|
||||
style: Option<DefaultMergeStyle>,
|
||||
fast_forward: bool,
|
||||
rebase: bool,
|
||||
) -> Repository {
|
||||
serde_json::from_value(serde_json::json!({
|
||||
"default_merge_style": style.map(|s| serde_json::to_value(s).expect("style json")),
|
||||
"allow_fast_forward_only_merge": fast_forward,
|
||||
"allow_rebase": rebase,
|
||||
}))
|
||||
.expect("repository json")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_config_repo_matches_only_with_fast_forward_default_and_rebase_allowed() {
|
||||
assert!(config_merge_matches(&config_repo(
|
||||
Some(DefaultMergeStyle::FastForwardOnly),
|
||||
true,
|
||||
true
|
||||
)));
|
||||
assert!(!config_merge_matches(&config_repo(
|
||||
Some(DefaultMergeStyle::Merge),
|
||||
true,
|
||||
true
|
||||
)));
|
||||
assert!(!config_merge_matches(&config_repo(None, true, true)));
|
||||
assert!(!config_merge_matches(&config_repo(
|
||||
Some(DefaultMergeStyle::FastForwardOnly),
|
||||
false,
|
||||
true
|
||||
)));
|
||||
assert!(!config_merge_matches(&config_repo(
|
||||
Some(DefaultMergeStyle::FastForwardOnly),
|
||||
true,
|
||||
false
|
||||
)));
|
||||
}
|
||||
|
||||
/// The edit is what the match checks for, and it leaves every other
|
||||
/// setting alone.
|
||||
#[test]
|
||||
fn the_config_merge_edit_produces_a_matching_repo_and_touches_nothing_else() {
|
||||
let edit = config_merge_edit();
|
||||
let applied = config_repo(
|
||||
edit.default_merge_style,
|
||||
edit.allow_fast_forward_only_merge == Some(true),
|
||||
edit.allow_rebase == Some(true),
|
||||
);
|
||||
assert!(config_merge_matches(&applied));
|
||||
assert!(edit.allow_squash_merge.is_none() && edit.allow_merge_commits.is_none());
|
||||
assert!(edit.private.is_none() && edit.archived.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_a_config_repo_with_other_merge_settings_is_converged() {
|
||||
let mut o = converged();
|
||||
o.config_merge.insert(s("atlas"), true);
|
||||
o.config_merge.insert(s("damocles"), false);
|
||||
assert_eq!(
|
||||
plan(&desired(), &o),
|
||||
vec![Action::ConvergeConfigMerge {
|
||||
repo: s("damocles")
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_owners_join_the_seeded_orgs_once() {
|
||||
let d = Desired::new(
|
||||
|
|
@ -1525,6 +1675,19 @@ mod tests {
|
|||
"merge_whitelist_usernames": [],
|
||||
"enable_approvals_whitelist": true,
|
||||
"approvals_whitelist_teams": [OPERATORS_TEAM],
|
||||
"enable_status_check": true,
|
||||
"status_check_contexts": [crate::builder::CONTEXT],
|
||||
})
|
||||
}
|
||||
|
||||
/// A config repo as the org's repo list returns it, with the merge settings
|
||||
/// [`config_merge_edit`] sets.
|
||||
fn config_repo_json() -> Value {
|
||||
json!({
|
||||
"name": "atlas",
|
||||
"default_merge_style": "fast-forward-only",
|
||||
"allow_fast_forward_only_merge": true,
|
||||
"allow_rebase": true,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -1569,7 +1732,7 @@ mod tests {
|
|||
put(
|
||||
format!("GET /api/v1/orgs/{CONFIG_ORG}/repos"),
|
||||
200,
|
||||
json!([{"name": "atlas"}, {"name": "gone", "archived": true}]),
|
||||
json!([config_repo_json(), {"name": "gone", "archived": true}]),
|
||||
);
|
||||
put(
|
||||
format!("GET /api/v1/repos/{CONFIG_ORG}/atlas/branch_protections/main"),
|
||||
|
|
@ -1798,6 +1961,36 @@ mod tests {
|
|||
"enable_merge_whitelist": true,
|
||||
"merge_whitelist_teams": [OPERATORS_TEAM],
|
||||
"merge_whitelist_usernames": [],
|
||||
"enable_status_check": true,
|
||||
"status_check_contexts": [crate::builder::CONTEXT],
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_config_repo_with_other_merge_settings_gets_exactly_the_merge_edit() {
|
||||
let path = "/api/v1/repos/agent-configs/atlas";
|
||||
let (client, seen) = world(vec![(
|
||||
"GET /api/v1/orgs/agent-configs/repos",
|
||||
200,
|
||||
json!([{"name": "atlas", "default_merge_style": "merge", "allow_rebase": false}]),
|
||||
)])
|
||||
.await;
|
||||
let outcome = client.reconcile(&desired_without_avatar()).await;
|
||||
assert_eq!(
|
||||
outcome,
|
||||
PassOutcome {
|
||||
applied: 1,
|
||||
failed: 0
|
||||
}
|
||||
);
|
||||
assert_eq!(writes(&seen), [format!("PATCH {path}")]);
|
||||
assert_eq!(
|
||||
sent(&seen, "PATCH", path).unwrap(),
|
||||
json!({
|
||||
"allow_fast_forward_only_merge": true,
|
||||
"allow_rebase": true,
|
||||
"default_merge_style": "fast-forward-only",
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue