| 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::User; |
| 7 | use g1t_contracts::work::*; |
| 8 | use worker::Result; |
| 9 | |
| 10 | use crate::Work; |
| 11 | use crate::items::{self, NamedItems}; |
| 12 | use crate::rows::ValueRow; |
| 13 | |
| 14 | impl Work { |
| 15 | /// The work items a pull request closes and is part of as its title and |
| 16 | /// description say now: none closed where it does not merge into the |
| 17 | /// default branch, where the repository does not close work items that |
| 18 | /// way, or where it is kept open (`keep_open`), when its own work item |
| 19 | /// is only something it is part of. Only what each of `who` (who merges |
| 20 | /// it, and whoever it is for) may close, or see, counts. |
| 21 | pub(crate) async fn items_to_close( |
| 22 | &self, |
| 23 | pull: &Pull, |
| 24 | repo: &Repo, |
| 25 | settings: &RepoSettings, |
| 26 | keep_open: bool, |
| 27 | who: &[&User], |
| 28 | ) -> Result<NamedItems> { |
| 29 | let own = self.pull_item_id(&pull.id).await?; |
| 30 | let text = format!("{}\n{}", pull.title, pull.body.as_deref().unwrap_or_default()); |
| 31 | let mut named = items::named(self, repo, own.as_deref(), &text, who).await?; |
| 32 | if keep_open || !settings.auto_close_items || !pull.targets_default(&repo.default_branch) { |
| 33 | let closed = std::mem::take(&mut named.closes); |
| 34 | let mut parts = if keep_open { closed } else { Vec::new() }; |
| 35 | parts.extend(named.parts); |
| 36 | named.parts = parts; |
| 37 | } |
| 38 | Ok(named) |
| 39 | } |
| 40 | |
| 41 | /// Whoever a pull request is for, as identity knows them: its author, |
| 42 | /// or the person g1t's agent opened it for. None when not known. |
| 43 | pub(crate) async fn pull_owner(&self, pull: &Pull) -> Result<Option<User>> { |
| 44 | let id = pull.owner().id.clone(); |
| 45 | g1t_kit::call(&self.identity, "user_by_id", &g1t_contracts::identity::UserIdArgs { id }).await |
| 46 | } |
| 47 | |
| 48 | /// The id of a pull request's own work item, if it has one. |
| 49 | pub(crate) async fn pull_item_id(&self, pull_id: &str) -> Result<Option<String>> { |
| 50 | Ok(self |
| 51 | .db |
| 52 | .prepare("SELECT item_id AS value FROM pulls WHERE id = ? AND item_id IS NOT NULL") |
| 53 | .bind(&[pull_id.into()])? |
| 54 | .first::<ValueRow>(None) |
| 55 | .await? |
| 56 | .map(|row| row.value)) |
| 57 | } |
| 58 | } |