Skip to content

Commit

Permalink
feat(core): Implement per-repository secret configure
Browse files Browse the repository at this point in the history
* test: Implement new configure file test

Signed-off-by: KunoiSayami <[email protected]>
  • Loading branch information
KunoiSayami committed Dec 14, 2021
1 parent 675b807 commit dea8b03
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "github-webhook-notification"
version = "2.1.0-alpha"
version = "2.1.0-beta"
edition = "2021"

[dependencies]
Expand Down
16 changes: 10 additions & 6 deletions example/sample.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
[server]
bind = "0.0.0.0"
bind = "127.0.0.1"
port = 11451
secrets = "1145141919810"
#token = ""
#token = "henghengaaaaaaa"

[telegram]
bot_token = "1145141919:810abcdefg"
send_to = [114514, "1919810"]
send_to = [114514, 1919810]

[[repository]]
full_name = "114514/1919810"
send_to = [1, 4, 5, 9, 8, 0]
full_name = "MonsterSenpai/SummerNight-HornyFantasy"
send_to = [11, 4, 514, 1919, 81, 0]

[[repository]]
full_name = "2147483647/114514"
full_name = "BillyKing/Wrestling"
send_to = 233
branch_ignore = ["test", "2323"]

[[repository]]
full_name = "sample/test"
secrets = "2333"
71 changes: 70 additions & 1 deletion src/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub struct TomlRepository {
full_name: String,
send_to: Option<Value>,
branch_ignore: Option<Vec<String>>,
secrets: Option<String>,
}

impl TomlRepository {
Expand All @@ -89,13 +90,17 @@ impl TomlRepository {
pub fn branch_ignore(&self) -> &Option<Vec<String>> {
&self.branch_ignore
}
pub fn secrets(&self) -> &Option<String> {
&self.secrets
}
}

#[derive(Debug, Clone)]
pub struct Repository {
//full_name: String,
send_to: Vec<i64>,
branch_ignore: Vec<String>,
secrets: String,
}

impl Repository {
Expand All @@ -108,6 +113,9 @@ impl Repository {
pub fn branch_ignore(&self) -> &Vec<String> {
&self.branch_ignore
}
pub fn secrets(&self) -> &String {
&self.secrets
}
}

impl From<&TomlRepository> for Repository {
Expand All @@ -116,16 +124,64 @@ impl From<&TomlRepository> for Repository {
//full_name: repo.full_name().clone(),
send_to: match repo.send_to() {
None => vec![],
Some(v) => parse_value(v)
Some(v) => parse_value(v),
},
branch_ignore: match repo.branch_ignore() {
Some(v) => v.clone(),
None => vec![],
},
secrets: match repo.secrets() {
None => "".to_string(),
Some(ref secret) => secret.clone(),
},
}
}
}

/*impl From<&Config> for Repository {
fn from(s: &Config) -> Self {
Self {
send_to: s.telegram().send_to().clone(),
secrets: Some(s.server().secrets().clone()),
..Default::default()
}
}
}*/

#[derive(Debug, Default, Clone)]
pub struct RepositoryBuilder {
send_to: Vec<i64>,
branch_ignore: Vec<String>,
secrets: String,
}

impl RepositoryBuilder {
pub fn set_send_to(&mut self, send_to: Vec<i64>) -> &mut Self {
self.send_to = send_to;
self
}
#[allow(unused)]
pub fn set_branch_ignore(&mut self, branch_ignore: Vec<String>) -> &mut Self {
self.branch_ignore = branch_ignore;
self
}
pub fn set_secrets(&mut self, secrets: &String) -> &mut Self{
self.secrets = secrets.clone();
self
}
pub fn build(&self) -> Repository {
Repository {
send_to: self.send_to.clone(),
branch_ignore: self.branch_ignore.clone(),
secrets: self.secrets.clone(),
}
}
pub fn new() -> Self {
Self { ..Default::default() }
}
}


#[derive(Debug, Clone)]
pub struct Telegram {
bot_token: String,
Expand Down Expand Up @@ -217,6 +273,19 @@ impl Config {
let config = TomlConfig::new(path)?;
Ok(Self::from(&config))
}

pub fn fetch_repository_configure(&self, branch_name: &str) -> Repository {
let conf = self.repo_mapping().get(branch_name);
match conf {
None => {
RepositoryBuilder::new()
.set_send_to(self.telegram().send_to().clone())
.set_secrets(self.server().secrets())
.build()
}
Some(repository) => repository.clone()
}
}
}

impl From<&TomlConfig> for Config {
Expand Down
33 changes: 33 additions & 0 deletions src/datastructures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ where
}
}

#[derive(Deserialize, Serialize, Debug)]
pub struct GitHubEarlyParse {
repository: Repository,
}

impl GitHubEarlyParse {
pub fn repository(&self) -> &Repository {
&self.repository
}

pub fn get_full_name(&self) -> &String {
self.repository().full_name()
}
}

#[derive(Deserialize, Serialize, Debug)]
pub struct GitHubPingEvent {
zen: String,
Expand Down Expand Up @@ -277,3 +292,21 @@ impl Guard for AuthorizationGuard {
false
}
}

#[derive(Debug, Clone)]
pub struct CommandBundle {
receiver: Vec<i64>,
text: String,
}

impl CommandBundle {
pub fn new(receiver: Vec<i64>, text: String) -> Self {
Self { receiver, text }
}
pub fn receiver(&self) -> &Vec<i64> {
&self.receiver
}
pub fn text(&self) -> &str {
&self.text
}
}
Loading

0 comments on commit dea8b03

Please sign in to comment.