Commit 5509959a authored by Ben Boeckel's avatar Ben Boeckel Committed by Kitware Robot
Browse files

Merge topic 'hook'

7d48e3c6 Handle the hook token
175e23b7 Added branch protection
207b33b5

 Add type for protected branches
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !190
parents 04918a1a 7d48e3c6
......@@ -653,17 +653,28 @@ impl Gitlab {
}
/// Add a project hook.
pub fn add_hook<U>(
pub fn add_hook<U, T>(
&self,
project: ProjectId,
url: U,
enable_ssl_verification: Option<bool>,
token: Option<T>,
events: WebhookEvents,
) -> GitlabResult<ProjectHook>
where
U: AsRef<str>,
T: AsRef<str>,
{
let mut flags = Self::event_flags(events);
flags.push(("url", url.as_ref()));
let s: String;
if let Some(ssl) = enable_ssl_verification {
s = ssl.to_string();
flags.push(("enable_ssl_verification", s.as_str()));
}
if let Some(tk) = token.as_ref() {
flags.push(("token", tk.as_ref()));
}
self.post_with_param(format!("projects/{}/hooks", project), &flags)
}
......@@ -810,6 +821,44 @@ impl Gitlab {
)
}
/// Protect a branch
///
/// # Arguments
/// * project: The project id
/// * branch: The name of the branch or wildcard
/// * push_access_level: Access level allowed to push (defaults: maintainers)
/// * merge_access_level: Access level allowed to merge (defaults: maintainers)
/// * unprotect_access_level: Access level allowed to unproctect (defaults: mainainers)
pub fn protect_branch<B: AsRef<str>>(
&self,
project: ProjectId,
branch: B,
push_access_level: Option<AccessLevel>,
merge_access_level: Option<AccessLevel>,
unprotect_access_level: Option<AccessLevel>,
) -> GitlabResult<ProtectedRepoBranch> {
let url = format!("projects/{}/protected_branches", project);
self.post_with_param(
url,
&[
("name", branch.as_ref()),
(
"push_access_level",
&u64::from(push_access_level.unwrap_or(AccessLevel::Maintainer)).to_string(),
),
(
"merge_access_level",
&u64::from(merge_access_level.unwrap_or(AccessLevel::Maintainer)).to_string(),
),
(
"unprotect_access_level",
&u64::from(unprotect_access_level.unwrap_or(AccessLevel::Maintainer))
.to_string(),
),
],
)
}
/// Get a commit.
pub fn commit<C>(&self, project: ProjectId, commit: C) -> GitlabResult<RepoCommitDetail>
where
......
......@@ -344,6 +344,8 @@ pub struct ProjectHook {
pub pipeline_events: bool,
/// Whether the hook is contacted for wiki page events.
pub wiki_page_events: bool,
/// Secret token to validate received payloads
pub token: Option<String>,
}
impl From<ProjectHook> for Hook {
......@@ -694,6 +696,8 @@ pub enum AccessLevel {
Maintainer,
/// Owner access (full rights).
Owner,
/// Admin access (full rights).
Admin,
}
impl From<AccessLevel> for u64 {
......@@ -705,13 +709,16 @@ impl From<AccessLevel> for u64 {
AccessLevel::Developer => 30,
AccessLevel::Maintainer => 40,
AccessLevel::Owner => 50,
AccessLevel::Admin => 60,
}
}
}
impl From<u64> for AccessLevel {
fn from(access: u64) -> Self {
if access >= 50 {
if access >= 60 {
AccessLevel::Admin
} else if access >= 50 {
AccessLevel::Owner
} else if access >= 40 {
AccessLevel::Maintainer
......@@ -730,6 +737,7 @@ impl From<u64> for AccessLevel {
impl AccessLevel {
pub fn as_str(&self) -> &str {
match self {
AccessLevel::Admin => "admin",
AccessLevel::Owner => "owner",
AccessLevel::Developer => "developer",
AccessLevel::Anonymous => "anonymous",
......@@ -946,6 +954,21 @@ pub struct RepoBranch {
pub default: Option<bool>,
}
#[derive(Deserialize, Debug, Clone)]
pub struct PRBAccessLevel {
access_level: u64,
access_level_description: String,
}
/// A protected branch on a repository
#[derive(Deserialize, Debug, Clone)]
pub struct ProtectedRepoBranch {
pub name: String,
pub push_access_levels: Vec<PRBAccessLevel>,
pub merge_access_levels: Vec<PRBAccessLevel>,
pub code_owner_approval_required: bool,
}
/// The ID of a git object.
#[derive(Serialize, Deserialize, Debug, Clone, Hash, PartialEq, Eq)]
pub struct ObjectId(String);
......
Markdown is supported
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