WIP: migrate to full featured filter list

I don't like the fact that ordering is significant, given that we would
be processing this filter list in multiple places...
This commit is contained in:
Bruno BELANYI 2021-04-18 13:36:42 +00:00
parent d942dc1098
commit efbfc3f4a0
3 changed files with 39 additions and 15 deletions

View file

@ -42,26 +42,26 @@ Setting up `lohr` should be quite simple:
configuration](https://rocket.rs/v0.4/guide/configuration/). configuration](https://rocket.rs/v0.4/guide/configuration/).
2. Export a secret variable: 2. Export a secret variable:
$ export LOHR_SECRET=42 # please don't use this secret $ export LOHR_SECRET=42 # please don't use this secret
3. Run `lohr`: 3. Run `lohr`:
$ cargo run # or `cargo run --release` for production usage $ cargo run # or `cargo run --release` for production usage
4. Configure your favorite git server to send a webhook to `lohr`'s address on 4. Configure your favorite git server to send a webhook to `lohr`'s address on
every push event. every push event.
I used [Gitea's webhooks format](https://docs.gitea.io/en-us/webhooks/), but I used [Gitea's webhooks format](https://docs.gitea.io/en-us/webhooks/), but
I **think** they're similar to GitHub and GitLab's webhooks, so these should I **think** they're similar to GitHub and GitLab's webhooks, so these should
work too! (If they don't, **please** file an issue!) work too! (If they don't, **please** file an issue!)
Don't forget to set the webhook secret to the one you chose above. Don't forget to set the webhook secret to the one you chose above.
5. Add a `.lohr` file containing the remotes you want to mirror this repo to: 5. Add a `.lohr` file containing the remotes you want to mirror this repo to:
git@github.com:you/your_repo git@github.com:you/your_repo
and push it. That's it! `lohr` is mirroring your repo now. and push it. That's it! `lohr` is mirroring your repo now.
@ -96,8 +96,8 @@ default_remotes:
additional_remotes: additional_remotes:
- "git@git.sr.ht:~user" - "git@git.sr.ht:~user"
blacklist: filters:
- "private-.*" - FIXME:
``` ```
- `default_remotes` is a list of remotes to use if no `.lohr` file is found in a - `default_remotes` is a list of remotes to use if no `.lohr` file is found in a

View file

@ -34,12 +34,15 @@ fn gitea_webhook(
config: State<GlobalSettings>, config: State<GlobalSettings>,
) -> Status { ) -> Status {
if config if config
.blacklist .filters
.iter() .iter()
.any(|re| re.is_match(&payload.repository.full_name)) // Find first filter that matches the given destination
.find(|filter| filter.destination.as_ref().map_or(false, |re| re.is_match(&payload.repository.full_name)))
// Default to mirroring, unless told not to
.map_or(true, |filter| filter.mirror)
{ {
info!( info!(
"Ignoring webhook for repo {} which is blacklisted", "Ignoring webhook for repo {} which is marked as not mirrored",
payload.repository.full_name payload.repository.full_name
); );
return Status::Ok; return Status::Ok;

View file

@ -10,8 +10,29 @@ pub(crate) struct GlobalSettings {
/// List of remote stems to use for every repository /// List of remote stems to use for every repository
#[serde(default)] #[serde(default)]
pub additional_remotes: Vec<RepoUrl>, pub additional_remotes: Vec<RepoUrl>,
/// List of regexes, if a repository's name matches any of the, it is not mirrored by `lohr` /// List of filters to blacklist repositories, or modify push options on specific remotes.
/// even if it contains a `.lorh` file. /// Only the first matching filter is applied, so order is important.
#[serde(with = "serde_regex", default)] #[serde(default)]
pub blacklist: Vec<regex::Regex>, pub filters: Vec<FilterSettings>,
}
#[derive(Clone, Default, Deserialize)]
pub(crate) struct FilterSettings {
/// Match on the source remote
#[serde(with = "serde_regex", default)]
pub source: Option<regex::Regex>,
/// Match on the destination remote
#[serde(with = "serde_regex", default)]
pub destination: Option<regex::Regex>,
/// Whether to mirror the repository or not
#[serde(default = "default_true")]
pub mirror: bool,
/// Push options to be used for the matched remote
#[serde(default)]
pub push_options: Vec<String>,
}
// Workaround for https://github.com/serde-rs/serde/issues/368
fn default_true() -> bool {
true
} }