interprex_github/code_reviews/
branch_updates.rs1use async_trait::async_trait;
2use interprex::{
3 BranchFreshness, BranchUpdateError, BranchUpdateObservation, BranchUpdatesProvider,
4 ChangeRequestNumber, CommitRange, ProviderError, Repository, Result,
5};
6use serde::Deserialize;
7use serde_json::{Value, json};
8
9use crate::{
10 GithubProvider,
11 client::{authenticated_external, read_error},
12};
13
14#[derive(Deserialize)]
15struct GithubComparison {
16 status: String,
17}
18
19fn freshness(status: &str) -> Result<BranchFreshness> {
20 match status {
21 "ahead" | "identical" => Ok(BranchFreshness::Current),
22 "behind" | "diverged" => Ok(BranchFreshness::Behind),
23 other => Err(unrepresentable(format!(
24 "unknown commit comparison status {other}"
25 ))),
26 }
27}
28
29fn unrepresentable(fact: impl Into<String>) -> ProviderError {
30 ProviderError::Unrepresentable {
31 provider: "github",
32 fact: fact.into(),
33 }
34}
35
36fn stale_head(expected: &str, observed: &str) -> BranchUpdateError {
37 BranchUpdateError::StaleHead {
38 expected_head_sha: expected.to_owned(),
39 observed_head_sha: observed.to_owned(),
40 }
41}
42
43#[async_trait]
44impl BranchUpdatesProvider for GithubProvider {
45 async fn branch_update(
46 &self,
47 repository: &Repository,
48 number: ChangeRequestNumber,
49 ) -> Result<BranchUpdateObservation> {
50 let pull_request = self.github_pull_request(repository, number).await?;
51 let comparison: GithubComparison = self
52 .user()?
53 .get(
54 format!(
55 "/repos/{repository}/compare/{}...{}",
56 pull_request.base.sha, pull_request.head.sha
57 ),
58 None::<&()>,
59 )
60 .await
61 .map_err(|error| {
62 read_error(
63 "compare change request revisions",
64 format!(
65 "revisions {} and {} in {repository}",
66 pull_request.base.sha, pull_request.head.sha
67 ),
68 error,
69 )
70 })?;
71
72 Ok(BranchUpdateObservation {
73 commit_range: CommitRange {
74 base_sha: pull_request.base.sha,
75 head_sha: pull_request.head.sha,
76 },
77 freshness: freshness(&comparison.status)?,
78 })
79 }
80
81 async fn update_change_request_branch(
82 &self,
83 repository: &Repository,
84 number: ChangeRequestNumber,
85 expected_head_sha: &str,
86 ) -> std::result::Result<(), BranchUpdateError> {
87 if expected_head_sha.is_empty() {
88 return Err(ProviderError::InvalidInput {
89 provider: "github",
90 fact: "expected change-request head must not be empty".to_owned(),
91 }
92 .into());
93 }
94 let observed = self.github_pull_request(repository, number).await?;
95 if observed.head.sha != expected_head_sha {
96 return Err(stale_head(expected_head_sha, &observed.head.sha));
97 }
98
99 let write = self
103 .streaming_user()?
104 .put::<Value, _, _>(
105 format!("/repos/{repository}/pulls/{}/update-branch", number.get()),
106 Some(&json!({"expected_head_sha": expected_head_sha})),
107 )
108 .await;
109 match write {
110 Ok(_) => Ok(()),
111 Err(error) => {
112 let failure = authenticated_external("update change request branch", &error);
113 match self.github_pull_request(repository, number).await {
114 Ok(current) if current.head.sha != expected_head_sha => {
115 Err(stale_head(expected_head_sha, ¤t.head.sha))
116 }
117 Err(error @ ProviderError::NotFound { .. }) => Err(error.into()),
118 Ok(_) | Err(_) => Err(failure.into()),
119 }
120 }
121 }
122 }
123}