Skip to content

Commit

Permalink
Add support for labels attribute (#13)
Browse files Browse the repository at this point in the history
* Add support for labels attributes

* Include full generated sdk

* Fix missing labels

* Build fixes

* Fmt

* Add `revision.labels`

* Add missing labels to tests

* Add test to check for empty labels

* Use commas instead of semicolons (AsciiDoc has no standard for this)

* Oops missed a semicolon

* Schema updates

---------

Co-authored-by: augustuswm <[email protected]>
  • Loading branch information
benjaminleonard and augustuswm authored Feb 28, 2024
1 parent 629e7b5 commit 648c224
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rfd-api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -3538,6 +3538,10 @@
"nullable": true,
"type": "string"
},
"labels": {
"nullable": true,
"type": "string"
},
"commit": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions rfd-api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub struct FullRfd {
pub title: String,
pub state: Option<String>,
pub authors: Option<String>,
pub labels: Option<String>,
#[partial(ListRfd(skip))]
pub content: String,
pub sha: String,
Expand Down Expand Up @@ -577,6 +578,7 @@ impl ApiContext {
title: revision.title,
state: revision.state,
authors: revision.authors,
labels: revision.labels,
sha: revision.sha,
commit: revision.commit_sha,
committed_at: revision.committed_at,
Expand Down Expand Up @@ -637,6 +639,7 @@ impl ApiContext {
title: revision.title,
state: revision.state,
authors: revision.authors,
labels: revision.labels,
content: revision.content,
sha: revision.sha,
commit: revision.commit_sha,
Expand Down
3 changes: 3 additions & 0 deletions rfd-api/src/endpoints/rfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ mod tests {
state: None,
discussion: None,
authors: None,
labels: None,
content: String::new(),
content_format: rfd_model::schema_ext::ContentFormat::Asciidoc,
sha: String::new(),
Expand All @@ -368,6 +369,7 @@ mod tests {
state: None,
discussion: None,
authors: None,
labels: None,
content: String::new(),
content_format: rfd_model::schema_ext::ContentFormat::Asciidoc,
sha: String::new(),
Expand All @@ -384,6 +386,7 @@ mod tests {
state: None,
discussion: None,
authors: None,
labels: None,
content: String::new(),
content_format: rfd_model::schema_ext::ContentFormat::Asciidoc,
sha: String::new(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE rfd_revision DROP COLUMN labels;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE rfd_revision ADD COLUMN labels VARCHAR;
1 change: 1 addition & 0 deletions rfd-model/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct RfdRevisionModel {
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub deleted_at: Option<DateTime<Utc>>,
pub labels: Option<String>,
}

#[derive(Debug, Deserialize, Serialize, Queryable, Insertable)]
Expand Down
2 changes: 2 additions & 0 deletions rfd-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct RfdRevision {
pub state: Option<String>,
pub discussion: Option<String>,
pub authors: Option<String>,
pub labels: Option<String>,
pub content: String,
pub content_format: ContentFormat,
pub sha: String,
Expand All @@ -86,6 +87,7 @@ impl From<RfdRevisionModel> for RfdRevision {
state: value.state,
discussion: value.discussion,
authors: value.authors,
labels: value.labels,
content: value.content,
content_format: value.content_format,
sha: value.sha,
Expand Down
1 change: 1 addition & 0 deletions rfd-model/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ diesel::table! {
created_at -> Timestamptz,
updated_at -> Timestamptz,
deleted_at -> Nullable<Timestamptz>,
labels -> Nullable<Varchar>,
}
}

Expand Down
2 changes: 2 additions & 0 deletions rfd-model/src/storage/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ impl RfdRevisionStore for PostgresStore {
rfd_revision::state.eq(new_revision.state.clone()),
rfd_revision::discussion.eq(new_revision.discussion.clone()),
rfd_revision::authors.eq(new_revision.authors.clone()),
rfd_revision::labels.eq(new_revision.labels.clone()),
rfd_revision::content.eq(new_revision.content.clone()),
rfd_revision::content_format.eq(new_revision.content_format.clone()),
rfd_revision::sha.eq(new_revision.sha.clone()),
Expand All @@ -309,6 +310,7 @@ impl RfdRevisionStore for PostgresStore {
rfd_revision::state.eq(excluded(rfd_revision::state)),
rfd_revision::discussion.eq(excluded(rfd_revision::discussion)),
rfd_revision::authors.eq(excluded(rfd_revision::authors)),
rfd_revision::labels.eq(excluded(rfd_revision::labels)),
rfd_revision::content.eq(excluded(rfd_revision::content)),
rfd_revision::content_format.eq(excluded(rfd_revision::content_format)),
rfd_revision::sha.eq(excluded(rfd_revision::sha)),
Expand Down
36 changes: 36 additions & 0 deletions rfd-processor/src/content/asciidoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ impl<'a> RfdAttributes for RfdAsciidoc<'a> {
})
})
}

fn get_labels(&self) -> Option<&str> {
self.attr("labels")
}

fn update_labels(&mut self, value: &str) {
self.set_attr("labels", value)
}
}

#[async_trait]
Expand Down Expand Up @@ -570,6 +578,7 @@ sdf
:revremark: State: {state}
:docdatetime: 2019-01-04 19:26:06 UTC
:localdatetime: 2019-01-04 19:26:06 UTC
:labels: label1, label2
= RFD 123 Place
FirstName LastName <[email protected]>
Expand Down Expand Up @@ -626,4 +635,31 @@ in velit.

assert_eq!(expected, pdf);
}

#[test]
fn test_get_asciidoc_labels() {
let rfd = RfdAsciidoc::new(Cow::Borrowed(test_rfd_content()));
let labels = rfd.get_labels().unwrap();
let expected = "label1, label2".to_string();
assert_eq!(expected, labels);
}

#[test]
fn test_get_asciidoc_empty_labels() {
let content = r#"sdfsdf
= RFD 43 Identity and Access Management (IAM)
No labels here
"#;
let rfd = RfdContent::new_asciidoc(content);
assert!(rfd.get_labels().is_none());
}

#[test]
fn test_update_asciidoc_labels() {
let mut rfd = RfdAsciidoc::new(Cow::Borrowed(test_rfd_content()));
rfd.update_labels("newlabel1, newlabel2");
let labels = rfd.get_labels().unwrap();
let expected = "newlabel1, newlabel2".to_string();
assert_eq!(expected, labels);
}
}
8 changes: 8 additions & 0 deletions rfd-processor/src/content/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ impl<'a> RfdAttributes for RfdMarkdown<'a> {
fn get_authors(&self) -> Option<&str> {
self.attr("authors")
}

fn get_labels(&self) -> Option<&str> {
self.attr("labels")
}

fn update_labels(&mut self, value: &str) {
self.set_attr("labels", value)
}
}

#[cfg(test)]
Expand Down
20 changes: 20 additions & 0 deletions rfd-processor/src/content/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ pub trait RfdAttributes {
/// Get the authors line stored within the document. The returned string may contain multiple
/// names
fn get_authors(&self) -> Option<&str>;

/// Get the labels stored within the document
fn get_labels(&self) -> Option<&str>;

// Update the labels stored within the document or add them if they do not exist
fn update_labels(&mut self, value: &str);
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -184,6 +190,20 @@ impl<'a> RfdAttributes for RfdContent<'a> {
Self::Markdown(md) => md.get_authors(),
}
}

fn get_labels(&self) -> Option<&str> {
match self {
Self::Asciidoc(adoc) => adoc.get_labels(),
Self::Markdown(md) => md.get_labels(),
}
}

fn update_labels(&mut self, value: &str) {
match self {
Self::Asciidoc(adoc) => adoc.update_labels(value),
Self::Markdown(md) => md.update_labels(value),
}
}
}

#[async_trait]
Expand Down
13 changes: 13 additions & 0 deletions rfd-processor/src/rfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ pub struct RfdPayload {
pub content: RfdContent<'static>,
pub content_format: ContentFormat,
pub authors: String,
pub labels: String,
pub sha: String,
pub commit_sha: String,
pub commit_date: DateTime<Utc>,
Expand Down Expand Up @@ -285,6 +286,12 @@ impl RemoteRfd {
.get_authors()
.unwrap_or_default()
.to_string();
let labels = self
.readme
.content
.get_labels()
.unwrap_or_default()
.to_string();
let discussion = self.readme.content.get_discussion();
let state = self
.readme
Expand All @@ -311,6 +318,7 @@ impl RemoteRfd {
content: self.readme.content,
content_format,
authors,
labels,
sha: self.readme.sha,
commit_sha: self.commit_sha,
commit_date: self.commit_date,
Expand Down Expand Up @@ -376,6 +384,11 @@ impl RemoteRfd {
} else {
Some(payload.authors)
},
labels: if payload.labels.is_empty() {
None
} else {
Some(payload.labels)
},
content: payload.content.raw().to_string(),
content_format: payload.content_format,
sha: payload.sha,
Expand Down
22 changes: 22 additions & 0 deletions rfd-sdk/src/generated/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2998,6 +2998,12 @@ pub mod types {
/// "type": "string",
/// "format": "uuid"
/// },
/// "labels": {
/// "type": [
/// "string",
/// "null"
/// ]
/// },
/// "link": {
/// "type": [
/// "string",
Expand Down Expand Up @@ -3048,6 +3054,8 @@ pub mod types {
pub discussion: Option<String>,
pub id: uuid::Uuid,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub labels: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub link: Option<String>,
pub pdfs: Vec<FullRfdPdfEntry>,
pub rfd_number: i32,
Expand Down Expand Up @@ -6339,6 +6347,7 @@ pub mod types {
content: Result<String, String>,
discussion: Result<Option<String>, String>,
id: Result<uuid::Uuid, String>,
labels: Result<Option<String>, String>,
link: Result<Option<String>, String>,
pdfs: Result<Vec<super::FullRfdPdfEntry>, String>,
rfd_number: Result<i32, String>,
Expand All @@ -6357,6 +6366,7 @@ pub mod types {
content: Err("no value supplied for content".to_string()),
discussion: Ok(Default::default()),
id: Err("no value supplied for id".to_string()),
labels: Ok(Default::default()),
link: Ok(Default::default()),
pdfs: Err("no value supplied for pdfs".to_string()),
rfd_number: Err("no value supplied for rfd_number".to_string()),
Expand Down Expand Up @@ -6429,6 +6439,16 @@ pub mod types {
.map_err(|e| format!("error converting supplied value for id: {}", e));
self
}
pub fn labels<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<Option<String>>,
T::Error: std::fmt::Display,
{
self.labels = value
.try_into()
.map_err(|e| format!("error converting supplied value for labels: {}", e));
self
}
pub fn link<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<Option<String>>,
Expand Down Expand Up @@ -6511,6 +6531,7 @@ pub mod types {
content: value.content?,
discussion: value.discussion?,
id: value.id?,
labels: value.labels?,
link: value.link?,
pdfs: value.pdfs?,
rfd_number: value.rfd_number?,
Expand All @@ -6531,6 +6552,7 @@ pub mod types {
content: Ok(value.content),
discussion: Ok(value.discussion),
id: Ok(value.id),
labels: Ok(value.labels),
link: Ok(value.link),
pdfs: Ok(value.pdfs),
rfd_number: Ok(value.rfd_number),
Expand Down

0 comments on commit 648c224

Please sign in to comment.