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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use rusty_yaml::Yaml;
use crate::unwrap;
use std::fmt::{Display, Error, Formatter};
use std::process::exit;
pub enum VersionControlSystem {
GitHub,
Unsupported,
}
pub struct MergeRequestHandler {
vcs: VersionControlSystem,
owner: String,
repo_name: String,
whitelist: Vec<String>,
auth_token: String,
repository_type: String,
}
impl MergeRequestHandler {
pub fn new(
vcs: VersionControlSystem,
owner: String,
repo_name: String,
whitelist: Vec<String>,
auth_token: String,
) -> Self {
Self {
vcs,
owner,
repo_name,
whitelist,
auth_token,
repository_type: String::from("git"),
}
}
}
impl Display for MergeRequestHandler {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match &self.vcs {
VersionControlSystem::GitHub => write!(
f,
"whitelist_authors = {:?}
def github_pull_check(pull_request):
sender = pull_request[\"user\"][\"login\"]
for author in whitelist_authors:
if author == sender:
return True
return False
try:
c['change_source'].append(changes.GitHubPullrequestPoller(
owner=\"{owner}\",
repo=\"{name}\",
# right now just poll every 60 seconds
# this will need to change in the future, but this is just for testing.
pollInterval=60,
pullrequest_filter=github_pull_check,
repository_type=\"{repository_type}\",
token=\"{token}\"))
except Exception as e:
print(f\"Could not create merge request handler: {{str(e)}}\")
",
self.whitelist,
token = self.auth_token.trim_matches('"'),
name = self.repo_name.trim_matches('"'),
owner = self.owner.trim_matches('"'),
repository_type = self.repository_type.trim_matches('"'),
),
VersionControlSystem::Unsupported => write!(
f,
"print('We currently dont support building merge requests on your VCS.')"
),
}
}
}
impl From<Yaml> for MergeRequestHandler {
fn from(yaml: Yaml) -> Self {
for section in [
"version-control-system",
"owner",
"repo-name",
"whitelist",
"auth-token",
]
.iter()
{
if !yaml.has_section(section) {
error!("There was an error creating the merge request handler: '{}' section not specified", section);
exit(1);
}
}
let vcs: VersionControlSystem = match
unwrap(&yaml, "version-control-system").as_str()
{
"github" => VersionControlSystem::GitHub,
_ => {
warn!(
"We do not support building merge requests on your version control system yet!"
);
warn!(
"We will proceed with the build. All other features should function as intended."
);
VersionControlSystem::Unsupported
}
};
let owner: String = unwrap(&yaml, "owner");
let repo_name: String = unwrap(&yaml, "repo-name");
let mut whitelist: Vec<String> = vec![];
for author in yaml.get_section("whitelist").unwrap() {
whitelist.push(
author.to_string()
.trim_matches('"')
.trim_matches('\'')
.to_string()
);
}
let auth_token: String = unwrap(&yaml, "auth-token");
if auth_token.len() == 0 {
error!("You cannot have an empty authentication token!");
exit(1);
}
Self::new(vcs, owner, repo_name, whitelist, auth_token)
}
}