Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ octocrab = "*"
url = "*"
lockable = "*"
version-compare = "*"
humantime = "*"
67 changes: 59 additions & 8 deletions src/chat/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{collections::BTreeSet, fmt, sync::Arc};

use chrono::Utc;
use git2::{BranchType, Oid};
use octocrab::models::IssueState;
use teloxide::types::{ChatId, Message};
Expand All @@ -17,6 +18,7 @@ use crate::{
condition::{Action, Condition},
error::Error,
github::{self, GitHubInfo},
options,
repo::{cache::query_cache_commit, resources::RepoResources},
utils::empty_or_start_new_line,
};
Expand Down Expand Up @@ -275,18 +277,67 @@ pub async fn pr_issue_check(
let commit = merged_pr_to_commit(resources, github_info, id, settings).await?;
return Ok(PRIssueCheckResult::Merged(commit));
}
if issue.state == IssueState::Closed {
{
match issue.state {
IssueState::Open => {
let mut locked = resources.settings.write().await;
locked
let issue_settings = locked
.pr_issues
.remove(&id)
.get_mut(&id)
.ok_or(Error::UnknownPRIssue(id))?;
};
resources.save_settings().await?;
return Ok(PRIssueCheckResult::Closed);
match &issue_settings.closed_at {
Some(_) => {
issue_settings.closed_at = None;
drop(locked);
resources.save_settings().await?;
Ok(PRIssueCheckResult::Opened)
}
None => Ok(PRIssueCheckResult::Waiting),
}
}
IssueState::Closed => {
let mut locked = resources.settings.write().await;
let issue_settings = locked
.pr_issues
.get_mut(&id)
.ok_or(Error::UnknownPRIssue(id))?;
match issue_settings.closed_at {
None => {
issue_settings.closed_at = Some(issue.closed_at.unwrap_or_else(Utc::now));
drop(locked);
resources.save_settings().await?;
Ok(PRIssueCheckResult::Closed)
}
Some(closed_at) => {
match (Utc::now() - closed_at).to_std() {
Ok(duration) => {
if duration >= *options::get().pr_issue_expire {
// expired
locked
.pr_issues
.remove(&id)
.ok_or(Error::UnknownPRIssue(id))?;
drop(locked);
resources.save_settings().await?;
Ok(PRIssueCheckResult::Expired)
} else {
// not expired
Ok(PRIssueCheckResult::Expiring)
}
}
Err(e) => {
log::error!("failed to calculate elapsed time for pr/issue {id}: {e}");
Ok(PRIssueCheckResult::Unknown)
}
}
}
}
}
// issue state is non-exhaustive
s => {
log::warn!("unknown issue state: {s:?}");
Ok(PRIssueCheckResult::Unknown)
}
}
Ok(PRIssueCheckResult::Waiting)
}

pub async fn merged_pr_to_commit(
Expand Down
4 changes: 4 additions & 0 deletions src/chat/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub struct BranchCheckResult {
#[derive(Debug)]
pub enum PRIssueCheckResult {
Merged(String),
Opened,
Closed,
Waiting,
Expiring,
Expired,
Unknown,
}
2 changes: 2 additions & 0 deletions src/chat/settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::{BTreeMap, BTreeSet};

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use teloxide::{types::User, utils::markdown};
use url::Url;
Expand All @@ -26,6 +27,7 @@ pub struct CommitSettings {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PRIssueSettings {
pub url: Url,
pub closed_at: Option<DateTime<Utc>>,
#[serde(flatten)]
pub notify: NotifySettings,
}
Expand Down
38 changes: 31 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,7 @@ async fn pr_issue_add(
let comment = optional_comment.unwrap_or_default();
let settings = PRIssueSettings {
url,
closed_at: None,
notify: NotifySettings {
comment,
subscribers,
Expand Down Expand Up @@ -848,13 +849,15 @@ async fn pr_issue_check(bot: Bot, msg: Message, repo: String, id: u64) -> Result
match result {
PRIssueCheckResult::Merged(commit) => commit_check(bot, msg, repo, commit).await,
PRIssueCheckResult::Closed => {
reply_to_msg(
&bot,
&msg,
format!("{pretty_id} has been closed \\(and removed\\)"),
)
.parse_mode(ParseMode::MarkdownV2)
.await?;
reply_to_msg(&bot, &msg, format!("{pretty_id} has been closed"))
.parse_mode(ParseMode::MarkdownV2)
.await?;
Ok(())
}
PRIssueCheckResult::Opened => {
reply_to_msg(&bot, &msg, format!("{pretty_id} has been opened"))
.parse_mode(ParseMode::MarkdownV2)
.await?;
Ok(())
}
PRIssueCheckResult::Waiting => {
Expand All @@ -874,6 +877,27 @@ async fn pr_issue_check(bot: Bot, msg: Message, repo: String, id: u64) -> Result
send.await?;
Ok(())
}
PRIssueCheckResult::Expiring => {
let mut send = reply_to_msg(&bot, &msg, format!("{pretty_id} has been closed"))
.parse_mode(ParseMode::MarkdownV2);
send = try_attach_subscribe_button_markup(
msg.chat.id,
send,
"p",
&repo,
&id.to_string(),
);
send.await?;
Ok(())
}
PRIssueCheckResult::Expired => {
log::info!("stopped tracking to pr/issue {id}");
Ok(())
}
PRIssueCheckResult::Unknown => {
log::warn!("issue {id} state unknown");
Ok(())
}
}
}
Err(Error::CommitExists(commit)) => {
Expand Down
12 changes: 12 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ pub async fn pr_issue_id_pretty(resources: &RepoResources, id: u64) -> Result<St
))
}

pub async fn pr_issue_opened_message(
resources: &RepoResources,
id: u64,
settings: &PRIssueSettings,
) -> Result<String, Error> {
Ok(format!(
"{pretty_id} has been opened{notify}",
pretty_id = pr_issue_id_pretty(resources, id).await?,
notify = empty_or_start_new_line(&settings.notify.subscribers_markdown()),
))
}

pub async fn pr_issue_merged_message(
resources: &RepoResources,
id: u64,
Expand Down
3 changes: 3 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Parser;
use humantime::Duration;
use std::path::PathBuf;

#[derive(Debug, Parser)]
Expand All @@ -10,6 +11,8 @@ pub struct Options {
pub cron: String,
#[arg(short, long)]
pub admin_chat_id: i64,
#[arg(short, long, default_value = "1week")]
pub pr_issue_expire: Duration,
}

pub static OPTIONS: once_cell::sync::OnceCell<Options> = once_cell::sync::OnceCell::new();
Expand Down
18 changes: 17 additions & 1 deletion src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
condition::Action,
message::{
branch_check_message, commit_check_message, pr_issue_closed_message,
pr_issue_merged_message,
pr_issue_merged_message, pr_issue_opened_message,
},
options,
repo::{self, resources::RepoResources},
Expand Down Expand Up @@ -162,6 +162,13 @@ async fn update_chat_repo_pr_issue(
.await?;
Ok(())
}
PRIssueCheckResult::Opened => {
let message = pr_issue_opened_message(repo_resources, id, settings).await?;
bot.send_message(chat, message)
.parse_mode(ParseMode::MarkdownV2)
.await?;
Ok(())
}
PRIssueCheckResult::Closed => {
let message = pr_issue_closed_message(repo_resources, id, settings).await?;
bot.send_message(chat, message)
Expand All @@ -170,6 +177,15 @@ async fn update_chat_repo_pr_issue(
Ok(())
}
PRIssueCheckResult::Waiting => Ok(()),
PRIssueCheckResult::Expiring => Ok(()),
PRIssueCheckResult::Expired => {
log::info!("stopped tracking to pr/issue {id}");
Ok(())
}
PRIssueCheckResult::Unknown => {
log::warn!("issue {id} state unknown");
Ok(())
}
}
}

Expand Down