gh_workflow/
event.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use derive_setters::Setters;
use merge::Merge;
use serde::{Deserialize, Serialize};

#[derive(Default, Setters, Debug, Serialize, Deserialize, Clone, Merge, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
#[setters(strip_option, into)]
pub struct Event {
    #[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
}

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct Push {
    branches: Vec<String>,
}

impl Push {
    pub fn add_branch<S: ToString>(mut self, branch: S) -> Self {
        self.branches.push(branch.to_string());
        self
    }

    pub fn branches(self, branches: Vec<String>) -> Self {
        Push { branches }
    }
}

impl From<Push> for Event {
    fn from(value: Push) -> Self {
        Event::default().push(value)
    }
}

#[derive(Debug, Setters, Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct PullRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    types: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    branches: Option<Vec<String>>,
}

impl PullRequest {
    pub fn add_branch<S: ToString>(mut self, branch: S) -> Self {
        let mut branches = self.branches.unwrap_or_default();
        branches.push(branch.to_string());
        self.branches = Some(branches);
        self
    }

    fn add_type(mut self, ty: &str) -> Self {
        let mut types = self.types.unwrap_or_default();
        types.push(ty.to_string());
        self.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<PullRequest> for Event {
    fn from(value: PullRequest) -> Self {
        Event::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 PullRequestTarget {
    pub fn add_branch<S: ToString>(mut self, branch: S) -> Self {
        let mut branches = self.branches.unwrap_or_default();
        branches.push(branch.to_string());
        self.branches = Some(branches);
        self
    }

    fn add_type(mut self, ty: &str) -> Self {
        let mut types = self.types.unwrap_or_default();
        types.push(ty.to_string());
        self.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 Event {
    fn from(value: PullRequestTarget) -> Self {
        Event::default().pull_request_target(value)
    }
}