Commit 9e698263 authored by Ben Boeckel's avatar Ben Boeckel Committed by Kitware Robot
Browse files

Merge topic 'add-commits-listing-api'

099b0773

 api/repository/commits: add commit listing API
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: William Dillon's avatarWilliam Dillon <william@housedillon.com>
Merge-request: !287
parents 4848f053 099b0773
......@@ -4,6 +4,7 @@
* Added the `api::projects::releases::ProjectReleases` endpoint to list all
releases for a project.
* Listing commits in a repository can now be done via `Commits`
# v0.1310.0
......
......@@ -104,6 +104,7 @@ These API endpoints have been implemented.
* `GET /projects/:project/repository/branches` `projects/repository/branches/branches.rs`
* `POST /projects/:project/repository/branches` `projects/repository/branches/create.rs`
* `GET /projects/:project/repository/branches/:branch` `projects/repository/branches/branch.rs`
* `GET /projects/:project/repository/commits` `projects/repository/commits/commits.rs`
* `GET /projects/:project/repository/commits/:sha` `projects/repository/commits/commit.rs`
* `GET /projects/:project/repository/commits/:sha/comments` `projects/repository/commits/comments.rs`
* `POST /projects/:project/repository/commits/:sha/comments` `projects/repository/commits/comment.rs`
......@@ -311,7 +312,6 @@ instead of having to search the page for missing endpoints.
* `DELETE /projects/:project/releases/:tag_name` https://gitlab.kitware.com/help/api/releases/index.md#delete-a-release
* `GET /projects/:project/releases/:tag_name/evidence` https://gitlab.kitware.com/help/api/releases/index.md#collect-release-evidence
* `DELETE /projects/:project/repository/branches/:branch` https://gitlab.kitware.com/help/api/branches.md#delete-repository-branch
* `GET /projects/:project/repository/commits` https://gitlab.kitware.com/help/api/commits.md#list-repository-commits
* `POST /projects/:project/repository/commits` https://gitlab.kitware.com/help/api/commits.md#create-a-commit-with-multiple-files-and-actions
* `POST /projects/:project/repository/commits/:sha/cherry_pick` https://gitlab.kitware.com/help/api/commits.md#cherry-pick-a-commit
* `GET /projects/:project/repository/commits/:sha/diffs` https://gitlab.kitware.com/help/api/commits.md#get-the-diff-of-a-commit
......
......@@ -11,6 +11,7 @@
mod comment;
mod comments;
mod commit;
mod commits;
mod create_status;
mod statuses;
......@@ -24,6 +25,10 @@ pub use self::comments::CommitCommentsBuilder;
pub use self::commit::Commit;
pub use self::commit::CommitBuilder;
pub use self::commits::Commits;
pub use self::commits::CommitsBuilder;
pub use self::commits::CommitsOrder;
pub use self::create_status::CommitStatusState;
pub use self::create_status::CreateCommitStatus;
pub use self::create_status::CreateCommitStatusBuilder;
......
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use chrono::{DateTime, Utc};
use derive_builder::Builder;
use crate::api::common::NameOrId;
use crate::api::endpoint_prelude::*;
use crate::api::ParamValue;
/// Orders commits may be ordered by.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommitsOrder {
/// Commits are returned in reverse chronological order.
Default,
/// Commits are returned in topological order.
Topo,
}
impl Default for CommitsOrder {
fn default() -> Self {
CommitsOrder::Default
}
}
impl CommitsOrder {
fn as_str(self) -> &'static str {
match self {
CommitsOrder::Default => "default",
CommitsOrder::Topo => "topo",
}
}
}
impl ParamValue<'static> for CommitsOrder {
fn as_value(&self) -> Cow<'static, str> {
self.as_str().into()
}
}
/// Query for commits in a project.
#[derive(Debug, Builder)]
#[builder(setter(strip_option))]
pub struct Commits<'a> {
/// The project to get commits from.
#[builder(setter(into))]
project: NameOrId<'a>,
/// The ref to get commits from.
///
/// If not given, the default branch will be used.
#[builder(default, setter(into))]
ref_name: Option<Cow<'a, str>>,
/// Only return commits after a given date.
#[builder(default)]
since: Option<DateTime<Utc>>,
/// Only return commits before a given date.
#[builder(default)]
until: Option<DateTime<Utc>>,
/// Only return commits which affect a given path.
#[builder(default, setter(into))]
path: Option<Cow<'a, str>>,
/// If true, return every commit from the repository.
#[builder(default)]
all: Option<bool>,
/// Include commit stats in each commit object.
#[builder(default)]
with_stats: Option<bool>,
/// If true, only consider commits in the first parent history.
#[builder(default)]
first_parent: Option<bool>,
/// If true, only consider commits in the first parent history.
#[builder(default)]
order: Option<CommitsOrder>,
}
impl<'a> Commits<'a> {
/// Create a builder for the endpoint.
pub fn builder() -> CommitsBuilder<'a> {
CommitsBuilder::default()
}
}
impl<'a> Endpoint for Commits<'a> {
fn method(&self) -> Method {
Method::GET
}
fn endpoint(&self) -> Cow<'static, str> {
format!("projects/{}/repository/commits", self.project).into()
}
fn parameters(&self) -> QueryParams {
let mut params = QueryParams::default();
params
.push_opt("ref_name", self.ref_name.as_ref())
.push_opt("since", self.since)
.push_opt("until", self.until)
.push_opt("path", self.path.as_ref())
.push_opt("all", self.all)
.push_opt("with_stats", self.with_stats)
.push_opt("first_parent", self.first_parent)
.push_opt("order", self.order);
params
}
}
impl<'a> Pageable for Commits<'a> {}
#[cfg(test)]
mod tests {
use chrono::{TimeZone, Utc};
use crate::api::projects::repository::commits::{Commits, CommitsOrder};
use crate::api::{self, Query};
use crate::test::client::{ExpectedUrl, SingleTestClient};
#[test]
fn commits_order_default() {
assert_eq!(CommitsOrder::default(), CommitsOrder::Default);
}
#[test]
fn commits_order_as_str() {
let items = &[
(CommitsOrder::Default, "default"),
(CommitsOrder::Topo, "topo"),
];
for (i, s) in items {
assert_eq!(i.as_str(), *s);
}
}
#[test]
fn project_is_necessary() {
let err = Commits::builder().build().unwrap_err();
assert_eq!(err, "`project` must be initialized");
}
#[test]
fn project_is_sufficient() {
Commits::builder().project(1).build().unwrap();
}
#[test]
fn endpoint() {
let endpoint = ExpectedUrl::builder()
.endpoint("projects/simple%2Fproject/repository/commits")
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = Commits::builder()
.project("simple/project")
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_ref_name() {
let endpoint = ExpectedUrl::builder()
.endpoint("projects/simple%2Fproject/repository/commits")
.add_query_params(&[("ref_name", "refs/tags/v1.0.0")])
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = Commits::builder()
.project("simple/project")
.ref_name("refs/tags/v1.0.0")
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_since() {
let endpoint = ExpectedUrl::builder()
.endpoint("projects/simple%2Fproject/repository/commits")
.add_query_params(&[("since", "2021-01-01T00:00:00Z")])
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = Commits::builder()
.project("simple/project")
.since(Utc.ymd(2021, 1, 1).and_hms_milli(0, 0, 0, 0))
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_until() {
let endpoint = ExpectedUrl::builder()
.endpoint("projects/simple%2Fproject/repository/commits")
.add_query_params(&[("until", "2021-01-01T00:00:00Z")])
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = Commits::builder()
.project("simple/project")
.until(Utc.ymd(2021, 1, 1).and_hms_milli(0, 0, 0, 0))
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_path() {
let endpoint = ExpectedUrl::builder()
.endpoint("projects/simple%2Fproject/repository/commits")
.add_query_params(&[("path", "path/to/file")])
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = Commits::builder()
.project("simple/project")
.path("path/to/file")
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_all() {
let endpoint = ExpectedUrl::builder()
.endpoint("projects/simple%2Fproject/repository/commits")
.add_query_params(&[("all", "true")])
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = Commits::builder()
.project("simple/project")
.all(true)
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_with_stats() {
let endpoint = ExpectedUrl::builder()
.endpoint("projects/simple%2Fproject/repository/commits")
.add_query_params(&[("with_stats", "false")])
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = Commits::builder()
.project("simple/project")
.with_stats(false)
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_first_parent() {
let endpoint = ExpectedUrl::builder()
.endpoint("projects/simple%2Fproject/repository/commits")
.add_query_params(&[("first_parent", "true")])
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = Commits::builder()
.project("simple/project")
.first_parent(true)
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
#[test]
fn endpoint_order() {
let endpoint = ExpectedUrl::builder()
.endpoint("projects/simple%2Fproject/repository/commits")
.add_query_params(&[("order", "topo")])
.build()
.unwrap();
let client = SingleTestClient::new_raw(endpoint, "");
let endpoint = Commits::builder()
.project("simple/project")
.order(CommitsOrder::Topo)
.build()
.unwrap();
api::ignore(endpoint).query(&client).unwrap();
}
}
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