Commit 04e94ae6 authored by Ben Boeckel's avatar Ben Boeckel
Browse files

gitlab: expose hook addition API

Fixes #5.
parent 981262b0
......@@ -147,6 +147,33 @@ impl Gitlab {
self._get(&format!("projects/{}/hooks/{}", project, hook))
}
fn bool_param_value(value: bool) -> &'static str {
if value {
"true"
} else {
"false"
}
}
fn set_event_flags(request: &mut Request, events: WebhookEvents) {
request
.param("build_events", Self::bool_param_value(events.build()))
.param("issues_events", Self::bool_param_value(events.issues()))
.param("merge_requests_events", Self::bool_param_value(events.merge_requests()))
.param("note_events", Self::bool_param_value(events.note()))
.param("pipeline_events", Self::bool_param_value(events.pipeline()))
.param("push_events", Self::bool_param_value(events.push()))
.param("wiki_page_events", Self::bool_param_value(events.wiki_page()));
}
/// Add a project hook.
pub fn add_hook(&self, project: ProjectId, url: &str, events: WebhookEvents) -> GitlabResult<Hook> {
let mut req = try!(self._mkrequest(&format!("projects/{}/hooks", project)));
Self::set_event_flags(&mut req, events);
self._post(&format!("projects/{}/hooks", project))
}
/// Get the team members of a group.
pub fn group_members(&self, group: GroupId) -> GitlabResult<Vec<Member>> {
self._get_paged(&format!("groups/{}/members", group))
......
......@@ -66,3 +66,20 @@ macro_rules! enum_serialize {
}
};
}
macro_rules! with_event {
{ $method: ident, $name: ident } => {
pub fn $method(mut self) -> Self {
self.$name = true;
self
}
};
}
macro_rules! get_event {
{ $name: ident } => {
pub fn $name(&self) -> bool {
self.$name
}
};
}
......@@ -297,6 +297,47 @@ impl From<ProjectHook> for Hook {
}
}
#[derive(Default)]
pub struct WebhookEvents {
build: bool,
issues: bool,
merge_requests: bool,
note: bool,
pipeline: bool,
push: bool,
wiki_page: bool,
}
impl WebhookEvents {
fn new() -> Self {
WebhookEvents {
build: false,
issues: false,
merge_requests: false,
note: false,
pipeline: false,
push: false,
wiki_page: false,
}
}
with_event!{with_build, build}
with_event!{with_issues, issues}
with_event!{with_merge_requests, merge_requests}
with_event!{with_note, note}
with_event!{with_pipeline, pipeline}
with_event!{with_push, push}
with_event!{with_wiki_page, wiki_page}
get_event!{build}
get_event!{issues}
get_event!{merge_requests}
get_event!{note}
get_event!{pipeline}
get_event!{push}
get_event!{wiki_page}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
/// Type-safe project ID.
pub struct ProjectId(u64);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment