| 1 | //! The work items a pull request closes and is part of, as its title and |
| 2 | //! description say: read through items/ (`items::named`, refs.rs), and |
| 3 | //! landed when it merges (`items::land`, landing.rs). |
| 4 | |
| 5 | use g1t_contracts::repos::Repo; |
| 6 | use g1t_contracts::work::*; |
| 7 | use worker::Result; |
| 8 | |
| 9 | use crate::Work; |
| 10 | use crate::items::{self, NamedItems}; |
| 11 | use crate::rows::ValueRow; |
| 12 | |
| 13 | impl Work { |
| 14 | /// The work items a pull request closes and is part of as its title and |
| 15 | /// description say now: none closed where it does not merge into the |
| 16 | /// default branch, where the repository does not close work items that |
| 17 | /// way, or where it is kept open (`keep_open`), when its own work item |
| 18 | /// is only something it is part of. |
| 19 | pub(crate) async fn items_to_close(&self, pull: &Pull, repo: &Repo, settings: &RepoSettings, keep_open: bool) -> Result<NamedItems> { |
| 20 | let own = self.pull_item_id(&pull.id).await?; |
| 21 | let text = format!("{}\n{}", pull.title, pull.body.as_deref().unwrap_or_default()); |
| 22 | let mut named = items::named(self, repo, own.as_deref(), &text).await?; |
| 23 | if keep_open || !settings.auto_close_items || !pull.targets_default(&repo.default_branch) { |
| 24 | let closed = std::mem::take(&mut named.closes); |
| 25 | let mut parts = if keep_open { closed } else { Vec::new() }; |
| 26 | parts.extend(named.parts); |
| 27 | named.parts = parts; |
| 28 | } |
| 29 | Ok(named) |
| 30 | } |
| 31 | |
| 32 | /// The id of a pull request's own work item, if it has one. |
| 33 | pub(crate) async fn pull_item_id(&self, pull_id: &str) -> Result<Option<String>> { |
| 34 | Ok(self |
| 35 | .db |
| 36 | .prepare("SELECT item_id AS value FROM pulls WHERE id = ? AND item_id IS NOT NULL") |
| 37 | .bind(&[pull_id.into()])? |
| 38 | .first::<ValueRow>(None) |
| 39 | .await? |
| 40 | .map(|row| row.value)) |
| 41 | } |
| 42 | } |