Skip to content

Commit

Permalink
update event
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Nov 8, 2024
1 parent c976a68 commit a0bf957
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 46 deletions.
92 changes: 85 additions & 7 deletions Cargo.lock

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

23 changes: 11 additions & 12 deletions workspace/gh-workflow-gen/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ fn main() {
Workflow::new("CI")
.env(rust_flags)
.permissions(Permissions::read())
.on(Event::default()
.push(Push::default().branch("main"))
.pull_request(
PullRequest::default()
.open()
.synchronize()
.reopen()
.branch("main"),
))
.on(Event::push().branch("main"))
.on(Event::pull_request()
.open()
.synchronize()
.reopen()
.branch("main"))
.add_job(
"build",
Job::new("Build and Test")
.add_step(Step::checkout())
.add_step(
Step::setup_rust()
.add_toolchain(Toolchain::Stable)
.add_toolchain(Toolchain::Nightly)
.components(vec![Component::Clippy, Component::Rustfmt]),
.toolchain_stable()
.toolchain_nightly()
.component_clippy()
.component_rustfmt(),
)
// TODO: Improve type-safety and intellisense
.add_step(Step::cargo("test", vec!["--all-features", "--workspace"]))
.add_step(Step::cargo_nightly("fmt", vec!["--check"]))
.add_step(Step::cargo_nightly(
Expand Down
3 changes: 2 additions & 1 deletion workspace/gh-workflow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ async-trait = "0.1.83"
derive_more = { version = "1.0.0", features = ["from"] }
derive_setters = "0.1.6"
indexmap = { version = "2.6.0", features = ["serde"] }
merge = "0.1.0"
serde = { version = "1.0.210", features = ["derive"] }
serde_json = { version = "1.0.128", features = ["preserve_order"] }
serde_json = { version = "1.0.128" }
serde_yaml = "0.9.34"

[dev-dependencies]
Expand Down
123 changes: 101 additions & 22 deletions workspace/gh-workflow/src/event.rs
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)
}
}
Loading

0 comments on commit a0bf957

Please sign in to comment.