gitlab/api/projects/hooks/
hook.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use derive_builder::Builder;
8
9use crate::api::common::NameOrId;
10use crate::api::endpoint_prelude::*;
11
12/// Query for a webhook within a project.
13#[derive(Debug, Builder, Clone)]
14pub struct Hook<'a> {
15    /// The project to query for webhooks.
16    #[builder(setter(into))]
17    project: NameOrId<'a>,
18    /// The ID of the hook.
19    hook: u64,
20}
21
22impl<'a> Hook<'a> {
23    /// Create a builder for the endpoint.
24    pub fn builder() -> HookBuilder<'a> {
25        HookBuilder::default()
26    }
27}
28
29impl Endpoint for Hook<'_> {
30    fn method(&self) -> Method {
31        Method::GET
32    }
33
34    fn endpoint(&self) -> Cow<'static, str> {
35        format!("projects/{}/hooks/{}", self.project, self.hook).into()
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use crate::api::projects::hooks::{Hook, HookBuilderError};
42    use crate::api::{self, Query};
43    use crate::test::client::{ExpectedUrl, SingleTestClient};
44
45    #[test]
46    fn project_and_hook_are_needed() {
47        let err = Hook::builder().build().unwrap_err();
48        crate::test::assert_missing_field!(err, HookBuilderError, "project");
49    }
50
51    #[test]
52    fn project_is_needed() {
53        let err = Hook::builder().hook(1).build().unwrap_err();
54        crate::test::assert_missing_field!(err, HookBuilderError, "project");
55    }
56
57    #[test]
58    fn hook_is_needed() {
59        let err = Hook::builder().project(1).build().unwrap_err();
60        crate::test::assert_missing_field!(err, HookBuilderError, "hook");
61    }
62
63    #[test]
64    fn project_and_hook_are_sufficient() {
65        Hook::builder().project(1).hook(1).build().unwrap();
66    }
67
68    #[test]
69    fn endpoint() {
70        let endpoint = ExpectedUrl::builder()
71            .endpoint("projects/simple%2Fproject/hooks/1")
72            .build()
73            .unwrap();
74        let client = SingleTestClient::new_raw(endpoint, "");
75
76        let endpoint = Hook::builder()
77            .project("simple/project")
78            .hook(1)
79            .build()
80            .unwrap();
81        api::ignore(endpoint).query(&client).unwrap();
82    }
83}