Skip to content
Snippets Groups Projects
Commit 748aa1e8 authored by Éloïs's avatar Éloïs
Browse files

feat: add param GITBOT_EXCLUDED_REPOSITORIES to exclude some repos

parent 5c7ffff3
No related branches found
No related tags found
No related merge requests found
Pipeline #12797 failed
......@@ -13,10 +13,11 @@ Use docker image `duniter/gitbot:master`.
The configuration is done entirely by environment variables:
| Environment variable | Default value | description |
|------------------------|---------------|--------------------------------------------------------------------------|
|--------------------------------|---------------|--------------------------------------------------------------------------|
| `GITBOT_XMPP_JID` | Mandatory | Gitbot xmpp account full JID (user@host) |
| `GITBOT_XMPP_PASSWORD` | Mandatory | Gitbot xmpp account password |
| `GITBOT_XMPP_MUC` | Mandatory | Full jib of target xmpp room (room@host) |
| `GITBOT_IP` | `0.0.0.0` | IP address on which the gitbot server is listening (to receive webwook) |
| `GITBOT_PORT` | `8080` | port number on which the gitbot server is listening (to receive webwook) |
| `GITLAB_TOKEN` | Mandatory | The secret token that gitlab must use for the webhook |
| `GITBOT_EXCLUDED_REPOSITORIES` | `""` | List of git repositories that should not be tracked |
......@@ -2,6 +2,7 @@ use bytes::Bytes;
use gitlab::webhooks::{IssueAction, MergeRequestAction, WebHook, WikiPageAction};
use log::{info, warn};
use std::{
collections::HashSet,
net::{IpAddr, SocketAddr},
str::FromStr,
};
......@@ -35,6 +36,9 @@ struct Opt {
/// Gitlab token ("X-Gitlab-Token")
#[structopt(short = "H", long = "header", env("GITLAB_TOKEN"))]
gitlab_token: String,
/// List of git repositories that should not be tracked
#[structopt(short, long, env("GITBOT_EXCLUDED_REPOSITORIES"))]
excluded_repositories: Vec<String>,
}
#[tokio::main]
......@@ -47,6 +51,7 @@ async fn main() -> anyhow::Result<()> {
let ip = opt.ip;
let port = opt.port;
let gitlab_token = opt.gitlab_token;
let excluded_repositories = opt.excluded_repositories.into_iter().collect();
pretty_env_logger::init();
......@@ -64,11 +69,11 @@ async fn main() -> anyhow::Result<()> {
// Functional logic: convert each received webhook to text message and send it to xmpp room
while let Some(gitlab_web_hook) = receiver.recv().await {
if let Some(message) = webhook::format_webhook(&gitlab_web_hook) {
if let Some(message) = webhook::format_webhook(&excluded_repositories, &gitlab_web_hook) {
info!("message: {}", message);
xmpp_agent.send_message(&message, &muc_jid).await;
} else {
warn!("Unhandled webhook payload: {:?}", gitlab_web_hook);
info!("Ignored webhook: {:?}", gitlab_web_hook);
}
}
......
use crate::*;
pub(super) fn format_webhook(wh: &WebHook) -> Option<String> {
pub(super) fn format_webhook(
excluded_repositories: &HashSet<String>,
wh: &WebHook,
) -> Option<String> {
log::debug!("receive webhook: {:?}", wh);
Some(match wh {
WebHook::Build(build) => {
println!("Build: {:?}", build);
info!("Build: {:?}", build);
return None;
}
WebHook::Issue(issue) => {
// Error("invalid value: web hook, expected
// Error(\"invalid value: hook date, expected
// ParseError(Invalid)\", line: 0, column: 0)", line: 0,
// column: 0)
if excluded_repositories.contains(&issue.project.path_with_namespace) {
return None;
}
let action = match issue.object_attributes.action {
Some(IssueAction::Update) => "updated",
Some(IssueAction::Open) => "opened",
......@@ -35,6 +37,9 @@ pub(super) fn format_webhook(wh: &WebHook) -> Option<String> {
)
}
WebHook::MergeRequest(merge_req) => {
if excluded_repositories.contains(&merge_req.project.path_with_namespace) {
return None;
}
let action = match merge_req.object_attributes.action {
Some(MergeRequestAction::Approved) => "approved",
Some(MergeRequestAction::Update) => "updated",
......@@ -61,14 +66,17 @@ pub(super) fn format_webhook(wh: &WebHook) -> Option<String> {
)
}
WebHook::Note(note) => {
println!("Note: {:?}", note);
info!("Note: {:?}", note);
return None;
}
WebHook::Pipeline(_pipeline) => {
// TODO create text for pipeline
WebHook::Pipeline(pipeline) => {
info!("Pipeline: {:?}", pipeline);
return None;
}
WebHook::Push(push) => {
if excluded_repositories.contains(&push.project.path_with_namespace) {
return None;
}
let branch_name = if push.ref_.starts_with("refs/heads/") {
&push.ref_[11..]
} else {
......@@ -89,6 +97,9 @@ pub(super) fn format_webhook(wh: &WebHook) -> Option<String> {
text
}
WebHook::WikiPage(page) => {
if excluded_repositories.contains(&page.project.path_with_namespace) {
return None;
}
let action = match page.object_attributes.action {
WikiPageAction::Update => "updated",
WikiPageAction::Create => "created",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment