interprex-github 2.1.0

GitHub provider for all Interprex development-platform domains
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use std::collections::{BTreeMap, BTreeSet};

use interprex::{
    ChangeRequest, ChangeRequestHead, ChangeRequestNumber, ChangeRequestState, CommitRange,
    Mergeability, ProviderError, Repository, Result, Review, ReviewAuthor, ReviewComment,
    ReviewCommentId, ReviewDisposition, ReviewFinding, ReviewId, ReviewRelationship, ReviewState,
    ReviewThread, ReviewThreadId, ReviewThreadStatus, ReviewedRevision,
};
use octocrab::Page;
use serde::Deserialize;

use crate::{GithubProvider, client::external};

use super::actors::{GithubApp, GithubUser, ghost_actor, normalize_app, rest_actor};
use super::finding_resolutions::latest_finding_resolution;
use super::review_requests::{
    ReviewRequestNode, TimelineItemNode, normalize_review_request, outstanding_request_times,
};
use super::review_threads::{ThreadNode, normalize_comment, normalize_review_location};

#[derive(Deserialize)]
pub(super) struct GithubPullRequest {
    number: u64,
    pub(super) node_id: String,
    title: String,
    state: String,
    merged: bool,
    merged_at: Option<chrono::DateTime<chrono::Utc>>,
    draft: bool,
    /// GitHub computes the merge after the read arrives and reports `null`
    /// until that finishes. A response that carries no field at all states as
    /// little as `null` does, and reads the same way.
    mergeable: Option<bool>,
    head: GitRef,
    base: GitRef,
    user: Option<GithubUser>,
    updated_at: chrono::DateTime<chrono::Utc>,
}

#[derive(Deserialize)]
pub(super) struct GitRef {
    /// The branch name, which GitHub returns unqualified.
    #[serde(rename = "ref")]
    pub(super) branch: String,
    sha: String,
    /// The repository holding the branch, absent once GitHub stops
    /// identifying it, as for a change request whose fork was deleted.
    repo: Option<GithubRepositoryRef>,
}

#[derive(Deserialize)]
struct GithubRepositoryRef {
    full_name: String,
}

/// What a head listing reads from each pull request.
///
/// GitHub's `head` filter addresses an owner and a branch, so the repository
/// name comes back on each result and is compared here rather than assumed.
#[derive(Deserialize)]
pub(super) struct GithubPullRequestNumber {
    pub(super) number: u64,
    pub(super) head: GitRef,
}

#[derive(Deserialize, PartialEq)]
pub(super) struct GithubReview {
    node_id: String,
    pub(super) user: Option<GithubUser>,
    body: String,
    state: String,
    commit_id: String,
    submitted_at: Option<chrono::DateTime<chrono::Utc>>,
    performed_via_github_app: Option<GithubApp>,
}

#[derive(Deserialize, PartialEq)]
pub(super) struct GithubUnanchoredComment {
    node_id: String,
    user: Option<GithubUser>,
    body: String,
    created_at: chrono::DateTime<chrono::Utc>,
    updated_at: chrono::DateTime<chrono::Utc>,
}

fn normalize_disposition(value: &str) -> Result<ReviewDisposition> {
    match value {
        "APPROVED" => Ok(ReviewDisposition::Approved),
        "CHANGES_REQUESTED" => Ok(ReviewDisposition::ChangesRequested),
        "COMMENTED" => Ok(ReviewDisposition::Commented),
        "DISMISSED" => Ok(ReviewDisposition::Dismissed),
        other => Err(ProviderError::Unrepresentable {
            provider: "github",
            fact: format!("unknown review state {other}"),
        }),
    }
}

fn normalize_unanchored_comment(value: GithubUnanchoredComment) -> Result<ReviewComment> {
    let comment_id = value.node_id;
    Ok(ReviewComment {
        id: ReviewCommentId::new(comment_id.clone()).map_err(|error| {
            ProviderError::Unrepresentable {
                provider: "github",
                fact: error.to_string(),
            }
        })?,
        author: match value.user {
            Some(author) => rest_actor(author)?,
            None => ghost_actor(format!(
                "unavailable-unanchored-comment-author:{comment_id}"
            ))?,
        },
        body: value.body,
        created_at: value.created_at,
        updated_at: Some(value.updated_at),
    })
}

/// Reads GitHub's `state`, `merged` and `merged_at` fields as one state.
///
/// GitHub reports a merge as a closed pull request carrying `merged` and a
/// merge time. Every other combination of the three fields contradicts itself,
/// and the provider refuses it rather than deciding which field to believe.
fn normalize_change_request_state(
    number: u64,
    state: &str,
    merged: bool,
    merged_at: Option<chrono::DateTime<chrono::Utc>>,
) -> Result<ChangeRequestState> {
    match (state, merged, merged_at) {
        ("open", false, None) => Ok(ChangeRequestState::Open),
        ("closed", false, None) => Ok(ChangeRequestState::Closed),
        ("closed", true, Some(merged_at)) => Ok(ChangeRequestState::Merged { merged_at }),
        ("closed", true, None) => Err(ProviderError::Unrepresentable {
            provider: "github",
            fact: format!("merged change request {number} has no merge time"),
        }),
        ("open" | "closed", false, Some(_)) => Err(ProviderError::Unrepresentable {
            provider: "github",
            fact: format!("change request {number} has a merge time but is not merged"),
        }),
        ("open", true, _) => Err(ProviderError::Unrepresentable {
            provider: "github",
            fact: format!("change request {number} is open and merged"),
        }),
        (other, _, _) => Err(ProviderError::Unrepresentable {
            provider: "github",
            fact: format!("unknown change request state {other}"),
        }),
    }
}

pub(super) fn normalize_change_request(
    value: GithubPullRequest,
    mut reviews: Vec<GithubReview>,
    threads: Vec<ThreadNode>,
    review_requests: Vec<ReviewRequestNode>,
    request_events: Vec<TimelineItemNode>,
    unanchored_comments: Vec<GithubUnanchoredComment>,
) -> Result<ChangeRequest> {
    let author_provider_id = value.user.as_ref().map(|user| user.node_id.clone());
    let author = match value.user {
        Some(user) => rest_actor(user)?,
        None => ghost_actor(format!("unavailable-change-author:{}", value.node_id))?,
    };
    let base_sha = value.base.sha;
    let base_branch = value.base.branch;
    let head = observed_head(&value.head)?;
    let mut review_positions = BTreeMap::new();
    let mut normalized_reviews = Vec::new();

    reviews.sort_by(|left, right| {
        left.submitted_at
            .is_none()
            .cmp(&right.submitted_at.is_none())
            .then_with(|| left.submitted_at.cmp(&right.submitted_at))
            .then_with(|| left.node_id.cmp(&right.node_id))
    });
    for review in reviews {
        let relationship = match (
            author_provider_id.as_deref(),
            review.user.as_ref().map(|user| user.node_id.as_str()),
        ) {
            (Some(author), Some(review_author)) if author == review_author => {
                ReviewRelationship::ChangeAuthor
            }
            (Some(_), Some(_)) => ReviewRelationship::Other,
            _ => ReviewRelationship::Unknown,
        };
        let review_author = match review.user {
            Some(user) => rest_actor(user)?,
            None => ghost_actor(format!("unavailable-review-author:{}", review.node_id))?,
        };
        let review_author = match relationship {
            ReviewRelationship::ChangeAuthor => ReviewAuthor::ChangeAuthor,
            ReviewRelationship::Other => ReviewAuthor::Other(review_author),
            ReviewRelationship::Unknown => ReviewAuthor::Unknown(review_author),
        };
        let state = if review.state == "PENDING" {
            if review.submitted_at.is_some() {
                return Err(ProviderError::Unrepresentable {
                    provider: "github",
                    fact: format!("draft review {} has a submission time", review.node_id),
                });
            }
            ReviewState::Draft
        } else {
            let submitted_at =
                review
                    .submitted_at
                    .ok_or_else(|| ProviderError::Unrepresentable {
                        provider: "github",
                        fact: format!("submitted review {} has no submission time", review.node_id),
                    })?;
            ReviewState::Submitted {
                disposition: normalize_disposition(&review.state)?,
                submitted_at,
            }
        };
        let id = ReviewId::new(review.node_id.clone()).map_err(|error| {
            ProviderError::Unrepresentable {
                provider: "github",
                fact: error.to_string(),
            }
        })?;
        review_positions.insert(review.node_id, normalized_reviews.len());
        normalized_reviews.push(Review {
            id,
            author: review_author,
            via_app: review
                .performed_via_github_app
                .map(normalize_app)
                .transpose()?,
            revision: ReviewedRevision {
                head_sha: review.commit_id,
            },
            state,
            summary: (!review.body.trim().is_empty()).then_some(review.body),
            findings: Vec::new(),
        });
    }

    let mut standalone_threads = Vec::new();
    for thread in threads {
        let location = normalize_review_location(&thread)?;
        let mut comments = thread.comments.nodes.into_iter();
        let initial = comments
            .next()
            .ok_or_else(|| ProviderError::Unrepresentable {
                provider: "github",
                fact: format!("review thread {} has no comments", thread.id),
            })?;
        let review_position = match initial.pull_request_review.as_ref() {
            None => None,
            Some(review) => match review_positions.get(&review.id) {
                Some(position) => Some(*position),
                None => {
                    return Err(ProviderError::Unrepresentable {
                        provider: "github",
                        fact: format!(
                            "review thread {} references missing review {}",
                            thread.id, review.id
                        ),
                    });
                }
            },
        };
        let replies = comments
            .map(normalize_comment)
            .collect::<Result<Vec<_>>>()?;
        let resolution = review_position
            .is_some()
            .then(|| latest_finding_resolution(&replies))
            .flatten();
        let normalized = ReviewThread {
            id: ReviewThreadId::new(thread.id).map_err(|error| ProviderError::Unrepresentable {
                provider: "github",
                fact: error.to_string(),
            })?,
            location,
            outdated: thread.outdated,
            status: if thread.resolved {
                ReviewThreadStatus::Resolved
            } else {
                ReviewThreadStatus::Open
            },
            comment: normalize_comment(initial)?,
            replies,
        };
        if let Some(position) = review_position {
            normalized_reviews[position].findings.push(ReviewFinding {
                thread: normalized,
                resolution,
            });
        } else {
            standalone_threads.push(normalized);
        }
    }

    let request_times = outstanding_request_times(&request_events);
    let outstanding_requests = review_requests
        .into_iter()
        .map(|request| normalize_review_request(request, &request_times))
        .collect::<Result<Vec<_>>>()?;
    let mut unanchored_comments = unanchored_comments
        .into_iter()
        .map(normalize_unanchored_comment)
        .collect::<Result<Vec<_>>>()?;
    unanchored_comments.sort_by(|left, right| {
        left.created_at
            .cmp(&right.created_at)
            .then_with(|| left.id.cmp(&right.id))
    });

    Ok(ChangeRequest {
        number: ChangeRequestNumber::new(value.number).map_err(|error| {
            ProviderError::Unrepresentable {
                provider: "github",
                fact: error.to_string(),
            }
        })?,
        title: value.title,
        state: normalize_change_request_state(
            value.number,
            &value.state,
            value.merged,
            value.merged_at,
        )?,
        draft: value.draft,
        commit_range: CommitRange {
            base_sha,
            head_sha: value.head.sha,
        },
        base_branch,
        head,
        mergeability: match value.mergeable {
            Some(true) => Mergeability::Mergeable,
            Some(false) => Mergeability::Conflicted,
            None => Mergeability::Unknown,
        },
        author,
        updated_at: value.updated_at,
        reviews: normalized_reviews,
        standalone_threads,
        unanchored_comments,
        outstanding_requests,
    })
}

pub(super) fn thread_references_missing_review(
    reviews: &[GithubReview],
    threads: &[ThreadNode],
) -> bool {
    let review_ids = reviews
        .iter()
        .map(|review| review.node_id.as_str())
        .collect::<BTreeSet<_>>();
    threads.iter().any(|thread| {
        thread
            .comments
            .nodes
            .first()
            .and_then(|comment| comment.pull_request_review.as_ref())
            .is_some_and(|review| !review_ids.contains(review.id.as_str()))
    })
}

/// Reads the head GitHub reports for one pull request.
///
/// GitHub returns the branch unqualified and drops the repository once the
/// fork holding it is deleted. A branch without its repository is not a head,
/// so that observation is absent rather than paired with the targeted
/// repository, which did not hold the branch.
pub(super) fn observed_head(head: &GitRef) -> Result<Option<ChangeRequestHead>> {
    let Some(repository) = &head.repo else {
        return Ok(None);
    };
    let unrepresentable = |fact: String| ProviderError::Unrepresentable {
        provider: "github",
        fact,
    };
    let repository = repository
        .full_name
        .parse::<Repository>()
        .map_err(|error| {
            unrepresentable(format!("head repository {}: {error}", repository.full_name))
        })?;
    ChangeRequestHead::new(repository, &format!("refs/heads/{}", head.branch))
        .map(Some)
        .map_err(|error| unrepresentable(format!("head branch {}: {error}", head.branch)))
}

/// Writes a change request's head as GitHub's `head` pull-request filter.
///
/// The filter is `owner:branch`, naming where the branch lives rather than
/// which repository the change request targets. The two differ for a change
/// request proposed from a fork, so the owner comes from the head's own
/// repository.
pub(super) fn head_filter(head: &ChangeRequestHead) -> String {
    format!("{}:{}", head.repository().owner(), head.branch())
}

pub(super) fn number(value: u64) -> Result<ChangeRequestNumber> {
    ChangeRequestNumber::new(value).map_err(|error| ProviderError::Unrepresentable {
        provider: "github",
        fact: error.to_string(),
    })
}

impl GithubProvider {
    pub(super) async fn github_pull_request(
        &self,
        repository: &Repository,
        number: ChangeRequestNumber,
    ) -> Result<GithubPullRequest> {
        self.user()?
            .get(
                format!("/repos/{repository}/pulls/{}", number.get()),
                None::<&()>,
            )
            .await
            .map_err(|error| {
                crate::client::read_error(
                    "read change request",
                    format!("change request {} in {repository}", number.get()),
                    error,
                )
            })
    }

    pub(super) async fn github_reviews(
        &self,
        repository: &Repository,
        number: ChangeRequestNumber,
    ) -> Result<Vec<GithubReview>> {
        let page: Page<GithubReview> = self
            .user()?
            .get(
                format!("/repos/{repository}/pulls/{}/reviews", number.get()),
                Some(&[("per_page", 100)]),
            )
            .await
            .map_err(|error| external("read reviews", error))?;
        self.user()?
            .all_pages(page)
            .await
            .map_err(|error| external("read reviews", error))
    }

    pub(super) async fn github_unanchored_comments(
        &self,
        repository: &Repository,
        number: ChangeRequestNumber,
    ) -> Result<Vec<GithubUnanchoredComment>> {
        let page: Page<GithubUnanchoredComment> = self
            .user()?
            .get(
                format!("/repos/{repository}/issues/{}/comments", number.get()),
                Some(&[("per_page", 100)]),
            )
            .await
            .map_err(|error| external("read unanchored comments", error))?;
        self.user()?
            .all_pages(page)
            .await
            .map_err(|error| external("read unanchored comments", error))
    }
}

#[cfg(test)]
mod tests;