Co-authors of pushed commits get credit on their contribution calendar
Browse files at this commita651055ee5cc4299e8063736881b137365bd7a52Parent: f558fe8- feat
- co-authors of pushed commits get credit on their contribution calendar
7 changed files +205 -11
- crates/forge-core/migrations/0067_coauthored_commits.sql +13 -0
- crates/forge-core/src/store/contributions.rs +92 -6
- crates/forge-core/src/store/migrate.rs +1 -0
- crates/forge-core/src/store/user_delete.rs +1 -0
- crates/forge-web/src/i18n/misc.rs +11 -3
- crates/forge-web/src/post_update.rs +79 -2
- crates/forge-web/src/views/heatmap.rs +8 -0
crates/forge-core/migrations/0067_coauthored_commits.sqladded+13 -0
| @@ -0,0 +1,13 @@ | |||
| 1 | +-- Commits a user is credited with as a co-author (a `Co-authored-by:` trailer) in a branch push. | ||
| 2 | +-- One row per (user, repo, commit); `created_at` is the push time. Applied by store/migrate.rs. | ||
| 3 | + | ||
| 4 | +CREATE TABLE coauthored_commits ( | ||
| 5 | + id TEXT PRIMARY KEY, | ||
| 6 | + user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE, | ||
| 7 | + repo_id TEXT NOT NULL REFERENCES repos(id) ON DELETE CASCADE, | ||
| 8 | + commit_oid TEXT NOT NULL, | ||
| 9 | + summary TEXT NOT NULL, | ||
| 10 | + created_at TEXT NOT NULL, | ||
| 11 | + UNIQUE(user_id, repo_id, commit_oid) | ||
| 12 | +); | ||
| 13 | +CREATE INDEX coauthored_commits_user_created ON coauthored_commits(user_id, created_at); | ||
crates/forge-core/src/store/contributions.rsmodified+92 -6
| @@ -6,20 +6,22 @@ use jiff::Timestamp; | |||
| 6 | use turso::{Row, Value}; | 6 | use turso::{Row, Value}; |
| 7 | 7 | ||
| 8 | use super::push_recent::{ZERO_OID_SHA1, ZERO_OID_SHA256}; | 8 | use super::push_recent::{ZERO_OID_SHA1, ZERO_OID_SHA256}; |
| 9 | -use super::{Store, col_int, col_text, fetch_all}; | 9 | +use super::{Store, col_int, col_text, db_err, fetch_all, new_id}; |
| 10 | use crate::error::{Error, Result}; | 10 | use crate::error::{Error, Result}; |
| 11 | use crate::tz::local_date; | 11 | use crate::tz::local_date; |
| 12 | 12 | ||
| 13 | /// Most contributions `contributions_between` returns for one day. | 13 | /// Most contributions `contributions_between` returns for one day. |
| 14 | const DAY_LIMIT: u32 = 200; | 14 | const DAY_LIMIT: u32 = 200; |
| 15 | 15 | ||
| 16 | -/// What a contribution was: a branch push, or an issue, pull request or comment the user wrote. | 16 | +/// What a contribution was: a branch push, an issue, pull request or comment the user wrote, or |
| 17 | +/// a commit the user was credited with as a co-author. | ||
| 17 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | 18 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 18 | pub enum ContributionKind { | 19 | pub enum ContributionKind { |
| 19 | Push, | 20 | Push, |
| 20 | Issue, | 21 | Issue, |
| 21 | Pull, | 22 | Pull, |
| 22 | Comment, | 23 | Comment, |
| 24 | + CoAuthored, | ||
| 23 | } | 25 | } |
| 24 | 26 | ||
| 25 | /// One contribution, with the repo it landed in. | 27 | /// One contribution, with the repo it landed in. |
| @@ -34,8 +36,10 @@ pub struct ContributionItem { | |||
| 34 | pub ref_name: String, | 36 | pub ref_name: String, |
| 35 | /// The issue or pull request number (a comment's is its issue's); 0 for a push. | 37 | /// The issue or pull request number (a comment's is its issue's); 0 for a push. |
| 36 | pub number: i64, | 38 | pub number: i64, |
| 37 | - /// The issue or pull request title; empty for a push. | 39 | + /// The issue or pull request title, or a co-authored commit's summary; empty for a push. |
| 38 | pub title: String, | 40 | pub title: String, |
| 41 | + /// The co-authored commit's object id; empty for everything else. | ||
| 42 | + pub commit: String, | ||
| 39 | } | 43 | } |
| 40 | 44 | ||
| 41 | fn map_item(row: &Row) -> Result<ContributionItem> { | 45 | fn map_item(row: &Row) -> Result<ContributionItem> { |
| @@ -44,6 +48,7 @@ fn map_item(row: &Row) -> Result<ContributionItem> { | |||
| 44 | "issue" => ContributionKind::Issue, | 48 | "issue" => ContributionKind::Issue, |
| 45 | "pull" => ContributionKind::Pull, | 49 | "pull" => ContributionKind::Pull, |
| 46 | "comment" => ContributionKind::Comment, | 50 | "comment" => ContributionKind::Comment, |
| 51 | + "coauthored" => ContributionKind::CoAuthored, | ||
| 47 | other => { | 52 | other => { |
| 48 | return Err(Error::Database(format!( | 53 | return Err(Error::Database(format!( |
| 49 | "unknown contribution kind {other:?}" | 54 | "unknown contribution kind {other:?}" |
| @@ -58,6 +63,7 @@ fn map_item(row: &Row) -> Result<ContributionItem> { | |||
| 58 | ref_name: col_text(row, 4)?, | 63 | ref_name: col_text(row, 4)?, |
| 59 | number: col_int(row, 5)?, | 64 | number: col_int(row, 5)?, |
| 60 | title: col_text(row, 6)?, | 65 | title: col_text(row, 6)?, |
| 66 | + commit: col_text(row, 7)?, | ||
| 61 | }) | 67 | }) |
| 62 | } | 68 | } |
| 63 | 69 | ||
| @@ -84,6 +90,9 @@ impl Store { | |||
| 84 | UNION ALL \ | 90 | UNION ALL \ |
| 85 | SELECT substr(created_at, 1, 16) FROM comments \ | 91 | SELECT substr(created_at, 1, 16) FROM comments \ |
| 86 | WHERE author_id = ?1 AND created_at >= ?2 \ | 92 | WHERE author_id = ?1 AND created_at >= ?2 \ |
| 93 | + UNION ALL \ | ||
| 94 | + SELECT substr(created_at, 1, 16) FROM coauthored_commits \ | ||
| 95 | + WHERE user_id = ?1 AND created_at >= ?2 \ | ||
| 87 | ) GROUP BY minute"; | 96 | ) GROUP BY minute"; |
| 88 | let params = vec![ | 97 | let params = vec![ |
| 89 | Value::Text(user_id.to_string()), | 98 | Value::Text(user_id.to_string()), |
| @@ -121,20 +130,25 @@ impl Store { | |||
| 121 | ) -> Result<Vec<ContributionItem>> { | 130 | ) -> Result<Vec<ContributionItem>> { |
| 122 | // Parameters: ?1 user, ?2 from, ?3 to, ?4 and ?5 zero oids. | 131 | // Parameters: ?1 user, ?2 from, ?3 to, ?4 and ?5 zero oids. |
| 123 | let sql = "SELECT 'push' AS created_kind, o.username, r.name, p.created_at, p.ref_name, \ | 132 | let sql = "SELECT 'push' AS created_kind, o.username, r.name, p.created_at, p.ref_name, \ |
| 124 | - 0, '' FROM push_events p \ | 133 | + 0, '', '' FROM push_events p \ |
| 125 | JOIN repos r ON r.id = p.repo_id JOIN users o ON o.id = r.owner_id \ | 134 | JOIN repos r ON r.id = p.repo_id JOIN users o ON o.id = r.owner_id \ |
| 126 | WHERE p.actor_id = ?1 AND p.created_at >= ?2 AND p.created_at < ?3 \ | 135 | WHERE p.actor_id = ?1 AND p.created_at >= ?2 AND p.created_at < ?3 \ |
| 127 | AND p.new_oid NOT IN (?4, ?5) \ | 136 | AND p.new_oid NOT IN (?4, ?5) \ |
| 128 | UNION ALL \ | 137 | UNION ALL \ |
| 129 | - SELECT i.kind, o.username, r.name, i.created_at, '', i.number, i.title FROM issues i \ | 138 | + SELECT i.kind, o.username, r.name, i.created_at, '', i.number, i.title, '' FROM issues i \ |
| 130 | JOIN repos r ON r.id = i.repo_id JOIN users o ON o.id = r.owner_id \ | 139 | JOIN repos r ON r.id = i.repo_id JOIN users o ON o.id = r.owner_id \ |
| 131 | WHERE i.author_id = ?1 AND i.created_at >= ?2 AND i.created_at < ?3 \ | 140 | WHERE i.author_id = ?1 AND i.created_at >= ?2 AND i.created_at < ?3 \ |
| 132 | AND i.kind IN ('issue', 'pull') \ | 141 | AND i.kind IN ('issue', 'pull') \ |
| 133 | UNION ALL \ | 142 | UNION ALL \ |
| 134 | - SELECT 'comment', o.username, r.name, c.created_at, '', i.number, i.title \ | 143 | + SELECT 'comment', o.username, r.name, c.created_at, '', i.number, i.title, '' \ |
| 135 | FROM comments c JOIN issues i ON i.id = c.issue_id \ | 144 | FROM comments c JOIN issues i ON i.id = c.issue_id \ |
| 136 | JOIN repos r ON r.id = i.repo_id JOIN users o ON o.id = r.owner_id \ | 145 | JOIN repos r ON r.id = i.repo_id JOIN users o ON o.id = r.owner_id \ |
| 137 | WHERE c.author_id = ?1 AND c.created_at >= ?2 AND c.created_at < ?3 \ | 146 | WHERE c.author_id = ?1 AND c.created_at >= ?2 AND c.created_at < ?3 \ |
| 147 | + UNION ALL \ | ||
| 148 | + SELECT 'coauthored', o.username, r.name, k.created_at, '', 0, k.summary, k.commit_oid \ | ||
| 149 | + FROM coauthored_commits k \ | ||
| 150 | + JOIN repos r ON r.id = k.repo_id JOIN users o ON o.id = r.owner_id \ | ||
| 151 | + WHERE k.user_id = ?1 AND k.created_at >= ?2 AND k.created_at < ?3 \ | ||
| 138 | ORDER BY created_at DESC LIMIT ?6"; | 152 | ORDER BY created_at DESC LIMIT ?6"; |
| 139 | let params = vec![ | 153 | let params = vec![ |
| 140 | Value::Text(user_id.to_string()), | 154 | Value::Text(user_id.to_string()), |
| @@ -148,6 +162,36 @@ impl Store { | |||
| 148 | let conn = self.read_conn().await; | 162 | let conn = self.read_conn().await; |
| 149 | fetch_all(&conn, sql, params, map_item).await | 163 | fetch_all(&conn, sql, params, map_item).await |
| 150 | } | 164 | } |
| 165 | + | ||
| 166 | + /// Credits `user_id` with a co-authored commit in `repo_id`. A commit is recorded once per | ||
| 167 | + /// user and repo; a repeat (the same commit pushed again) is ignored. `created_at` is the push | ||
| 168 | + /// time as an RFC 3339 stamp. | ||
| 169 | + pub async fn record_coauthored_commit( | ||
| 170 | + &self, | ||
| 171 | + user_id: &str, | ||
| 172 | + repo_id: &str, | ||
| 173 | + commit_oid: &str, | ||
| 174 | + summary: &str, | ||
| 175 | + created_at: &str, | ||
| 176 | + ) -> Result<()> { | ||
| 177 | + let conn = self.inner.conn.lock().await; | ||
| 178 | + conn.execute( | ||
| 179 | + "INSERT OR IGNORE INTO coauthored_commits \ | ||
| 180 | + (id, user_id, repo_id, commit_oid, summary, created_at) \ | ||
| 181 | + VALUES (?1, ?2, ?3, ?4, ?5, ?6)", | ||
| 182 | + ( | ||
| 183 | + new_id(), | ||
| 184 | + user_id.to_string(), | ||
| 185 | + repo_id.to_string(), | ||
| 186 | + commit_oid.to_string(), | ||
| 187 | + summary.to_string(), | ||
| 188 | + created_at.to_string(), | ||
| 189 | + ), | ||
| 190 | + ) | ||
| 191 | + .await | ||
| 192 | + .map_err(db_err)?; | ||
| 193 | + Ok(()) | ||
| 194 | + } | ||
| 151 | } | 195 | } |
| 152 | 196 | ||
| 153 | #[cfg(test)] | 197 | #[cfg(test)] |
| @@ -220,6 +264,48 @@ mod tests { | |||
| 220 | assert_eq!(items[1].ref_name, "refs/heads/main"); | 264 | assert_eq!(items[1].ref_name, "refs/heads/main"); |
| 221 | } | 265 | } |
| 222 | 266 | ||
| 267 | + #[tokio::test] | ||
| 268 | + async fn coauthored_commit_shows_in_counts() { | ||
| 269 | + let dir = tempfile::tempdir().unwrap(); | ||
| 270 | + let store = Store::open(&dir.path().join("forge.db")).await.unwrap(); | ||
| 271 | + let tahinli = store | ||
| 272 | + .create_user("tahinli", None, "x", false) | ||
| 273 | + .await | ||
| 274 | + .unwrap(); | ||
| 275 | + let bob = store.create_user("bob", None, "x", false).await.unwrap(); | ||
| 276 | + let demo = store | ||
| 277 | + .create_repo(&bob.id, "demo", "", Visibility::Public, "main") | ||
| 278 | + .await | ||
| 279 | + .unwrap(); | ||
| 280 | + let at = "2026-03-01T10:00:00.000000000Z"; | ||
| 281 | + store | ||
| 282 | + .record_coauthored_commit(&tahinli.id, &demo.id, OID, "pair session", at) | ||
| 283 | + .await | ||
| 284 | + .unwrap(); | ||
| 285 | + // A repeat of the same commit is ignored. | ||
| 286 | + store | ||
| 287 | + .record_coauthored_commit(&tahinli.id, &demo.id, OID, "pair session", at) | ||
| 288 | + .await | ||
| 289 | + .unwrap(); | ||
| 290 | + | ||
| 291 | + let since = "2026-03-01T00:00:00.000000000Z"; | ||
| 292 | + let counts = store | ||
| 293 | + .contribution_counts(&tahinli.id, since, "UTC") | ||
| 294 | + .await | ||
| 295 | + .unwrap(); | ||
| 296 | + assert_eq!(counts, vec![("2026-03-01".to_string(), 1)]); | ||
| 297 | + | ||
| 298 | + let items = store | ||
| 299 | + .contributions_between(&tahinli.id, since, "2026-03-02T00:00:00.000000000Z") | ||
| 300 | + .await | ||
| 301 | + .unwrap(); | ||
| 302 | + assert_eq!(items.len(), 1); | ||
| 303 | + assert_eq!(items[0].kind, ContributionKind::CoAuthored); | ||
| 304 | + assert_eq!(items[0].repo_owner, "bob"); | ||
| 305 | + assert_eq!(items[0].title, "pair session"); | ||
| 306 | + assert_eq!(items[0].commit, OID); | ||
| 307 | + } | ||
| 308 | + | ||
| 223 | #[tokio::test] | 309 | #[tokio::test] |
| 224 | async fn counts_pushes_issues_pulls_and_comments_per_day() { | 310 | async fn counts_pushes_issues_pulls_and_comments_per_day() { |
| 225 | let dir = tempfile::tempdir().unwrap(); | 311 | let dir = tempfile::tempdir().unwrap(); |
crates/forge-core/src/store/migrate.rsmodified+1 -0
| @@ -106,6 +106,7 @@ pub const MIGRATIONS: &[(u32, &str)] = &[ | |||
| 106 | (64, include_str!("../../migrations/0064_merge_queue.sql")), | 106 | (64, include_str!("../../migrations/0064_merge_queue.sql")), |
| 107 | (65, include_str!("../../migrations/0065_hue_colours.sql")), | 107 | (65, include_str!("../../migrations/0065_hue_colours.sql")), |
| 108 | (66, include_str!("../../migrations/0066_session_ip.sql")), | 108 | (66, include_str!("../../migrations/0066_session_ip.sql")), |
| 109 | + (67, include_str!("../../migrations/0067_coauthored_commits.sql")), | ||
| 109 | ]; | 110 | ]; |
| 110 | 111 | ||
| 111 | /// The highest migration version this binary knows about. | 112 | /// The highest migration version this binary knows about. |
crates/forge-core/src/store/user_delete.rsmodified+1 -0
| @@ -118,6 +118,7 @@ const REMOVED: &[(&str, &str, &str)] = &[ | |||
| 118 | ("collaborations", "collaborators", "user_id"), | 118 | ("collaborations", "collaborators", "user_id"), |
| 119 | ("stars", "stars", "user_id"), | 119 | ("stars", "stars", "user_id"), |
| 120 | ("profile pins", "profile_pins", "user_id"), | 120 | ("profile pins", "profile_pins", "user_id"), |
| 121 | + ("co-authored commits", "coauthored_commits", "user_id"), | ||
| 121 | ("issue assignments", "issue_assignees", "user_id"), | 122 | ("issue assignments", "issue_assignees", "user_id"), |
| 122 | ("recovery codes", "recovery_codes", "user_id"), | 123 | ("recovery codes", "recovery_codes", "user_id"), |
| 123 | ("passkeys", "passkeys", "user_id"), | 124 | ("passkeys", "passkeys", "user_id"), |
crates/forge-web/src/i18n/misc.rsmodified+11 -3
| @@ -729,6 +729,9 @@ pub fn day_item_words( | |||
| 729 | ("Opened pull request".to_owned(), format!("in {repo}")) | 729 | ("Opened pull request".to_owned(), format!("in {repo}")) |
| 730 | } | 730 | } |
| 731 | (Lang::En, ContributionKind::Comment) => ("Commented on".to_owned(), format!("in {repo}")), | 731 | (Lang::En, ContributionKind::Comment) => ("Commented on".to_owned(), format!("in {repo}")), |
| 732 | + (Lang::En, ContributionKind::CoAuthored) => { | ||
| 733 | + (format!("Co-authored a commit in {repo}:"), String::new()) | ||
| 734 | + } | ||
| 732 | (Lang::Tr, ContributionKind::Push) => { | 735 | (Lang::Tr, ContributionKind::Push) => { |
| 733 | (String::new(), format!("deposunda {branch} dalına gönderim")) | 736 | (String::new(), format!("deposunda {branch} dalına gönderim")) |
| 734 | } | 737 | } |
| @@ -739,9 +742,14 @@ pub fn day_item_words( | |||
| 739 | String::new(), | 742 | String::new(), |
| 740 | format!("çekme isteğini {repo} deposunda açtı"), | 743 | format!("çekme isteğini {repo} deposunda açtı"), |
| 741 | ), | 744 | ), |
| 742 | - (Lang::Tr, ContributionKind::Comment) => { | 745 | + (Lang::Tr, ContributionKind::Comment) => ( |
| 743 | - (String::new(), format!("konusuna {repo} deposunda yorum yaptı")) | 746 | + String::new(), |
| 744 | - } | 747 | + format!("konusuna {repo} deposunda yorum yaptı"), |
| 748 | + ), | ||
| 749 | + (Lang::Tr, ContributionKind::CoAuthored) => ( | ||
| 750 | + String::new(), | ||
| 751 | + format!("commit'ine {repo} deposunda ortak yazar oldu"), | ||
| 752 | + ), | ||
| 745 | } | 753 | } |
| 746 | } | 754 | } |
| 747 | 755 | ||
crates/forge-web/src/post_update.rsmodified+79 -2
| @@ -4,7 +4,9 @@ | |||
| 4 | //! 0. Webhook events for the ref: push, and create or delete (see `webhooks::emit`). | 4 | //! 0. Webhook events for the ref: push, and create or delete (see `webhooks::emit`). |
| 5 | //! 1. Closing keywords and references from the commits a default-branch update added. These run | 5 | //! 1. Closing keywords and references from the commits a default-branch update added. These run |
| 6 | //! in a spawned task and are not coalesced: every range must be processed. | 6 | //! in a spawned task and are not coalesced: every range must be processed. |
| 7 | -//! 2. Pull request sync for `refs/heads/<b>` (see `pulls_sync`), inline. | 7 | +//! 2. Co-author credit for the commits a branch update added (see `credit_coauthors`), also |
| 8 | +//! spawned. | ||
| 9 | +//! 3. Pull request sync for `refs/heads/<b>` (see `pulls_sync`), inline. | ||
| 8 | //! | 10 | //! |
| 9 | //! Maintenance is not part of this: it is coalesced per repo and runs once per push through | 11 | //! Maintenance is not part of this: it is coalesced per repo and runs once per push through |
| 10 | //! `internal::trigger_maintenance`. Callers that update a ref outside receive-pack (the merge | 12 | //! `internal::trigger_maintenance`. Callers that update a ref outside receive-pack (the merge |
| @@ -13,8 +15,9 @@ | |||
| 13 | use std::path::PathBuf; | 15 | use std::path::PathBuf; |
| 14 | 16 | ||
| 15 | use forge_core::issue_refs::{find_closing_refs, find_issue_refs}; | 17 | use forge_core::issue_refs::{find_closing_refs, find_issue_refs}; |
| 18 | +use forge_core::time::now_stamp; | ||
| 16 | use forge_core::{IssueState, Repo, StateReason}; | 19 | use forge_core::{IssueState, Repo, StateReason}; |
| 17 | -use forge_git::history; | 20 | +use forge_git::{history, trailers}; |
| 18 | use serde_json::json; | 21 | use serde_json::json; |
| 19 | 22 | ||
| 20 | use crate::internal::is_zero_oid; | 23 | use crate::internal::is_zero_oid; |
| @@ -27,6 +30,8 @@ use crate::webhooks::emit::{Sender, emit_item_action, emit_ref_update, lookup_se | |||
| 27 | const MAX_PUSH_COMMITS: usize = 500; | 30 | const MAX_PUSH_COMMITS: usize = 500; |
| 28 | /// Commits read for a push that creates the branch. | 31 | /// Commits read for a push that creates the branch. |
| 29 | const MAX_NEW_BRANCH_COMMITS: usize = 50; | 32 | const MAX_NEW_BRANCH_COMMITS: usize = 50; |
| 33 | +/// Commits read for co-author credit from a push that updates an existing branch. | ||
| 34 | +const MAX_COAUTHOR_COMMITS: usize = 200; | ||
| 30 | 35 | ||
| 31 | /// Runs the per-ref post-update work for `refname`, moving `old` to `new`. `actor_id` is the user | 36 | /// Runs the per-ref post-update work for `refname`, moving `old` to `new`. `actor_id` is the user |
| 32 | /// who made the change (empty for system updates). Closing-keyword work is spawned; pull request | 37 | /// who made the change (empty for system updates). Closing-keyword work is spawned; pull request |
| @@ -85,6 +90,23 @@ pub async fn after_ref_update( | |||
| 85 | } | 90 | } |
| 86 | } | 91 | } |
| 87 | } | 92 | } |
| 93 | + if refname.starts_with("refs/heads/") && !is_zero_oid(new) { | ||
| 94 | + match forge_git::repo::repo_path(&state.git.data_dir, &repo.id) { | ||
| 95 | + Ok(repo_dir) => { | ||
| 96 | + tokio::spawn(credit_coauthors( | ||
| 97 | + state.clone(), | ||
| 98 | + repo.clone(), | ||
| 99 | + repo_dir, | ||
| 100 | + actor_id.to_owned(), | ||
| 101 | + old.to_owned(), | ||
| 102 | + new.to_owned(), | ||
| 103 | + )); | ||
| 104 | + } | ||
| 105 | + Err(e) => { | ||
| 106 | + tracing::error!(error = %e, repo_id = %repo.id, "co-authors: bad repo path"); | ||
| 107 | + } | ||
| 108 | + } | ||
| 109 | + } | ||
| 88 | let synced = pulls_sync::sync_pulls_for_ref(state, repo, actor_id, refname, old, new).await; | 110 | let synced = pulls_sync::sync_pulls_for_ref(state, repo, actor_id, refname, old, new).await; |
| 89 | // A push can move, merge or open any pull of the repo, so every pull page is woken. | 111 | // A push can move, merge or open any pull of the repo, so every pull page is woken. |
| 90 | crate::pages::pull_live::bump_repo_pulls(state, &repo.id).await; | 112 | crate::pages::pull_live::bump_repo_pulls(state, &repo.id).await; |
| @@ -123,6 +145,61 @@ async fn close_from_pushed_commits( | |||
| 123 | } | 145 | } |
| 124 | } | 146 | } |
| 125 | 147 | ||
| 148 | +/// Credits each registered co-author (a `Co-authored-by:` trailer) of the commits a branch update | ||
| 149 | +/// added. The pusher and a co-author whose address is the commit's own author are skipped. Errors | ||
| 150 | +/// are logged; the push has already succeeded. | ||
| 151 | +async fn credit_coauthors( | ||
| 152 | + state: AppState, | ||
| 153 | + repo: Repo, | ||
| 154 | + repo_dir: PathBuf, | ||
| 155 | + actor_id: String, | ||
| 156 | + old: String, | ||
| 157 | + new: String, | ||
| 158 | +) { | ||
| 159 | + let old = (!is_zero_oid(&old)).then_some(old.as_str()); | ||
| 160 | + let max = if old.is_some() { | ||
| 161 | + MAX_COAUTHOR_COMMITS | ||
| 162 | + } else { | ||
| 163 | + MAX_NEW_BRANCH_COMMITS | ||
| 164 | + }; | ||
| 165 | + let commits = match history::new_commits(&state.git, &repo_dir, old, &new, max).await { | ||
| 166 | + Ok(commits) => commits, | ||
| 167 | + Err(e) => { | ||
| 168 | + tracing::error!(error = %e, repo_id = %repo.id, "co-authors: listing commits failed"); | ||
| 169 | + return; | ||
| 170 | + } | ||
| 171 | + }; | ||
| 172 | + let actor = (!actor_id.is_empty()).then_some(actor_id.as_str()); | ||
| 173 | + let at = now_stamp(); | ||
| 174 | + for commit in &commits { | ||
| 175 | + for co in trailers::co_authors(&commit.message) { | ||
| 176 | + if co.email.eq_ignore_ascii_case(&commit.author_email) { | ||
| 177 | + continue; | ||
| 178 | + } | ||
| 179 | + let user = match state.store.verified_user_by_email(&co.email).await { | ||
| 180 | + Ok(Some(user)) => user, | ||
| 181 | + Ok(None) => continue, | ||
| 182 | + Err(e) => { | ||
| 183 | + tracing::error!(error = %e, repo_id = %repo.id, "co-authors: user lookup failed"); | ||
| 184 | + continue; | ||
| 185 | + } | ||
| 186 | + }; | ||
| 187 | + if Some(user.id.as_str()) == actor { | ||
| 188 | + continue; | ||
| 189 | + } | ||
| 190 | + let recorded = state | ||
| 191 | + .store | ||
| 192 | + .record_coauthored_commit(&user.id, &repo.id, &commit.oid, &commit.summary, &at) | ||
| 193 | + .await; | ||
| 194 | + if let Err(e) = recorded { | ||
| 195 | + tracing::error!(error = %e, repo_id = %repo.id, commit = %commit.oid, "co-authors: recording failed"); | ||
| 196 | + continue; | ||
| 197 | + } | ||
| 198 | + state.live.bump(&crate::live::Topic::User(user.id.clone())); | ||
| 199 | + } | ||
| 200 | + } | ||
| 201 | +} | ||
| 202 | + | ||
| 126 | /// Closes open issues named by closing keywords in `text`, and records a `referenced` event on | 203 | /// Closes open issues named by closing keywords in `text`, and records a `referenced` event on |
| 127 | /// every other issue it mentions, once per (issue, commit). | 204 | /// every other issue it mentions, once per (issue, commit). |
| 128 | async fn apply_commit_refs( | 205 | async fn apply_commit_refs( |
crates/forge-web/src/views/heatmap.rsmodified+8 -0
| @@ -231,6 +231,14 @@ fn day_row(lang: Lang, item: &ContributionItem, now: i64) -> DayRow { | |||
| 231 | format!("/{repo}/pulls/{}", item.number), | 231 | format!("/{repo}/pulls/{}", item.number), |
| 232 | format!("#{} {}", item.number, item.title), | 232 | format!("#{} {}", item.number, item.title), |
| 233 | ), | 233 | ), |
| 234 | + ContributionKind::CoAuthored => { | ||
| 235 | + let summary = if item.title.is_empty() { | ||
| 236 | + item.commit.chars().take(7).collect() | ||
| 237 | + } else { | ||
| 238 | + item.title.clone() | ||
| 239 | + }; | ||
| 240 | + (format!("/{repo}/commit/{}", item.commit), summary) | ||
| 241 | + } | ||
| 234 | }; | 242 | }; |
| 235 | DayRow { | 243 | DayRow { |
| 236 | before, | 244 | before, |