-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c976a68
commit a0bf957
Showing
8 changed files
with
230 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,147 @@ | ||
use derive_setters::Setters; | ||
use serde::Serialize; | ||
use merge::Merge; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::SetEvent; | ||
|
||
#[derive(Serialize, Setters, Clone)] | ||
#[derive(Default, Setters, Debug, Serialize, Deserialize, Clone, Merge, PartialEq, Eq)] | ||
#[serde(rename_all = "snake_case")] | ||
#[setters(strip_option)] | ||
pub struct Event { | ||
pub struct EventValue { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub push: Option<Push>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub pull_request: Option<PullRequest>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub pull_request_target: Option<PullRequestTarget>, | ||
// TODO: add all more events | ||
} | ||
|
||
impl Default for Event { | ||
fn default() -> Self { | ||
Event { push: Some(Push::default()), pull_request: None } | ||
pub struct Event<A>(A); | ||
|
||
impl Event<Push> { | ||
pub fn push() -> Self { | ||
Event(Push::default()) | ||
} | ||
} | ||
|
||
impl Event<PullRequest> { | ||
pub fn pull_request() -> Self { | ||
Event(PullRequest::default()) | ||
} | ||
} | ||
|
||
impl SetEvent for Event { | ||
impl<A: Into<EventValue>> SetEvent for Event<A> { | ||
fn apply(self, mut workflow: crate::Workflow) -> crate::Workflow { | ||
workflow.on = serde_json::to_value(self).ok(); | ||
let mut on: EventValue = self.0.into(); | ||
if let Some(other) = workflow.on { | ||
on.merge(other); | ||
} | ||
workflow.on = Some(on); | ||
workflow | ||
} | ||
} | ||
|
||
#[derive(Default, Serialize, Clone)] | ||
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)] | ||
#[serde(rename_all = "snake_case")] | ||
pub struct Push { | ||
branches: Vec<String>, | ||
} | ||
|
||
impl Push { | ||
impl Event<Push> { | ||
pub fn branch<S: ToString>(mut self, branch: S) -> Self { | ||
self.branches.push(branch.to_string()); | ||
self.0.branches.push(branch.to_string()); | ||
self | ||
} | ||
} | ||
|
||
#[derive(Default, Serialize, Clone)] | ||
impl From<Push> for EventValue { | ||
fn from(value: Push) -> Self { | ||
EventValue::default().push(value) | ||
} | ||
} | ||
|
||
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)] | ||
#[serde(rename_all = "snake_case")] | ||
pub struct PullRequest { | ||
types: Vec<String>, | ||
branches: Vec<String>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
types: Option<Vec<String>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
branches: Option<Vec<String>>, | ||
} | ||
|
||
impl PullRequest { | ||
impl Event<PullRequest> { | ||
pub fn branch<S: ToString>(mut self, branch: S) -> Self { | ||
self.branches.push(branch.to_string()); | ||
let mut branches = self.0.branches.unwrap_or_default(); | ||
branches.push(branch.to_string()); | ||
self.0.branches = Some(branches); | ||
self | ||
} | ||
|
||
pub fn open(mut self) -> Self { | ||
self.types.push("opened".to_string()); | ||
fn add_type(mut self, ty: &str) -> Self { | ||
let mut types = self.0.types.unwrap_or_default(); | ||
types.push(ty.to_string()); | ||
self.0.types = Some(types); | ||
self | ||
} | ||
|
||
pub fn synchronize(mut self) -> Self { | ||
self.types.push("synchronize".to_string()); | ||
pub fn open(self) -> Self { | ||
self.add_type("opened") | ||
} | ||
|
||
pub fn synchronize(self) -> Self { | ||
self.add_type("synchronize") | ||
} | ||
|
||
pub fn reopen(self) -> Self { | ||
self.add_type("reopened") | ||
} | ||
} | ||
|
||
impl From<PullRequest> for EventValue { | ||
fn from(value: PullRequest) -> Self { | ||
EventValue::default().pull_request(value) | ||
} | ||
} | ||
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)] | ||
#[serde(rename_all = "snake_case")] | ||
pub struct PullRequestTarget { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
types: Option<Vec<String>>, | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
branches: Option<Vec<String>>, | ||
} | ||
|
||
impl Event<PullRequestTarget> { | ||
pub fn branch<S: ToString>(mut self, branch: S) -> Self { | ||
let mut branches = self.0.branches.unwrap_or_default(); | ||
branches.push(branch.to_string()); | ||
self.0.branches = Some(branches); | ||
self | ||
} | ||
|
||
pub fn reopen(mut self) -> Self { | ||
self.types.push("reopened".to_string()); | ||
fn add_type(mut self, ty: &str) -> Self { | ||
let mut types = self.0.types.unwrap_or_default(); | ||
types.push(ty.to_string()); | ||
self.0.types = Some(types); | ||
self | ||
} | ||
|
||
pub fn open(self) -> Self { | ||
self.add_type("opened") | ||
} | ||
|
||
pub fn synchronize(self) -> Self { | ||
self.add_type("synchronize") | ||
} | ||
|
||
pub fn reopen(self) -> Self { | ||
self.add_type("reopened") | ||
} | ||
} | ||
|
||
impl From<PullRequestTarget> for EventValue { | ||
fn from(value: PullRequestTarget) -> Self { | ||
EventValue::default().pull_request_target(value) | ||
} | ||
} |
Oops, something went wrong.