Skip to content

Commit 7115933

Browse files
mathstufkwrobot
authored andcommitted
Merge topic 'add-commit-reference-endpoint'
8673fa7 Add CommitReference endpoint Acked-by: Kitware Robot <[email protected]> Tested-by: buildbot <[email protected]> Reviewed-by: Ben Boeckel <[email protected]> Merge-request: !464
2 parents 041f383 + 8673fa7 commit 7115933

File tree

4 files changed

+178
-1
lines changed

4 files changed

+178
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* GraphQL support has been updated to use `graphql_client` 0.14.
66
* The `http` crate has been updated to 1.x.
77

8+
## Additions
9+
* Add `api::projects::repository::commits::refs` endpoint
10+
811
# v0.1610.0
912

1013
## Additions

src/api/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ These API endpoints have been implemented.
192192
* `GET /projects/:project/repository/commits` `projects/repository/commits/commits.rs`
193193
* `POST /projects/:project/repository/commits` `projects/repository/commits/create.rs`
194194
* `GET /projects/:project/repository/commits/:sha` `projects/repository/commits/commit.rs`
195+
* `GET /projects/:project/repository/commits/:sha/refs` `projects/repository/commits/refs.rs`
195196
* `GET /projects/:project/repository/commits/:sha/comments` `projects/repository/commits/comments.rs`
196197
* `POST /projects/:project/repository/commits/:sha/comments` `projects/repository/commits/comment.rs`
197198
* `GET /projects/:project/repository/commits/:sha/merge_requests` `projects/repository/commits/merge_requests.rs`
@@ -449,7 +450,6 @@ instead of having to search the page for missing endpoints.
449450
* `POST /projects/:project/repository/commits/:sha/cherry_pick` https://gitlab.kitware.com/help/api/commits.md#cherry-pick-a-commit
450451
* `GET /projects/:project/repository/commits/:sha/diff` https://gitlab.kitware.com/help/api/commits.md#get-the-diff-of-a-commit
451452
* `GET /projects/:project/repository/commits/:sha/discussions` https://gitlab.kitware.com/help/api/commits.md#get-the-discussions-of-a-commit
452-
* `GET /projects/:project/repository/commits/:sha/refs` https://gitlab.kitware.com/help/api/commits.md#get-references-a-commit-is-pushed-to
453453
* `POST /projects/:project/repository/commits/:sha/revert` https://gitlab.kitware.com/help/api/commits.md#revert-a-commit
454454
* `GET /projects/:project/repository/compare` https://gitlab.kitware.com/help/api/repositories.md#compare-branches-tags-or-commits
455455
* `GET /projects/:project/repository/contributors` https://gitlab.kitware.com/help/api/repositories.md#contributors

src/api/projects/repository/commits.rs

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod commits;
1515
mod create;
1616
mod create_status;
1717
mod merge_requests;
18+
mod refs;
1819
mod signature;
1920
mod statuses;
2021

@@ -49,6 +50,11 @@ pub use self::create_status::CreateCommitStatus;
4950
pub use self::create_status::CreateCommitStatusBuilder;
5051
pub use self::create_status::CreateCommitStatusBuilderError;
5152

53+
pub use self::refs::CommitReferences;
54+
pub use self::refs::CommitReferencesBuilder;
55+
pub use self::refs::CommitReferencesBuilderError;
56+
pub use self::refs::CommitRefsType;
57+
5258
pub use self::statuses::CommitStatuses;
5359
pub use self::statuses::CommitStatusesBuilder;
5460
pub use self::statuses::CommitStatusesBuilderError;
+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
7+
use derive_builder::Builder;
8+
9+
use crate::api::common::NameOrId;
10+
use crate::api::{endpoint_prelude::*, ParamValue};
11+
12+
/// Commit reference types
13+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14+
#[non_exhaustive]
15+
pub enum CommitRefsType {
16+
/// Scope branch
17+
Branch,
18+
/// Scope tag
19+
Tag,
20+
/// Scope all
21+
All,
22+
}
23+
24+
impl CommitRefsType {
25+
fn as_str(self) -> &'static str {
26+
match self {
27+
CommitRefsType::Branch => "branch",
28+
CommitRefsType::Tag => "tag",
29+
CommitRefsType::All => "all",
30+
}
31+
}
32+
}
33+
34+
impl ParamValue<'static> for CommitRefsType {
35+
fn as_value(&self) -> Cow<'static, str> {
36+
self.as_str().into()
37+
}
38+
}
39+
40+
/// Post a comment on a specific commit in a project.
41+
#[derive(Debug, Builder, Clone)]
42+
#[builder(setter(strip_option))]
43+
pub struct CommitReferences<'a> {
44+
/// The project to get a commit from.
45+
#[builder(setter(into))]
46+
project: NameOrId<'a>,
47+
/// The commit sha
48+
#[builder(setter(into))]
49+
sha: Cow<'a, str>,
50+
/// The ref types
51+
#[builder(default)]
52+
type_: Option<CommitRefsType>,
53+
}
54+
55+
impl<'a> CommitReferences<'a> {
56+
/// Create a builder for the endpoint.
57+
pub fn builder() -> CommitReferencesBuilder<'a> {
58+
CommitReferencesBuilder::default()
59+
}
60+
}
61+
62+
impl<'a> Endpoint for CommitReferences<'a> {
63+
fn method(&self) -> Method {
64+
Method::GET
65+
}
66+
67+
fn endpoint(&self) -> Cow<'static, str> {
68+
format!(
69+
"projects/{}/repository/commits/{}/refs",
70+
self.project, self.sha,
71+
)
72+
.into()
73+
}
74+
75+
fn parameters(&self) -> QueryParams {
76+
let mut params = QueryParams::default();
77+
78+
params.push_opt("type", self.type_);
79+
80+
params
81+
}
82+
}
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use http::Method;
87+
88+
use crate::api::projects::repository::commits::refs::{
89+
CommitReferences, CommitReferencesBuilderError,
90+
};
91+
use crate::api::{self, Query};
92+
use crate::test::client::{ExpectedUrl, SingleTestClient};
93+
94+
use super::CommitRefsType;
95+
96+
#[test]
97+
fn commit_refs_type_as_str() {
98+
let items = &[
99+
(CommitRefsType::Branch, "branch"),
100+
(CommitRefsType::Tag, "tag"),
101+
(CommitRefsType::All, "all"),
102+
];
103+
104+
for (i, s) in items {
105+
assert_eq!(i.as_str(), *s);
106+
}
107+
}
108+
109+
#[test]
110+
fn project_is_necessary() {
111+
let err = CommitReferences::builder()
112+
.sha("0000000000000000000000000000000000000000")
113+
.build()
114+
.unwrap_err();
115+
crate::test::assert_missing_field!(err, CommitReferencesBuilderError, "project");
116+
}
117+
118+
#[test]
119+
fn sha_is_necessary() {
120+
let err = CommitReferences::builder().project(1).build().unwrap_err();
121+
crate::test::assert_missing_field!(err, CommitReferencesBuilderError, "sha");
122+
}
123+
124+
#[test]
125+
fn project_and_sha_are_sufficient() {
126+
CommitReferences::builder()
127+
.project(1)
128+
.sha("0000000000000000000000000000000000000000")
129+
.build()
130+
.unwrap();
131+
}
132+
133+
#[test]
134+
fn endpoint() {
135+
let endpoint = ExpectedUrl::builder()
136+
.method(Method::GET)
137+
.endpoint("projects/simple%2Fproject/repository/commits/0000000000000000000000000000000000000000/refs")
138+
.build()
139+
.unwrap();
140+
let client = SingleTestClient::new_raw(endpoint, "");
141+
142+
let endpoint = CommitReferences::builder()
143+
.project("simple/project")
144+
.sha("0000000000000000000000000000000000000000")
145+
.build()
146+
.unwrap();
147+
api::ignore(endpoint).query(&client).unwrap();
148+
}
149+
150+
#[test]
151+
fn endpoint_type() {
152+
let endpoint = ExpectedUrl::builder()
153+
.method(Method::GET)
154+
.endpoint("projects/simple%2Fproject/repository/commits/0000000000000000000000000000000000000000/refs")
155+
.add_query_params(&[("type", "all")])
156+
.build()
157+
.unwrap();
158+
let client = SingleTestClient::new_raw(endpoint, "");
159+
160+
let endpoint = CommitReferences::builder()
161+
.project("simple/project")
162+
.sha("0000000000000000000000000000000000000000")
163+
.type_(CommitRefsType::All)
164+
.build()
165+
.unwrap();
166+
api::ignore(endpoint).query(&client).unwrap();
167+
}
168+
}

0 commit comments

Comments
 (0)