feat(nextcloud): add command 'nextcloud card list'
Some checks failed
Test / build (map[name:nightly]) (push) Successful in 1m54s
Test / build (map[name:stable]) (push) Successful in 1m46s
Release Please / Release-plz (push) Failing after 17s

This commit is contained in:
Paul Campbell 2024-11-30 18:04:48 +00:00
parent 4f4edc53c8
commit b09eb6b117
10 changed files with 135 additions and 67 deletions

View file

@ -46,6 +46,8 @@ enum NextcloudCommand {
Board(NextcloudBoardCommand),
#[clap(subcommand)]
Stack(NextcloudStackCommand),
#[clap(subcommand)]
Card(NextcloudCardCommand),
}
#[derive(Parser, Debug)]
@ -64,6 +66,15 @@ enum NextcloudStackCommand {
},
}
#[derive(Parser, Debug)]
enum NextcloudCardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
},
}
#[derive(Parser, Debug)]
enum TrelloCommand {
#[clap(subcommand)]
@ -141,6 +152,10 @@ pub async fn run(ctx: Ctx) -> color_eyre::Result<()> {
Command::Nextcloud(NextcloudCommand::Stack(NextcloudStackCommand::List {
dump,
})) => nextcloud::stack::list(ctx, dump).await,
Command::Nextcloud(NextcloudCommand::Card(NextcloudCardCommand::List {
dump,
stack_id,
})) => nextcloud::card::list(ctx, dump, stack_id.into()).await,
}
}
}

24
src/nextcloud/card.rs Normal file
View file

@ -0,0 +1,24 @@
//
use crate::nextcloud::model::NextcloudStackId;
use crate::{p, FullCtx};
pub(crate) async fn list(
ctx: FullCtx,
dump: bool,
stack_id: NextcloudStackId,
) -> color_eyre::Result<()> {
let api_result = ctx
.deck_client()
.get_stack(ctx.cfg.nextcloud.board_id, stack_id)
.await;
if dump {
p!(ctx.prt, "{}", api_result.text);
} else {
let mut cards = api_result.result?.cards;
cards.sort_by_key(|card| card.title.clone());
cards
.iter()
.for_each(|card| p!(ctx.prt, "{}:{}", card.id, card.title));
}
Ok(())
}

View file

@ -2,12 +2,18 @@
use bytes::Bytes;
use kxio::net::{Net, ReqBuilder};
use crate::{api_result::APIResult, f, FullCtx};
use crate::nextcloud::model::{NextcloudHostname, NextcloudPassword, NextcloudUsername};
use model::{Board, Card, NextcloudBoardId, Stack};
use crate::{
api_result::APIResult,
f,
nextcloud::model::{
Board, Card, NextcloudBoardId, NextcloudHostname, NextcloudPassword, NextcloudStackId,
NextcloudUsername, Stack,
},
FullCtx,
};
pub mod board;
pub mod card;
pub mod model;
pub mod stack;
@ -101,6 +107,17 @@ impl<'ctx> DeckClient<'ctx> {
.await
}
pub async fn get_stack(
&self,
board_id: NextcloudBoardId,
stack_id: NextcloudStackId,
) -> APIResult<Stack> {
self.request(f!("boards/{board_id}/stacks/{stack_id}"), |net, url| {
net.get(url)
})
.await
}
pub async fn create_card(
&self,
board_id: i64,

View file

@ -141,9 +141,11 @@ pub struct Stack {
pub board_id: NextcloudBoardId,
#[serde(rename = "ETag")]
pub etag: NextcloudETag,
#[serde(default)]
pub cards: Vec<Card>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Card {
pub id: NextcloudCardId,
pub title: NextcloudCardTitle,
@ -152,8 +154,10 @@ pub struct Card {
pub stack_id: NextcloudStackId,
pub order: NextcloudOrder,
pub archived: bool,
#[serde(default)]
pub due_date: Option<String>,
pub labels: Vec<NextcloudLabelId>,
#[serde(default)]
pub labels: Option<Vec<String>>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]

View file

@ -4,11 +4,6 @@ use std::collections::HashMap;
// type TestResult = Result<(), Box<dyn std::error::Error>>;
use assert2::let_assert;
use kxio::{
fs::{FileSystem, TempFileSystem},
net::{MockNet, Net},
print::Printer,
};
use crate::{config::AppConfig, f, init::run, Ctx, NAME};
@ -123,6 +118,11 @@ mod template {
mod given {
use super::*;
use kxio::{
fs::{FileSystem, TempFileSystem},
net::{MockNet, Net},
print::Printer,
};
pub fn a_context(fs: FileSystem, net: Net, prt: Printer) -> Ctx {
Ctx { fs, net, prt }
@ -139,32 +139,4 @@ mod given {
pub fn a_printer() -> Printer {
kxio::print::test()
}
// pub fn a_config() -> AppConfig {
// AppConfig {
// trello: a_trello_config(),
// nextcloud: a_nextcloud_config(),
// }
// }
// pub fn a_trello_config() -> TrelloConfig {
// TrelloConfig {
// api_key: s!("trello-api-key").into(),
// api_secret: s!("trello-api-secret").into(),
// board_name: s!("Trello Platform Changes").into(),
// }
// }
// pub fn a_nextcloud_config() -> NextcloudConfig {
// let hostname = s!("nextcloud.example.org").into();
// let username = s!("username").into();
// let password = s!("password").into();
// let board_id = NextcloudBoardId::new(2);
// NextcloudConfig {
// hostname,
// username,
// password,
// board_id,
// }
// }
}

View file

@ -0,0 +1,47 @@
{
"id": 3,
"title": "Done",
"boardId": 1,
"deletedAt": 0,
"lastModified": 1733515991,
"cards": [
{
"id": 322,
"title": "Lunch: Soup & Toast",
"description": "",
"stackId": 3,
"type": "plain",
"lastModified": 1733515991,
"lastEditor": "pcampbell",
"createdAt": 1733043472,
"labels": null,
"assignedUsers": [
{
"id": 25,
"participant": {
"primaryKey": "pcampbell",
"uid": "pcampbell",
"displayname": "Paul Campbell",
"type": 0
},
"cardId": 322,
"type": 0
}
],
"attachments": null,
"attachmentCount": 0,
"owner": "pcampbell",
"order": 0,
"archived": false,
"done": null,
"duedate": null,
"deletedAt": 0,
"commentsUnread": 0,
"commentsCount": 0,
"ETag": "dda386b3b247d7b4bd8917e19d38c01b",
"overdue": 0
}
],
"order": 2,
"ETag": "dda386b3b247d7b4bd8917e19d38c01b"
}

View file

@ -30,12 +30,3 @@
// .await?;
// Ok(board)
// }
// pub trait TrelloBoards {
// fn find_by_name(&self, board_name: &TrelloBoardName) -> Option<&TrelloBoard>;
// }
// impl TrelloBoards for Vec<TrelloBoard> {
// fn find_by_name(&self, board_name: &TrelloBoardName) -> Option<&TrelloBoard> {
// self.iter().find(|b| &b.name == board_name)
// }
// }

View file

@ -42,7 +42,10 @@ pub async fn get_boards_that_member_belongs_to(
net: &Net,
prt: &Printer,
) -> APIResult<Vec<TrelloBoard>> {
let auth = TrelloAuth::new(&cfg.api_key, &cfg.api_secret);
let auth = TrelloAuth {
api_key: &cfg.api_key,
api_secret: &cfg.api_secret,
};
APIResult::new(
net.get(url("/members/me/boards?lists=open"))
.headers(auth.into())

View file

@ -1,4 +1,6 @@
//
use crate::trello::types::board::TrelloBoard;
use crate::trello::types::TrelloBoardName;
use crate::{p, FullCtx};
pub(crate) async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
@ -16,3 +18,12 @@ pub(crate) async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
}
Ok(())
}
pub trait TrelloBoards {
fn find_by_name(&self, board_name: &TrelloBoardName) -> Option<&TrelloBoard>;
}
impl TrelloBoards for Vec<TrelloBoard> {
fn find_by_name(&self, board_name: &TrelloBoardName) -> Option<&TrelloBoard> {
self.iter().find(|b| &b.name == board_name)
}
}

View file

@ -18,29 +18,13 @@ pub struct TrelloAuth<'cfg> {
pub(crate) api_key: &'cfg TrelloApiKey,
pub(crate) api_secret: &'cfg TrelloApiSecret,
}
impl<'cfg> TrelloAuth<'cfg> {
pub const fn new(api_key: &'cfg TrelloApiKey, api_secret: &'cfg TrelloApiSecret) -> Self {
Self {
api_key,
api_secret,
}
}
pub const fn api_key(&self) -> &TrelloApiKey {
&self.api_key
}
pub const fn api_token(&self) -> &TrelloApiSecret {
&self.api_secret
}
}
impl<'cfg> From<TrelloAuth<'cfg>> for HashMap<String, String> {
impl From<TrelloAuth<'_>> for HashMap<String, String> {
fn from(value: TrelloAuth) -> Self {
HashMap::from([(
"Authorization".into(),
format!(
r#"OAuth oauth_consumer_key="{}", oauth_token="{}""#,
value.api_key(),
value.api_token()
value.api_key, value.api_secret
),
)])
}