Redone registration:
ci/woodpecker/push/woodpecker Pipeline was successful Details

1. removed url
2. added owner_name, repo_name
This commit is contained in:
Martin Slot 2022-11-03 21:51:04 +01:00
parent e05993d53e
commit ed65e9a392
10 changed files with 108 additions and 51 deletions

View File

@ -29,7 +29,8 @@ pub enum Action {
RegisterOrigin {
vcsType: VcsType,
name: String,
url: String,
repo_name: String,
owner_name: String,
},
RegisterCurrent {
vcsType: VcsType,
@ -50,8 +51,12 @@ impl ArgumentParser {
.subcommand(
Command::new("register")
.subcommand(
Command::new("origin")
.subcommand(Command::new("github").arg(arg!([name])).arg(arg!([url]))),
Command::new("origin").subcommand(
Command::new("github")
.arg(arg!([name]))
.arg(arg!([owner_name]))
.arg(arg!([repo_name])),
),
)
.subcommand(
Command::new("current").subcommand(
@ -94,7 +99,8 @@ impl ArgumentParser {
Some(("origin", sub_matches)) => match sub_matches.subcommand() {
Some(("github", sub_matches)) => {
let name = sub_matches.get_one::<String>("name");
let url = sub_matches.get_one::<String>("url");
let owner_name = sub_matches.get_one::<String>("owner_name");
let repo_name = sub_matches.get_one::<String>("repo_name");
let vcs_type = VcsType::Github;
let name = match name {
@ -102,14 +108,20 @@ impl ArgumentParser {
None => return Err(ArgumentParserError::NameError),
};
let url = match url {
let owner_name = match owner_name {
Some(u) => u,
None => return Err(ArgumentParserError::UrlError),
};
let repo_name = match repo_name {
Some(u) => u,
None => return Err(ArgumentParserError::UrlError),
};
Ok(Action::RegisterOrigin {
name: name.to_string(),
vcsType: vcs_type,
url: url.to_string(),
owner_name: owner_name.to_string(),
repo_name: repo_name.to_string(),
})
}
_ => Err(ArgumentParserError::CommandMissingError),
@ -174,7 +186,8 @@ mod tests {
String::from("origin"),
String::from("github"),
String::from("name"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
];
let parser = ArgumentParser {};
@ -184,7 +197,8 @@ mod tests {
Ok(Action::RegisterOrigin {
name: String::from("name"),
vcsType: VcsType::Github,
url: String::from("https://url")
owner_name: String::from("owner_name"),
repo_name: String::from("repo_name"),
})
)
}

View File

@ -66,10 +66,11 @@ impl<'a> Application<'a> {
migrate(&self.settings); //TODO: this should be run for it self by calling a subcommand on the cli it self
// TODO: redo this, because this is just me trying out rust :) I can be done a lot easier, without doing the function delegation
let register_origin = |name, url, vcs_type| {
let register_origin = |name, owner_name, repo_name, vcs_type| {
self.register_origin_handler.execute(OriginRequest {
name,
url,
owner_name,
repo_name,
vcs_type,
});
};

View File

@ -17,8 +17,12 @@ pub struct RegisterOriginHandler<'a> {
impl<'a> RegisterOriginHandler<'a> {
pub fn execute(&self, request: OriginRequest) {
self.origin_gateway
.upsert_origin(request.name, request.url, request.vcs_type);
self.origin_gateway.upsert_origin(
request.name,
request.owner_name,
request.repo_name,
request.vcs_type,
);
}
}
@ -31,7 +35,13 @@ impl<'a> OriginGateway<'a> {
OriginGateway { settings }
}
pub fn upsert_origin(&self, name: String, url: String, vcs_type: VcsType) {
pub fn upsert_origin(
&self,
name: String,
owner_name: String,
repo_name: String,
vcs_type: VcsType,
) {
let connection = self.settings.database.get_connection();
//TODO: this should be wrapped somewhere else, because it is going to be used a lot of places. I think there must be some rustic way of doing this
@ -42,11 +52,13 @@ impl<'a> OriginGateway<'a> {
let mut statement = connection
.prepare(
"INSERT INTO Origins (name, url, vcs_type) VALUES(?1,?2,?3) ON CONFLICT(name, vcs_type) DO UPDATE SET url = ?2",
"INSERT INTO Origins (name, repo_name, owner_name, vcs_type) VALUES(?1,?2,?3,?4) ON CONFLICT(name, vcs_type) DO UPDATE SET repo_name = ?2, owner_name = ?3",
)
.unwrap();
statement.execute([&name, &url, &vcs_type]).unwrap();
statement
.execute([&name, &repo_name, &owner_name, &vcs_type])
.unwrap();
}
}
@ -55,13 +67,15 @@ pub mod models {
pub struct OriginRequest {
pub name: String,
pub url: String,
pub owner_name: String,
pub repo_name: String,
pub vcs_type: VcsType,
}
pub struct Origin {
pub name: String,
pub url: String,
pub owner_name: String,
pub repo_name: String,
pub vcs_type: VcsType,
}
}

View File

@ -2,7 +2,7 @@ use crate::configuration::{Action, VcsType};
pub struct Router<A, F, L, R>
where
A: Fn(String, String, VcsType),
A: Fn(String, String, String, VcsType),
F: Fn(String, String, VcsType),
L: Fn(),
R: Fn(),
@ -15,7 +15,7 @@ where
impl<A, F, L, R> Router<A, F, L, R>
where
A: Fn(String, String, VcsType),
A: Fn(String, String, String, VcsType),
F: Fn(String, String, VcsType),
L: Fn(),
R: Fn(),
@ -30,9 +30,12 @@ where
}
pub fn route(&self, action: Action) {
match action {
Action::RegisterOrigin { name, url, vcsType } => {
(self.register_origin)(name, url, vcsType)
}
Action::RegisterOrigin {
name,
owner_name,
repo_name,
vcsType,
} => (self.register_origin)(name, owner_name, repo_name, vcsType),
Action::Request => {}
Action::List => (self.list)(),
Action::RegisterCurrent {
@ -55,9 +58,10 @@ mod tests {
#[test]
fn register_origin_is_called_with_correct_parameters() {
let add = |name, url, vcs_type| {
let add = |name, owner_name, repo_name, vcs_type| {
assert_eq!("name", name);
assert_eq!("url", url);
assert_eq!("repo_name", repo_name);
assert_eq!("owner_name", owner_name);
assert_eq!(Github, vcs_type);
};
@ -70,14 +74,15 @@ mod tests {
router.route(Action::RegisterOrigin {
name: String::from("name"),
url: String::from("url"),
repo_name: String::from("repo_name"),
owner_name: String::from("owner_name"),
vcsType: Github,
});
}
#[test]
fn register_current_is_called_with_correct_parameters() {
let add = |name, url, vcs_type| {};
let add = |name, owner_name, repo_name, vcs_type| {};
let current = |name, version, vcs_type| {
assert_eq!("name", name);
@ -104,7 +109,7 @@ mod tests {
let is_list_called = Cell::new(false);
let is_request_called = Cell::new(false);
let add = |name, vcs_type, url| {
let add = |name, owner_name, repo_name, vcs_type| {
is_add_called.set(true);
};
@ -138,7 +143,7 @@ mod tests {
let is_list_called = Cell::new(false);
let is_request_called = Cell::new(false);
let add = |name, vcs_type, url| {
let add = |name, owner_name, repo_name, vcs_type| {
is_add_called.set(true);
};
@ -155,7 +160,8 @@ mod tests {
router.route(Action::RegisterOrigin {
name: String::from("name"),
url: String::from("url"),
owner_name: String::from("owner_name"),
repo_name: String::from("repo_name"),
vcsType: Github,
});

View File

@ -3,9 +3,10 @@
*/
CREATE TABLE IF NOT EXISTS Origins
(
name NVARCHAR(50),
url TEXT,
vcs_type NVARCHAR(20),
name NVARCHAR(50),
vcs_type NVARCHAR(20),
owner_name NVARCHAR(100),
repo_name NVARCHAR(100),
PRIMARY KEY (name, vcs_type)
);

View File

@ -26,13 +26,14 @@ pub fn get_current(name: String, connection: &Connection) -> Current {
pub fn get_origin(name: String, connection: &Connection) -> Origin {
let origin = connection
.query_row(
"SELECT name, vcs_type, url FROM Origins WHERE name=?1",
"SELECT name, vcs_type, owner_name, repo_name FROM Origins WHERE name=?1",
[&name],
|row| {
Ok(Origin {
name: row.get(0).unwrap(),
vcs_type: VcsType::Github,
url: row.get(2).unwrap(),
owner_name: row.get(2).unwrap(),
repo_name: row.get(3).unwrap(),
})
},
)
@ -66,10 +67,16 @@ pub fn insert_current(name: String, version: String, vcs_type: String, connectio
.unwrap();
}
pub fn insert_origin(name: String, url: String, vcs_type: String, connection: &Connection) {
pub fn insert_origin(
name: String,
owner_name: String,
repo_name: String,
vcs_type: String,
connection: &Connection,
) {
match connection.execute(
"INSERT INTO Origins (name, url, vcs_type) VALUES(?1, ?2, ?3)",
[&name, &url, &vcs_type],
"INSERT INTO Origins (name, owner_name, repo_name, vcs_type) VALUES(?1, ?2, ?3, ?4)",
[&name, &owner_name, &repo_name, &vcs_type],
) {
Ok(x) => println!("good"),
Err(e) => println!("{}", e),

View File

@ -19,7 +19,8 @@ fn should_print_current(context: &FunctionalTestContext) {
insert_origin(
String::from("name1"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);
@ -33,7 +34,8 @@ fn should_print_current(context: &FunctionalTestContext) {
insert_origin(
String::from("name2"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);
@ -47,7 +49,8 @@ fn should_print_current(context: &FunctionalTestContext) {
insert_origin(
String::from("name3"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);

View File

@ -26,7 +26,8 @@ fn shoud_save_only_one_current(context: &FunctionalTestContext) {
insert_origin(
String::from("name"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);
@ -56,7 +57,8 @@ fn shoud_update_on_second_insert(context: &FunctionalTestContext) {
insert_origin(
String::from("name"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);

View File

@ -18,7 +18,8 @@ fn shoud_save_only_one_origin(context: &FunctionalTestContext) {
String::from("origin"),
String::from("github"),
String::from("name"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
];
let application = Application::new(&settings, &console, parser, arguments);
application.run();
@ -41,7 +42,8 @@ fn shoud_update_on_second_insert(context: &FunctionalTestContext) {
String::from("origin"),
String::from("github"),
String::from("name"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
];
let application = Application::new(&settings, &console, parser, arguments);
let console = Console {};
@ -52,7 +54,8 @@ fn shoud_update_on_second_insert(context: &FunctionalTestContext) {
let origin = get_origin(String::from("name"), &settings.database.get_connection());
assert_eq!(1, total_count_of_origins);
assert_eq!(origin.url, "https://url");
assert_eq!(origin.owner_name, "owner_name");
assert_eq!(origin.repo_name, "repo_name");
assert_eq!(origin.name, "name");
assert_eq!(origin.vcs_type, VcsType::Github);
@ -65,7 +68,8 @@ fn shoud_update_on_second_insert(context: &FunctionalTestContext) {
String::from("origin"),
String::from("github"),
String::from("name"),
String::from("https://url_updated"),
String::from("owner_name_updated"),
String::from("repo_name_updated"),
];
let application = Application::new(&settings, &console, parser, arguments);
application.run();
@ -74,7 +78,8 @@ fn shoud_update_on_second_insert(context: &FunctionalTestContext) {
let origin = get_origin(String::from("name"), &settings.database.get_connection());
assert_eq!(1, total_count_of_origins);
assert_eq!(origin.url, "https://url_updated");
assert_eq!(origin.repo_name, "repo_name_updated");
assert_eq!(origin.owner_name, "owner_name_updated");
assert_eq!(origin.name, "name");
assert_eq!(origin.vcs_type, VcsType::Github);
}
@ -92,7 +97,8 @@ fn should_route_to_origin(context: &FunctionalTestContext) {
String::from("origin"),
String::from("github"),
String::from("name"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
];
let application = Application::new(&settings, &console, parser, arguments);
application.run();

View File

@ -16,7 +16,8 @@ fn should_make_request(context: &FunctionalTestContext) {
insert_origin(
String::from("name1"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);
@ -30,7 +31,8 @@ fn should_make_request(context: &FunctionalTestContext) {
insert_origin(
String::from("name2"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);
@ -44,7 +46,8 @@ fn should_make_request(context: &FunctionalTestContext) {
insert_origin(
String::from("name3"),
String::from("https://url"),
String::from("owner_name"),
String::from("repo_name"),
String::from("github"),
&context.settings.database.get_connection(),
);