Commit 5bcd1584 authored by Brad King's avatar Brad King Committed by Kitware Robot
Browse files

Merge topic 'by-name-apis'

f996a515 webhooks: expose PushHook::user_username
ee55d115 gitlab: add more APIs which accept project names
fb4992ce

 gitlab: refactor out escaping project names
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Reviewed-by: Brad King's avatarBrad King <brad.king@kitware.com>
Merge-request: !130
parents e5d0ff2e f996a515
......@@ -7,7 +7,7 @@
// except according to those terms.
use crates::itertools::Itertools;
use crates::percent_encoding::{PATH_SEGMENT_ENCODE_SET, percent_encode};
use crates::percent_encoding::{PATH_SEGMENT_ENCODE_SET, PercentEncode, utf8_percent_encode};
use crates::reqwest::{Client, Method, RequestBuilder, Url};
use crates::serde::{Deserialize, Deserializer, Serializer};
use crates::serde::de::Error as SerdeError;
......@@ -164,12 +164,17 @@ impl Gitlab {
self.get(&format!("projects/{}", project))
}
/// A URL-safe name for projects.
fn url_name(name: &str) -> PercentEncode<PATH_SEGMENT_ENCODE_SET> {
utf8_percent_encode(name, PATH_SEGMENT_ENCODE_SET)
}
/// Find a project by name.
pub fn project_by_name<N>(&self, name: N) -> Result<Project>
where N: AsRef<str>,
{
self.get(&format!("projects/{}",
percent_encode(name.as_ref().as_bytes(), PATH_SEGMENT_ENCODE_SET)))
Self::url_name(name.as_ref())))
}
/// Get a project's hooks.
......@@ -243,6 +248,18 @@ impl Gitlab {
&[("user", &user_str), ("access", &access_str)])
}
/// Add a user to a project.
pub fn add_user_to_project_by_name<P>(&self, project: P, user: UserId, access: AccessLevel)
-> Result<Member>
where P: AsRef<str>,
{
let user_str = format!("{}", user);
let access_str = format!("{}", access);
self.post_with_param(&format!("projects/{}/members", Self::url_name(project.as_ref())),
&[("user", &user_str), ("access", &access_str)])
}
/// Get branches for a project.
pub fn branches(&self, project: ProjectId) -> Result<Vec<RepoBranch>> {
self.get_paged(&format!("projects/{}/branches", project))
......@@ -254,7 +271,7 @@ impl Gitlab {
{
self.get(&format!("projects/{}/repository/branches/{}",
project,
percent_encode(branch.as_ref().as_bytes(), PATH_SEGMENT_ENCODE_SET)))
Self::url_name(branch.as_ref())))
}
/// Get a commit.
......@@ -287,6 +304,19 @@ impl Gitlab {
&[("note", body.as_ref())])
}
/// Get comments on a commit.
pub fn create_commit_comment_by_name<P, C, B>(&self, project: P, commit: C, body: B)
-> Result<CommitNote>
where P: AsRef<str>,
C: AsRef<str>,
B: AsRef<str>,
{
self.post_with_param(&format!("projects/{}/repository/commits/{}/comment",
Self::url_name(project.as_ref()),
commit.as_ref()),
&[("note", body.as_ref())])
}
/// Get comments on a commit.
pub fn create_commit_line_comment(&self, project: ProjectId, commit: &str, body: &str,
path: &str, line: u64)
......@@ -313,6 +343,17 @@ impl Gitlab {
commit.as_ref()))
}
/// Get the latest statuses of a commit.
pub fn commit_latest_statuses_by_name<P, C>(&self, project: P, commit: C)
-> Result<Vec<CommitStatus>>
where P: AsRef<str>,
C: AsRef<str>,
{
self.get_paged(&format!("projects/{}/repository/commits/{}/statuses",
Self::url_name(project.as_ref()),
commit.as_ref()))
}
/// Get the all statuses of a commit.
pub fn commit_all_statuses<C>(&self, project: ProjectId, commit: C)
-> Result<Vec<CommitStatus>>
......@@ -359,6 +400,25 @@ impl Gitlab {
self.post_with_param(path, &params)
}
/// Create a status message for a commit.
pub fn create_commit_status_by_name<P, S>(&self, project: P, sha: S, state: StatusState,
info: &CommitStatusInfo)
-> Result<CommitStatus>
where P: AsRef<str>,
S: AsRef<str>,
{
let path = &format!("projects/{}/statuses/{}", Self::url_name(project.as_ref()), sha.as_ref());
let mut params = vec![("state", state.as_str())];
info.refname.map(|v| params.push(("ref", v)));
info.name.map(|v| params.push(("name", v)));
info.target_url.map(|v| params.push(("target_url", v)));
info.description.map(|v| params.push(("description", v)));
self.post_with_param(path, &params)
}
/// Get the issues for a project.
pub fn issues(&self, project: ProjectId) -> Result<Vec<Issue>> {
self.get_paged(&format!("projects/{}/issues", project))
......@@ -374,6 +434,13 @@ impl Gitlab {
self.get_paged(&format!("projects/{}/issues/{}/notes", project, issue))
}
/// Get the notes from a issue.
pub fn issue_notes_by_name<P>(&self, project: P, issue: IssueInternalId) -> Result<Vec<Note>>
where P: AsRef<str>,
{
self.get_paged(&format!("projects/{}/issues/{}/notes", Self::url_name(project.as_ref()), issue))
}
/// Create a note on a issue.
pub fn create_issue_note<C>(&self, project: ProjectId, issue: IssueInternalId, content: C)
-> Result<Note>
......@@ -384,6 +451,17 @@ impl Gitlab {
self.post_with_param(path, &[("body", content.as_ref())])
}
/// Create a note on a issue.
pub fn create_issue_note_by_name<P, C>(&self, project: P, issue: IssueInternalId, content: C)
-> Result<Note>
where P: AsRef<str>,
C: AsRef<str>,
{
let path = &format!("projects/{}/issues/{}/notes", Self::url_name(project.as_ref()), issue);
self.post_with_param(path, &[("body", content.as_ref())])
}
/// Get the merge requests for a project.
pub fn merge_requests(&self, project: ProjectId) -> Result<Vec<MergeRequest>> {
self.get_paged(&format!("projects/{}/merge_requests", project))
......@@ -418,6 +496,16 @@ impl Gitlab {
merge_request))
}
/// Get the notes from a merge request.
pub fn merge_request_notes_by_name<P>(&self, project: P, merge_request: MergeRequestInternalId)
-> Result<Vec<Note>>
where P: AsRef<str>,
{
self.get_paged(&format!("projects/{}/merge_requests/{}/notes",
Self::url_name(project.as_ref()),
merge_request))
}
/// Award a merge request note with an award.
pub fn award_merge_request_note(&self, project: ProjectId, merge_request: MergeRequestInternalId,
note: NoteId, award: &str)
......@@ -429,6 +517,19 @@ impl Gitlab {
self.post_with_param(path, &[("name", award)])
}
/// Award a merge request note with an award.
pub fn award_merge_request_note_by_name<P>(&self, project: P, merge_request: MergeRequestInternalId,
note: NoteId, award: &str)
-> Result<AwardEmoji>
where P: AsRef<str>,
{
let path = &format!("projects/{}/merge_requests/{}/notes/{}/award_emoji",
Self::url_name(project.as_ref()),
merge_request,
note);
self.post_with_param(path, &[("name", award)])
}
/// Get the awards for a merge request.
pub fn merge_request_awards(&self, project: ProjectId, merge_request: MergeRequestInternalId)
-> Result<Vec<AwardEmoji>> {
......@@ -437,6 +538,16 @@ impl Gitlab {
merge_request))
}
/// Get the awards for a merge request.
pub fn merge_request_awards_by_name<P>(&self, project: P, merge_request: MergeRequestInternalId)
-> Result<Vec<AwardEmoji>>
where P: AsRef<str>,
{
self.get_paged(&format!("projects/{}/merge_requests/{}/award_emoji",
Self::url_name(project.as_ref()),
merge_request))
}
/// Get the awards for a merge request note.
pub fn merge_request_note_awards(&self, project: ProjectId, merge_request: MergeRequestInternalId,
note: NoteId)
......@@ -447,6 +558,18 @@ impl Gitlab {
note))
}
/// Get the awards for a merge request note.
pub fn merge_request_note_awards_by_name<P>(&self, project: P, merge_request: MergeRequestInternalId,
note: NoteId)
-> Result<Vec<AwardEmoji>>
where P: AsRef<str>,
{
self.get_paged(&format!("projects/{}/merge_requests/{}/notes/{}/award_emoji",
Self::url_name(project.as_ref()),
merge_request,
note))
}
/// Create a note on a merge request.
pub fn create_merge_request_note(&self, project: ProjectId, merge_request: MergeRequestInternalId,
content: &str)
......@@ -457,6 +580,18 @@ impl Gitlab {
self.post_with_param(path, &[("body", content)])
}
/// Create a note on a merge request.
pub fn create_merge_request_note_by_name<P>(&self, project: P, merge_request: MergeRequestInternalId,
content: &str)
-> Result<Note>
where P: AsRef<str>,
{
let path = &format!("projects/{}/merge_requests/{}/notes",
Self::url_name(project.as_ref()),
merge_request);
self.post_with_param(path, &[("body", content)])
}
/// Get issues closed by a merge request.
pub fn get_issues_closed_by_merge_request(&self, project: ProjectId,
merge_request: MergeRequestInternalId)
......@@ -467,6 +602,18 @@ impl Gitlab {
self.get_paged(path)
}
/// Get issues closed by a merge request.
pub fn get_issues_closed_by_merge_request_by_name<P>(&self, project: P,
merge_request: MergeRequestInternalId)
-> Result<Vec<Issue>>
where P: AsRef<str>,
{
let path = &format!("projects/{}/merge_requests/{}/closes_issues",
Self::url_name(project.as_ref()),
merge_request);
self.get_paged(path)
}
/// Set the labels on an issue.
pub fn set_issue_labels<I, L>(&self, project: ProjectId, issue: IssueInternalId, labels: I)
-> Result<Issue>
......@@ -479,6 +626,19 @@ impl Gitlab {
self.put_with_param(path, &[("labels", labels.into_iter().join(","))])
}
/// Set the labels on an issue.
pub fn set_issue_labels_by_name<P, I, L>(&self, project: P, issue: IssueInternalId, labels: I)
-> Result<Issue>
where P: AsRef<str>,
I: IntoIterator<Item = L>,
L: Display,
{
let path = &format!("projects/{}/issues/{}",
Self::url_name(project.as_ref()),
issue);
self.put_with_param(path, &[("labels", labels.into_iter().join(","))])
}
/// Set the labels on a merge request.
pub fn set_merge_request_labels<I, L>(&self, project: ProjectId, merge_request: MergeRequestInternalId, labels: I)
-> Result<MergeRequest>
......
......@@ -169,6 +169,8 @@ pub struct PushHook {
pub user_id: UserId,
/// The name of the user who pushed.
pub user_name: String,
/// The username of the user who pushed.
pub user_username: String,
/// The email address of the user who pushed.
pub user_email: String,
/// The URL of the user's avatar.
......
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