interprex-github 5.0.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
use std::time::Duration;

use async_trait::async_trait;
use interprex::{
    ChangeRequestNumber, ProviderError, ProviderTextRecord, Repository, Result, ReviewDiffSide,
    ReviewId, ReviewPublicationKey, ReviewPublishingProvider, ReviewSubmission,
    ReviewSubmissionDisposition, ReviewerApplication,
};
use octocrab::Page;
use serde::{Deserialize, Serialize};
use serde_json::json;
use sha2::{Digest, Sha256};

use crate::{
    GithubProvider,
    client::{ConfiguredApp, authenticated_external, is_not_found},
};

use super::change_requests::GithubReview;

const PUBLICATION_NAMESPACE: &str = "interprex";
const PUBLICATION_NAME: &str = "review-publication";
const PUBLICATION_VERSION: u64 = 1;
const PUBLICATION_IDENTIFIER: &str = "interprex:review-publication";
const PUBLICATION_CARRIER: &str = "<!-- interprex:review-publication";
const MAX_OBSERVED_PULL_REQUEST_COMMITS: usize = 250;
const WRITE_TIMEOUT: Duration = Duration::from_secs(30);

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
struct PublicationRecord {
    version: u64,
    key: String,
    digest: String,
    disposition: ReviewSubmissionDisposition,
}

#[derive(Deserialize)]
struct PullRequestCommit {
    sha: String,
}

struct Publication<'a> {
    review: &'a GithubReview,
    record: PublicationRecord,
}

#[derive(Clone, Copy)]
struct PublicationScope<'a> {
    app: &'a ConfiguredApp,
    repository: &'a Repository,
    number: ChangeRequestNumber,
    reviewer: &'a ReviewerApplication,
    key: &'a ReviewPublicationKey,
    digest: Option<&'a str>,
    submission: Option<&'a ReviewSubmission>,
}

impl GithubProvider {
    fn reviewer_app(&self, reviewer: &ReviewerApplication) -> Result<&ConfiguredApp> {
        let configured = self.configured_app(&reviewer.app().slug)?;
        if configured.app_id.to_string() != reviewer.app().id.as_str() {
            return Err(ProviderError::Configuration {
                origin: configured.source.clone(),
                reason: format!(
                    "named app {} has APP_ID {}, but the resolved reviewer has provider app ID {}",
                    reviewer.app().slug,
                    configured.app_id,
                    reviewer.app().id.as_str()
                ),
            });
        }
        Ok(configured)
    }

    fn matching_publication<'a>(
        &self,
        reviews: &'a [GithubReview],
        reviewer: &ReviewerApplication,
        key: &ReviewPublicationKey,
        expected_digest: Option<&str>,
        expected_submission: Option<&ReviewSubmission>,
    ) -> Result<Option<Publication<'a>>> {
        let mut matches = Vec::new();
        for review in reviews {
            if !same_reviewer(review, reviewer) {
                continue;
            }
            if review.user.as_ref().and_then(|user| user.kind.as_deref()) != Some("Bot") {
                return Err(reconciliation_error(
                    "exact-identity publication review author is not a bot",
                ));
            }
            let records = publication_records(&review.body)?;
            if records.is_empty() {
                if review.state == "PENDING"
                    && expected_submission.is_some_and(|submission| {
                        review.commit_id == submission.revision().head_sha
                            && review.body.starts_with(submission.summary())
                    })
                {
                    return Err(reconciliation_error(
                        "an exact-identity pending review matches the submission but has no publication record",
                    ));
                }
                continue;
            }
            if records.len() != 1 {
                return Err(reconciliation_error(format!(
                    "exact-identity review {} contains {} publication records",
                    review.node_id,
                    records.len()
                )));
            }
            for record in records {
                if record.key == key.as_str() {
                    matches.push(Publication { review, record });
                }
            }
        }

        let publication = match matches.len() {
            0 => return Ok(None),
            1 => matches.pop().expect("one publication"),
            count => {
                return Err(reconciliation_error(format!(
                    "found {count} exact-identity reviews carrying publication key {}",
                    key.as_str()
                )));
            }
        };
        validate_review_record(publication.review, &publication.record)?;
        if let Some(expected_digest) = expected_digest
            && publication.record.digest != expected_digest
        {
            return Err(ProviderError::InvalidInput {
                provider: "github",
                fact: format!(
                    "review publication key {} already identifies a different submission",
                    key.as_str()
                ),
            });
        }
        if let Some(submission) = expected_submission
            && (publication.review.commit_id != submission.revision().head_sha
                || publication.record.disposition != submission.disposition())
        {
            return Err(reconciliation_error(
                "matching publication record contradicts the submitted revision or disposition",
            ));
        }
        Ok(Some(publication))
    }

    async fn read_matching_publication(
        &self,
        scope: PublicationScope<'_>,
    ) -> Result<Option<OwnedPublication>> {
        let reviews = self
            .github_reviews_with(&scope.app.read, scope.repository, scope.number)
            .await?;
        self.matching_publication(
            &reviews,
            scope.reviewer,
            scope.key,
            scope.digest,
            scope.submission,
        )
        .map(|publication| publication.map(OwnedPublication::from))
    }

    async fn require_review_revision(
        &self,
        app: &ConfiguredApp,
        repository: &Repository,
        number: ChangeRequestNumber,
        revision: &str,
    ) -> Result<()> {
        let page: Page<PullRequestCommit> = app
            .read
            .get(
                format!("/repos/{repository}/pulls/{}/commits", number.get()),
                Some(&[("per_page", 100)]),
            )
            .await
            .map_err(|error| {
                if is_not_found(&error) {
                    ProviderError::NotFound {
                        entity: format!("change request {} in {repository}", number.get()),
                    }
                } else {
                    authenticated_external("read change request revisions", &error)
                }
            })?;
        let commits = app
            .read
            .all_pages(page)
            .await
            .map_err(|error| authenticated_external("read change request revisions", &error))?;
        if commits.iter().any(|commit| commit.sha == revision) {
            return Ok(());
        }
        if commits.len() >= MAX_OBSERVED_PULL_REQUEST_COMMITS {
            return Err(reconciliation_error(
                "GitHub capped the change request revision observation before the requested revision was found",
            ));
        }
        Err(ProviderError::NotFound {
            entity: format!(
                "revision {revision} on change request {} in {repository}",
                number.get()
            ),
        })
    }

    async fn submit_publication(
        &self,
        scope: PublicationScope<'_>,
        pending: &OwnedPublication,
    ) -> Result<ReviewId> {
        let event = github_event(pending.record.disposition);
        let path = format!(
            "/repos/{}/pulls/{}/reviews/{}/events",
            scope.repository,
            scope.number.get(),
            pending.numeric_id
        );
        let write = tokio::time::timeout(
            WRITE_TIMEOUT,
            scope
                .app
                .write
                .post::<_, serde_json::Value>(path, Some(&json!({ "event": event }))),
        )
        .await;
        let write_error = match write {
            Ok(Ok(_)) => None,
            Ok(Err(error)) => Some(authenticated_external("submit review", &error)),
            Err(_) => Some(reconciliation_error("submit review timed out")),
        };

        match self.read_matching_publication(scope).await? {
            Some(publication) if publication.review_id != pending.review_id => Err(
                reconciliation_error("GitHub reread a different review after submission"),
            ),
            Some(publication) if publication.is_submitted() => Ok(publication.review_id),
            Some(_) => Err(write_error.unwrap_or_else(|| {
                reconciliation_error("GitHub accepted review submission but reread it as pending")
            })),
            None => Err(write_error.unwrap_or_else(|| {
                reconciliation_error("GitHub accepted review submission but reread found no review")
            })),
        }
    }

    async fn finish_publication(
        &self,
        scope: PublicationScope<'_>,
        publication: OwnedPublication,
    ) -> Result<ReviewId> {
        if publication.is_submitted() {
            return Ok(publication.review_id);
        }
        self.submit_publication(scope, &publication).await
    }

    async fn reconcile_create(
        &self,
        scope: PublicationScope<'_>,
        create_error: ProviderError,
    ) -> Result<ReviewId> {
        match self.read_matching_publication(scope).await? {
            Some(publication) => self.finish_publication(scope, publication).await,
            None => Err(create_error),
        }
    }
}

#[async_trait]
impl ReviewPublishingProvider for GithubProvider {
    async fn publish_review(
        &self,
        repository: &Repository,
        number: ChangeRequestNumber,
        reviewer: &ReviewerApplication,
        submission: &ReviewSubmission,
    ) -> Result<ReviewId> {
        let app = self.reviewer_app(reviewer)?;
        validate_submission_summary(submission.summary())?;
        let digest = submission_digest(submission)?;
        let scope = PublicationScope {
            app,
            repository,
            number,
            reviewer,
            key: submission.publication_key(),
            digest: Some(&digest),
            submission: Some(submission),
        };
        if let Some(publication) = self.read_matching_publication(scope).await? {
            if publication.is_submitted() {
                return Ok(publication.review_id);
            }
            self.require_review_revision(app, repository, number, &submission.revision().head_sha)
                .await?;
            return self.finish_publication(scope, publication).await;
        }

        self.require_review_revision(app, repository, number, &submission.revision().head_sha)
            .await?;

        let record = PublicationRecord {
            version: PUBLICATION_VERSION,
            key: submission.publication_key().as_str().to_owned(),
            digest: digest.clone(),
            disposition: submission.disposition(),
        };
        let body = publication_body(submission.summary(), &record)?;
        let comments = submission
            .findings()
            .iter()
            .map(|finding| {
                json!({
                    "path": finding.path(),
                    "line": finding.line().get(),
                    "side": match finding.side() {
                        ReviewDiffSide::Left => "LEFT",
                        ReviewDiffSide::Right => "RIGHT",
                    },
                    "body": finding.body(),
                })
            })
            .collect::<Vec<_>>();
        let path = format!("/repos/{repository}/pulls/{}/reviews", number.get());
        let create = tokio::time::timeout(
            WRITE_TIMEOUT,
            app.write.post::<_, GithubReview>(
                path,
                Some(&json!({
                    "commit_id": submission.revision().head_sha,
                    "body": body,
                    "comments": comments,
                })),
            ),
        )
        .await;

        let created = match create {
            Ok(Ok(review)) => match owned_publication(
                review,
                reviewer,
                submission.publication_key(),
                &digest,
                submission,
            ) {
                Ok(publication) => publication,
                Err(response_error) => {
                    return self.reconcile_create(scope, response_error).await;
                }
            },
            Ok(Err(error)) => {
                let create_error = create_review_error(&error);
                return self.reconcile_create(scope, create_error).await;
            }
            Err(_) => {
                let create_error = reconciliation_error("create review timed out");
                return self.reconcile_create(scope, create_error).await;
            }
        };

        if !created.is_pending() {
            return Err(reconciliation_error(
                "GitHub create-review response was not pending",
            ));
        }
        self.submit_publication(scope, &created).await
    }

    async fn resume_review_publication(
        &self,
        repository: &Repository,
        number: ChangeRequestNumber,
        reviewer: &ReviewerApplication,
        key: &ReviewPublicationKey,
    ) -> Result<Option<ReviewId>> {
        let app = self.reviewer_app(reviewer)?;
        let scope = PublicationScope {
            app,
            repository,
            number,
            reviewer,
            key,
            digest: None,
            submission: None,
        };
        let Some(publication) = self.read_matching_publication(scope).await? else {
            return Ok(None);
        };
        self.finish_publication(scope, publication).await.map(Some)
    }
}

fn validate_submission_summary(summary: &str) -> Result<()> {
    if summary.contains(PUBLICATION_IDENTIFIER) {
        return Err(ProviderError::InvalidInput {
            provider: "github",
            fact: "review summary contains the reserved interprex:review-publication identifier"
                .to_owned(),
        });
    }
    Ok(())
}

#[derive(Clone)]
struct OwnedPublication {
    numeric_id: u64,
    review_id: ReviewId,
    state: String,
    record: PublicationRecord,
}

impl OwnedPublication {
    fn is_pending(&self) -> bool {
        self.state == "PENDING"
    }

    fn is_submitted(&self) -> bool {
        !self.is_pending()
    }
}

impl From<Publication<'_>> for OwnedPublication {
    fn from(publication: Publication<'_>) -> Self {
        Self {
            numeric_id: publication.review.id,
            review_id: ReviewId::new(publication.review.node_id.clone())
                .expect("validated review identifier"),
            state: publication.review.state.clone(),
            record: publication.record,
        }
    }
}

fn owned_publication(
    review: GithubReview,
    reviewer: &ReviewerApplication,
    key: &ReviewPublicationKey,
    digest: &str,
    submission: &ReviewSubmission,
) -> Result<OwnedPublication> {
    if !same_reviewer(&review, reviewer) {
        return Err(reconciliation_error(
            "GitHub create-review response has a different reviewer identity",
        ));
    }
    let mut records = publication_records(&review.body)?;
    if records.len() != 1 || records[0].key != key.as_str() {
        return Err(reconciliation_error(format!(
            "GitHub create-review response contains {} matching publication records",
            records
                .iter()
                .filter(|record| record.key == key.as_str())
                .count()
        )));
    }
    let record = records.pop().expect("one matching publication record");
    validate_review_record(&review, &record)?;
    if record.digest != digest
        || record.disposition != submission.disposition()
        || review.commit_id != submission.revision().head_sha
    {
        return Err(reconciliation_error(
            "GitHub create-review response contradicts the requested digest, revision, or disposition",
        ));
    }
    let review_id = ReviewId::new(review.node_id.clone())
        .map_err(|error| reconciliation_error(error.to_string()))?;
    Ok(OwnedPublication {
        numeric_id: review.id,
        review_id,
        state: review.state,
        record,
    })
}

fn same_reviewer(review: &GithubReview, reviewer: &ReviewerApplication) -> bool {
    review
        .performed_via_github_app
        .as_ref()
        .is_some_and(|app| app.id.to_string() == reviewer.app().id.as_str())
        && review
            .user
            .as_ref()
            .is_some_and(|user| user.node_id == reviewer.bot().id.as_str())
}

fn publication_records(body: &str) -> Result<Vec<PublicationRecord>> {
    let carrier_count = body.match_indices(PUBLICATION_CARRIER).count();
    let records = super::text_records::extract_records(body)
        .into_iter()
        .filter(|record| {
            record.namespace() == PUBLICATION_NAMESPACE && record.name() == PUBLICATION_NAME
        })
        .map(|record| {
            serde_json::from_value::<PublicationRecord>(record.value().clone()).map_err(|error| {
                reconciliation_error(format!("malformed publication record: {error}"))
            })
        })
        .collect::<Result<Vec<_>>>()?;
    if records.len() != carrier_count {
        return Err(reconciliation_error(
            "malformed publication record carrier in exact-identity review",
        ));
    }
    for record in &records {
        validate_record(record)?;
    }
    Ok(records)
}

fn validate_record(record: &PublicationRecord) -> Result<()> {
    let digest = record.digest.strip_prefix("sha256:");
    if record.version != PUBLICATION_VERSION
        || record.key.trim().is_empty()
        || digest.is_none_or(|value| {
            value.len() != 64 || !value.bytes().all(|byte| byte.is_ascii_hexdigit())
        })
    {
        return Err(reconciliation_error(
            "publication record has an unsupported version, blank key, or invalid digest",
        ));
    }
    Ok(())
}

fn validate_review_record(review: &GithubReview, record: &PublicationRecord) -> Result<()> {
    if review.id == 0 || review.node_id.is_empty() || review.commit_id.trim().is_empty() {
        return Err(reconciliation_error(
            "publication review has an invalid identifier or revision",
        ));
    }
    match review.state.as_str() {
        "PENDING" if review.submitted_at.is_none() => Ok(()),
        "PENDING" => Err(reconciliation_error(
            "pending publication review has a submission time",
        )),
        state if state == github_state(record.disposition) && review.submitted_at.is_some() => {
            Ok(())
        }
        state if state == github_state(record.disposition) => Err(reconciliation_error(
            "submitted publication review has no submission time",
        )),
        state => Err(reconciliation_error(format!(
            "publication record intends {} but GitHub reports {state}",
            github_state(record.disposition)
        ))),
    }
}

fn publication_body(summary: &str, record: &PublicationRecord) -> Result<String> {
    let record = ProviderTextRecord::new(
        PUBLICATION_NAMESPACE,
        PUBLICATION_NAME,
        serde_json::to_value(record).map_err(|error| reconciliation_error(error.to_string()))?,
    )
    .map_err(|error| reconciliation_error(error.to_string()))?;
    Ok(super::text_records::embed_record(summary, &record))
}

fn submission_digest(submission: &ReviewSubmission) -> Result<String> {
    let bytes = serde_json::to_vec(submission)
        .map_err(|error| reconciliation_error(format!("serialize review submission: {error}")))?;
    Ok(format!("sha256:{:x}", Sha256::digest(bytes)))
}

fn create_review_error(error: &octocrab::Error) -> ProviderError {
    if let octocrab::Error::GitHub { source, .. } = error
        && source.status_code.as_u16() == 422
        && let Some(errors) = source.errors.as_deref()
        && !errors.is_empty()
        && let Some(mut fields) = errors
            .iter()
            .map(review_validation_field)
            .collect::<Option<Vec<_>>>()
    {
        fields.sort_unstable();
        fields.dedup();
        return ProviderError::InvalidInput {
            provider: "github",
            fact: format!("GitHub rejected these review fields: {}", fields.join(", ")),
        };
    }
    authenticated_external("create review", error)
}

fn review_validation_field(error: &serde_json::Value) -> Option<&'static str> {
    let resource = error.get("resource")?.as_str()?;
    let field = error.get("field")?.as_str()?;
    let code = error.get("code")?.as_str()?;
    if !matches!(
        code,
        "custom" | "invalid" | "missing" | "missing_field" | "unprocessable"
    ) {
        return None;
    }
    match (resource, field) {
        ("PullRequestReview", "body") => Some("body"),
        ("PullRequestReview", "comments") => Some("comments"),
        ("PullRequestReview", "commit_id") => Some("commit_id"),
        ("PullRequestReview", "event") => Some("event"),
        ("PullRequestReviewComment", "body") => Some("comments.body"),
        ("PullRequestReviewComment", "line") => Some("comments.line"),
        ("PullRequestReviewComment", "path") => Some("comments.path"),
        ("PullRequestReviewComment", "position") => Some("comments.position"),
        ("PullRequestReviewComment", "side") => Some("comments.side"),
        ("PullRequestReviewComment", "start_line") => Some("comments.start_line"),
        ("PullRequestReviewComment", "start_side") => Some("comments.start_side"),
        _ => None,
    }
}

fn github_event(disposition: ReviewSubmissionDisposition) -> &'static str {
    match disposition {
        ReviewSubmissionDisposition::Approved => "APPROVE",
        ReviewSubmissionDisposition::ChangesRequested => "REQUEST_CHANGES",
        ReviewSubmissionDisposition::Commented => "COMMENT",
    }
}

fn github_state(disposition: ReviewSubmissionDisposition) -> &'static str {
    match disposition {
        ReviewSubmissionDisposition::Approved => "APPROVED",
        ReviewSubmissionDisposition::ChangesRequested => "CHANGES_REQUESTED",
        ReviewSubmissionDisposition::Commented => "COMMENTED",
    }
}

fn reconciliation_error(message: impl Into<String>) -> ProviderError {
    ProviderError::External {
        provider: "github",
        operation: "reconcile review publication",
        message: message.into(),
    }
}