Skip to content

Commit 9692c95

Browse files
authored
Merge pull request #2264 from Urgau/gh-comments-ghost
Handle missing author in gh-comments by using the ghost account
2 parents ac32ef2 + 7c9170c commit 9692c95

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/gh_comments.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::fmt::Write;
21
use std::sync::Arc;
32
use std::time::Instant;
3+
use std::{fmt::Write, sync::LazyLock};
44

55
use anyhow::Context as _;
66
use axum::{
@@ -47,6 +47,9 @@ impl cache::EstimatedSize for CachedComments {
4747
}
4848
}
4949

50+
static GHOST_ACCOUNT: LazyLock<GitHubSimplifiedAuthor> =
51+
LazyLock::new(GitHubSimplifiedAuthor::default);
52+
5053
pub async fn gh_comments(
5154
Path(ref key @ (ref owner, ref repo, issue_id)): Path<(String, String, u64)>,
5255
State(ctx): State<Arc<Context>>,
@@ -114,8 +117,10 @@ pub async fn gh_comments(
114117
std::mem::size_of::<GitHubGraphQlComment>()
115118
+ c.url.len()
116119
+ c.body_html.len()
117-
+ c.author.login.len()
118-
+ c.author.avatar_url.len()
120+
+ c.author
121+
.as_ref()
122+
.map(|a| a.login.len() + a.avatar_url.len())
123+
.unwrap_or(0)
119124
})
120125
.sum::<usize>();
121126

@@ -225,7 +230,10 @@ pub async fn gh_comments(
225230
&mut html,
226231
&issue_with_comments.body_html,
227232
&issue_with_comments.url,
228-
&issue_with_comments.author,
233+
issue_with_comments
234+
.author
235+
.as_ref()
236+
.unwrap_or(&GHOST_ACCOUNT),
229237
&issue_with_comments.created_at,
230238
&issue_with_comments.updated_at,
231239
&issue_with_comments.reactions,
@@ -273,7 +281,7 @@ pub async fn gh_comments(
273281
&mut html,
274282
&comment.body_html,
275283
&comment.url,
276-
&comment.author,
284+
comment.author.as_ref().unwrap_or(&GHOST_ACCOUNT),
277285
&comment.created_at,
278286
&comment.updated_at,
279287
&comment.reactions,
@@ -288,7 +296,7 @@ pub async fn gh_comments(
288296
&mut html,
289297
&review.body_html,
290298
&review.url,
291-
&review.author,
299+
review.author.as_ref().unwrap_or(&GHOST_ACCOUNT),
292300
review.state,
293301
&review.submitted_at,
294302
&review.updated_at,
@@ -322,7 +330,7 @@ pub async fn gh_comments(
322330
&mut html,
323331
&comment.body_html,
324332
&comment.url,
325-
&comment.author,
333+
comment.author.as_ref().unwrap_or(&GHOST_ACCOUNT),
326334
&comment.created_at,
327335
&comment.updated_at,
328336
&comment.reactions,
@@ -616,8 +624,9 @@ fn write_review_thread_as_html(
616624
)?;
617625

618626
for comment in comments {
619-
let author_login = &comment.author.login;
620-
let author_avatar_url = &comment.author.avatar_url;
627+
let author = comment.author.as_ref().unwrap_or(&GHOST_ACCOUNT);
628+
let author_login = &author.login;
629+
let author_avatar_url = &author.avatar_url;
621630
let created_at = &comment.created_at;
622631
let created_at_rfc3339 = comment.created_at.to_rfc3339();
623632
let body_html = &comment.body_html;

src/github.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3187,7 +3187,7 @@ pub struct GitHubIssueWithComments {
31873187
pub body_html: String,
31883188
pub state: GitHubIssueState,
31893189
pub url: String,
3190-
pub author: GitHubSimplifiedAuthor,
3190+
pub author: Option<GitHubSimplifiedAuthor>,
31913191
#[serde(rename = "createdAt")]
31923192
pub created_at: chrono::DateTime<chrono::Utc>,
31933193
#[serde(rename = "updatedAt")]
@@ -3207,14 +3207,24 @@ pub struct GitHubSimplifiedAuthor {
32073207
pub avatar_url: String,
32083208
}
32093209

3210+
impl Default for GitHubSimplifiedAuthor {
3211+
fn default() -> Self {
3212+
// Default to the "Deleted user" (https://github.com/ghost)
3213+
GitHubSimplifiedAuthor {
3214+
login: "ghost".to_string(),
3215+
avatar_url: "https://avatars.githubusercontent.com/u/10137?v=4".to_string(),
3216+
}
3217+
}
3218+
}
3219+
32103220
#[derive(Debug, serde::Deserialize, serde::Serialize)]
32113221
pub struct GitHubGraphQlComments {
32123222
pub nodes: Vec<GitHubGraphQlComment>,
32133223
}
32143224

32153225
#[derive(Debug, serde::Deserialize, serde::Serialize)]
32163226
pub struct GitHubGraphQlComment {
3217-
pub author: GitHubSimplifiedAuthor,
3227+
pub author: Option<GitHubSimplifiedAuthor>,
32183228
#[serde(rename = "createdAt")]
32193229
pub created_at: chrono::DateTime<chrono::Utc>,
32203230
#[serde(rename = "updatedAt")]
@@ -3254,7 +3264,7 @@ pub struct GitHubGraphQlReviewThreadComments {
32543264

32553265
#[derive(Debug, serde::Deserialize, serde::Serialize)]
32563266
pub struct GitHubGraphQlReviewThreadComment {
3257-
pub author: GitHubSimplifiedAuthor,
3267+
pub author: Option<GitHubSimplifiedAuthor>,
32583268
#[serde(rename = "createdAt")]
32593269
pub created_at: chrono::DateTime<chrono::Utc>,
32603270
#[serde(rename = "updatedAt")]
@@ -3280,7 +3290,7 @@ pub struct GitHubGraphQlReviews {
32803290

32813291
#[derive(Debug, serde::Deserialize, serde::Serialize)]
32823292
pub struct GitHubGraphQlReview {
3283-
pub author: GitHubSimplifiedAuthor,
3293+
pub author: Option<GitHubSimplifiedAuthor>,
32843294
pub id: String,
32853295
pub state: GitHubReviewState,
32863296
#[serde(rename = "submittedAt")]

0 commit comments

Comments
 (0)