Skip to content

Commit edf78aa

Browse files
committed
Add concrete types for sha to reduce confusion between file and commit shas
1 parent 8769a5f commit edf78aa

File tree

11 files changed

+89
-54
lines changed

11 files changed

+89
-54
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rfd-api/src/context.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use rfd_model::{
2626
OAuthClientStore, RfdFilter, RfdPdfFilter, RfdPdfStore, RfdRevisionFilter,
2727
RfdRevisionStore, RfdStore, StoreError,
2828
},
29-
AccessGroup, AccessToken, ApiUser, ApiUserProvider, InvalidValueError, Job, LinkRequest,
30-
LoginAttempt, Mapper, NewAccessGroup, NewAccessToken, NewApiKey, NewApiUser,
29+
AccessGroup, AccessToken, ApiUser, ApiUserProvider, CommitSha, FileSha, InvalidValueError, Job,
30+
LinkRequest, LoginAttempt, Mapper, NewAccessGroup, NewAccessToken, NewApiKey, NewApiUser,
3131
NewApiUserProvider, NewJob, NewLinkRequest, NewLoginAttempt, NewMapper, NewOAuthClient,
3232
NewOAuthClientRedirectUri, NewOAuthClientSecret, OAuthClient, OAuthClientRedirectUri,
3333
OAuthClientSecret, Rfd, RfdRevision,
@@ -212,8 +212,8 @@ pub struct FullRfd {
212212
#[partial(ListRfd(skip))]
213213
pub content: String,
214214
pub format: ContentFormat,
215-
pub sha: String,
216-
pub commit: String,
215+
pub sha: FileSha,
216+
pub commit: CommitSha,
217217
pub committed_at: DateTime<Utc>,
218218
#[partial(ListRfd(skip))]
219219
pub pdfs: Vec<FullRfdPdfEntry>,
@@ -652,7 +652,7 @@ impl ApiContext {
652652
labels: revision.labels,
653653
format: revision.content_format,
654654
sha: revision.sha,
655-
commit: revision.commit_sha,
655+
commit: revision.commit.into(),
656656
committed_at: revision.committed_at,
657657
visibility: rfd.visibility,
658658
})
@@ -715,7 +715,7 @@ impl ApiContext {
715715
content: revision.content,
716716
format: revision.content_format,
717717
sha: revision.sha,
718-
commit: revision.commit_sha,
718+
commit: revision.commit.into(),
719719
committed_at: revision.committed_at,
720720
pdfs: pdfs
721721
.into_iter()
@@ -814,7 +814,7 @@ impl ApiContext {
814814
None => Err(ResourceError::DoesNotExist),
815815
}?;
816816

817-
let sha = latest_revision.commit_sha;
817+
let sha = latest_revision.commit;
818818
let mut github_locations = self
819819
.github
820820
.locations_for_commit(sha.clone())
@@ -825,7 +825,7 @@ impl ApiContext {
825825
match github_locations.len() {
826826
0 => {
827827
tracing::warn!(
828-
sha,
828+
?sha,
829829
rfd_number,
830830
"Failed to find a GitHub location for most recent revision"
831831
);

rfd-api/src/endpoints/rfd.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ mod tests {
609609
labels: None,
610610
content: String::new(),
611611
content_format: rfd_model::schema_ext::ContentFormat::Asciidoc,
612-
sha: String::new(),
613-
commit_sha: String::new(),
612+
sha: String::new().into(),
613+
commit: String::new().into(),
614614
committed_at: Utc::now(),
615615
created_at: Utc::now(),
616616
updated_at: Utc::now(),
@@ -626,8 +626,8 @@ mod tests {
626626
labels: None,
627627
content: String::new(),
628628
content_format: rfd_model::schema_ext::ContentFormat::Asciidoc,
629-
sha: String::new(),
630-
commit_sha: String::new(),
629+
sha: String::new().into(),
630+
commit: String::new().into(),
631631
committed_at: Utc::now(),
632632
created_at: Utc::now(),
633633
updated_at: Utc::now(),
@@ -643,8 +643,8 @@ mod tests {
643643
labels: None,
644644
content: String::new(),
645645
content_format: rfd_model::schema_ext::ContentFormat::Asciidoc,
646-
sha: String::new(),
647-
commit_sha: String::new(),
646+
sha: String::new().into(),
647+
commit: String::new().into(),
648648
committed_at: Utc::now(),
649649
created_at: Utc::now(),
650650
updated_at: Utc::now(),

rfd-api/src/endpoints/webhook.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl GitHubCommitPayload {
7676
owner: self.repository.owner.login.clone(),
7777
repository: self.repository.name.clone(),
7878
branch: self.branch().to_string(),
79-
sha: head_commit.id.clone(),
79+
sha: head_commit.id.clone().into(),
8080
rfd,
8181
webhook_delivery_id: Some(delivery_id),
8282
committed_at: head_commit.timestamp.clone(),

rfd-github/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ chrono = { workspace = true }
1212
http = { workspace = true }
1313
octorust = { workspace = true }
1414
rfd-data = { path = "../rfd-data" }
15+
rfd-model = { path = "../rfd-model" }
1516
thiserror = { workspace = true }
1617
tracing = { workspace = true }

rfd-github/src/lib.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rfd_data::{
2222
content::{RfdAsciidoc, RfdContent, RfdMarkdown},
2323
RfdNumber,
2424
};
25+
use rfd_model::{CommitSha, FileSha};
2526
use thiserror::Error;
2627
use tracing::{instrument, Instrument};
2728

@@ -86,7 +87,7 @@ impl GitHubRfdRepo {
8687
})
8788
}
8889

89-
pub fn location(&self, branch: String, commit: String) -> GitHubRfdLocation {
90+
pub fn location(&self, branch: String, commit: CommitSha) -> GitHubRfdLocation {
9091
GitHubRfdLocation {
9192
client: self.client.clone(),
9293
owner: self.owner.clone(),
@@ -99,17 +100,17 @@ impl GitHubRfdRepo {
99100

100101
pub async fn locations_for_commit(
101102
&self,
102-
commit_sha: String,
103+
commit: CommitSha,
103104
) -> Result<Vec<GitHubRfdLocation>, GitHubError> {
104105
let branches = self
105106
.client
106107
.repos()
107-
.list_branches_for_head_commit(&self.owner, &self.repo, &commit_sha)
108+
.list_branches_for_head_commit(&self.owner, &self.repo, commit.0.as_str())
108109
.await?;
109110
Ok(branches
110111
.body
111112
.into_iter()
112-
.map(|branch| self.location(branch.name, commit_sha.clone()))
113+
.map(|branch| self.location(branch.name, commit.clone()))
113114
.collect::<Vec<_>>())
114115
}
115116

@@ -172,8 +173,10 @@ impl GitHubRfdRepo {
172173

173174
let update = GitHubRfdUpdate {
174175
number: number.into(),
175-
location: self
176-
.location(self.default_branch.clone(), default.commit.sha.clone()),
176+
location: self.location(
177+
self.default_branch.clone(),
178+
default.commit.sha.clone().into(),
179+
),
177180
committed_at: default_committed_at,
178181
};
179182

@@ -261,7 +264,7 @@ impl GitHubRfdRepo {
261264
if let Ok(number) = path_parts[1].parse::<i32>() {
262265
let update = GitHubRfdUpdate {
263266
number: rfd_number,
264-
location: self.location(branch.name, branch.commit.sha),
267+
location: self.location(branch.name, branch.commit.sha.into()),
265268
committed_at,
266269
};
267270

@@ -297,7 +300,7 @@ pub struct GitHubRfdLocation {
297300
pub repo: String,
298301
pub default_branch: String,
299302
pub branch: String,
300-
pub commit: String,
303+
pub commit: CommitSha,
301304
}
302305

303306
impl Debug for GitHubRfdLocation {
@@ -374,7 +377,7 @@ impl GitHubRfdLocation {
374377

375378
Ok(GitHubRfdReadme {
376379
content,
377-
sha,
380+
sha: sha.into(),
378381
location: GitHubRfdReadmeLocation {
379382
file: readme_path,
380383
blob_link: url,
@@ -388,11 +391,11 @@ impl GitHubRfdLocation {
388391
&self,
389392
client: &Client,
390393
path: &str,
391-
ref_: &str,
394+
ref_: &CommitSha,
392395
) -> Result<FetchedRfdContent, GitHubError> {
393396
let file = client
394397
.repos()
395-
.get_content_blob(&self.owner, &self.repo, ref_, path)
398+
.get_content_blob(&self.owner, &self.repo, ref_.0.as_str(), path)
396399
.await?;
397400

398401
let decoded = decode_base64(&file.content)?;
@@ -401,7 +404,7 @@ impl GitHubRfdLocation {
401404
Ok(FetchedRfdContent {
402405
decoded,
403406
parsed,
404-
sha: file.sha,
407+
sha: file.sha.into(),
405408
url: file.html_url,
406409
})
407410
}
@@ -421,7 +424,7 @@ impl GitHubRfdLocation {
421424
client: &'a Client,
422425
owner: &'a String,
423426
repo: &'a String,
424-
ref_: &'a String,
427+
ref_: &'a CommitSha,
425428
dir: String,
426429
) -> Pin<
427430
Box<
@@ -433,7 +436,7 @@ impl GitHubRfdLocation {
433436

434437
let resp = client
435438
.repos()
436-
.get_content_vec_entries(owner, repo, &dir, ref_)
439+
.get_content_vec_entries(owner, repo, &dir, ref_.0.as_str())
437440
.await?;
438441

439442
for file in resp.body {
@@ -449,7 +452,7 @@ impl GitHubRfdLocation {
449452
} else if is_image(&file.name) {
450453
let file = client
451454
.repos()
452-
.get_content_blob(owner, repo, ref_, &file.path)
455+
.get_content_blob(owner, repo, ref_.0.as_str(), &file.path)
453456
.await?;
454457
files.push(file);
455458
}
@@ -516,7 +519,7 @@ impl GitHubRfdLocation {
516519
.list_commits(
517520
&self.owner,
518521
&self.repo,
519-
&self.commit,
522+
&self.commit.0.as_str(),
520523
&rfd_number.repo_path(),
521524
"",
522525
None,
@@ -572,7 +575,7 @@ impl GitHubRfdLocation {
572575
&readme_path.trim_start_matches('/'),
573576
&ReposCreateUpdateFileContentsRequest {
574577
message: format!("{}\nCommitted via rfd-api", message),
575-
sha,
578+
sha: sha.into(),
576579
branch: self.branch.clone(),
577580
content: BASE64_STANDARD.encode(content),
578581
committer: Default::default(),
@@ -588,14 +591,14 @@ impl GitHubRfdLocation {
588591
struct FetchedRfdContent {
589592
decoded: Vec<u8>,
590593
parsed: String,
591-
sha: String,
594+
sha: FileSha,
592595
url: String,
593596
}
594597

595598
#[derive(Debug)]
596599
pub struct GitHubRfdReadme<'a> {
597600
pub content: RfdContent<'a>,
598-
pub sha: String,
601+
pub sha: FileSha,
599602
pub location: GitHubRfdReadmeLocation,
600603
}
601604

rfd-model/src/lib.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,36 @@ pub mod schema;
2626
pub mod schema_ext;
2727
pub mod storage;
2828

29+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
30+
pub struct CommitSha(pub String);
31+
32+
impl From<String> for CommitSha {
33+
fn from(value: String) -> Self {
34+
Self(value)
35+
}
36+
}
37+
38+
impl From<CommitSha> for String {
39+
fn from(value: CommitSha) -> Self {
40+
value.0
41+
}
42+
}
43+
44+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
45+
pub struct FileSha(pub String);
46+
47+
impl From<String> for FileSha {
48+
fn from(value: String) -> Self {
49+
Self(value)
50+
}
51+
}
52+
53+
impl From<FileSha> for String {
54+
fn from(value: FileSha) -> Self {
55+
value.0
56+
}
57+
}
58+
2959
#[partial(NewRfd)]
3060
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
3161
pub struct Rfd {
@@ -67,8 +97,8 @@ pub struct RfdRevision {
6797
pub labels: Option<String>,
6898
pub content: String,
6999
pub content_format: ContentFormat,
70-
pub sha: String,
71-
pub commit_sha: String,
100+
pub sha: FileSha,
101+
pub commit: CommitSha,
72102
pub committed_at: DateTime<Utc>,
73103
#[partial(NewRfdRevision(skip))]
74104
pub created_at: DateTime<Utc>,
@@ -90,8 +120,8 @@ impl From<RfdRevisionModel> for RfdRevision {
90120
labels: value.labels,
91121
content: value.content,
92122
content_format: value.content_format,
93-
sha: value.sha,
94-
commit_sha: value.commit_sha,
123+
sha: value.sha.into(),
124+
commit: value.commit_sha.into(),
95125
committed_at: value.committed_at,
96126
created_at: value.created_at,
97127
updated_at: value.updated_at,
@@ -141,7 +171,7 @@ pub struct Job {
141171
pub owner: String,
142172
pub repository: String,
143173
pub branch: String,
144-
pub sha: String,
174+
pub sha: CommitSha,
145175
pub rfd: i32,
146176
pub webhook_delivery_id: Option<Uuid>,
147177
pub committed_at: DateTime<Utc>,
@@ -160,7 +190,7 @@ impl From<JobModel> for Job {
160190
owner: value.owner,
161191
repository: value.repository,
162192
branch: value.branch,
163-
sha: value.sha,
193+
sha: value.sha.into(),
164194
rfd: value.rfd,
165195
webhook_delivery_id: value.webhook_delivery_id,
166196
committed_at: value.committed_at,

rfd-model/src/storage/postgres.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ impl RfdRevisionStore for PostgresStore {
303303
rfd_revision::labels.eq(new_revision.labels.clone()),
304304
rfd_revision::content.eq(new_revision.content.clone()),
305305
rfd_revision::content_format.eq(new_revision.content_format.clone()),
306-
rfd_revision::sha.eq(new_revision.sha.clone()),
307-
rfd_revision::commit_sha.eq(new_revision.commit_sha.clone()),
306+
rfd_revision::sha.eq(String::from(new_revision.sha)),
307+
rfd_revision::commit_sha.eq(String::from(new_revision.commit)),
308308
rfd_revision::committed_at.eq(new_revision.committed_at.clone()),
309309
))
310310
.on_conflict(rfd_revision::id)
@@ -500,7 +500,7 @@ impl JobStore for PostgresStore {
500500
job::owner.eq(new_job.owner.clone()),
501501
job::repository.eq(new_job.repository.clone()),
502502
job::branch.eq(new_job.branch.clone()),
503-
job::sha.eq(new_job.sha.clone()),
503+
job::sha.eq(String::from(new_job.sha)),
504504
job::rfd.eq(new_job.rfd.clone()),
505505
job::webhook_delivery_id.eq(new_job.webhook_delivery_id.clone()),
506506
job::processed.eq(false),

0 commit comments

Comments
 (0)