Skip to main content

gitfox_client/
rules.rs

1//! Protection rules and gitspaces.
2//!
3//! Verified against the GitFox API v1.3.0 OpenAPI document:
4//!
5//! | Operation | Endpoint |
6//! |---|---|
7//! | list rules       | `GET /api/v1/repos/{repo_ref}/rules` |
8//! | view rule        | `GET /api/v1/repos/{repo_ref}/rules/{rule_identifier}` |
9//! | list gitspaces   | `GET /api/v1/gitspaces` |
10//! | view gitspace    | `GET /api/v1/gitspaces/{gitspace_identifier}` |
11//! | create gitspace  | `POST /api/v1/gitspaces` |
12//! | delete gitspace  | `DELETE /api/v1/gitspaces/{gitspace_identifier}` |
13//! | start / stop     | `POST …/gitspaces/{gitspace_identifier}/action` |
14//! | gitspace logs    | `GET …/gitspaces/{gitspace_identifier}/logs/stream` (SSE) |
15//!
16//! Gitspaces are an instance feature that can be switched off; an instance
17//! with them disabled answers the gitspace endpoints with a bare 500, so
18//! callers should check [`crate::SystemConfig::gitspace_enabled`] first.
19
20use std::time::Duration;
21
22use serde_json::json;
23
24use crate::client::{GitFoxClient, Method, Query};
25use crate::error::Result;
26use crate::models::{CreateGitspace, Gitspace, LogLine, RepoRef, Rule};
27use crate::pipeline::urlencode;
28
29pub struct RulesApi<'a> {
30    client: &'a GitFoxClient,
31}
32
33impl<'a> RulesApi<'a> {
34    pub(crate) fn new(client: &'a GitFoxClient) -> Self {
35        Self { client }
36    }
37
38    /// `GET /api/v1/repos/{repo_ref}/rules`
39    pub async fn list(&self, repo: &RepoRef, page: u32, limit: u32) -> Result<Vec<Rule>> {
40        let mut q = Query::new();
41        q.push("page", page).push("limit", limit);
42        self.client
43            .get_json(&q.apply(&format!("/api/v1/repos/{}/rules", repo.encoded())))
44            .await
45    }
46
47    /// `GET /api/v1/repos/{repo_ref}/rules/{rule_identifier}`
48    pub async fn get(&self, repo: &RepoRef, identifier: &str) -> Result<Rule> {
49        self.client
50            .get_json(&format!(
51                "/api/v1/repos/{}/rules/{}",
52                repo.encoded(),
53                urlencode(identifier)
54            ))
55            .await
56    }
57}
58
59pub struct GitspacesApi<'a> {
60    client: &'a GitFoxClient,
61}
62
63impl<'a> GitspacesApi<'a> {
64    pub(crate) fn new(client: &'a GitFoxClient) -> Self {
65        Self { client }
66    }
67
68    fn path(identifier: &str) -> String {
69        format!("/api/v1/gitspaces/{}", urlencode(identifier))
70    }
71
72    /// `GET /api/v1/gitspaces`
73    pub async fn list(&self, page: u32, limit: u32) -> Result<Vec<Gitspace>> {
74        let mut q = Query::new();
75        q.push("sort", "updated")
76            .push("order", "desc")
77            .push("page", page)
78            .push("limit", limit);
79        self.client.get_json(&q.apply("/api/v1/gitspaces")).await
80    }
81
82    /// `GET /api/v1/gitspaces/{gitspace_identifier}`
83    pub async fn get(&self, identifier: &str) -> Result<Gitspace> {
84        self.client.get_json(&Self::path(identifier)).await
85    }
86
87    /// `POST /api/v1/gitspaces`
88    pub async fn create(&self, input: &CreateGitspace) -> Result<Gitspace> {
89        let body = serde_json::to_value(input).map_err(|e| crate::Error::Decode(e.to_string()))?;
90        self.client
91            .request(Method::POST, "/api/v1/gitspaces", Some(&body), &[])
92            .await?
93            .deserialize()
94    }
95
96    /// `DELETE /api/v1/gitspaces/{gitspace_identifier}`
97    pub async fn delete(&self, identifier: &str) -> Result<()> {
98        self.client
99            .request(Method::DELETE, &Self::path(identifier), None, &[])
100            .await
101            .map(|_| ())
102    }
103
104    /// `POST …/action` with `start` or `stop`.
105    pub async fn action(&self, identifier: &str, action: &str) -> Result<Gitspace> {
106        self.client
107            .request(
108                Method::POST,
109                &format!("{}/action", Self::path(identifier)),
110                Some(&json!({ "action": action })),
111                &[],
112            )
113            .await?
114            .deserialize()
115    }
116
117    /// `GET …/logs/stream`, collected until the stream goes quiet for `idle`.
118    pub async fn logs(&self, identifier: &str, idle: Duration) -> Result<Vec<LogLine>> {
119        self.client
120            .sse_lines(&format!("{}/logs/stream", Self::path(identifier)), idle)
121            .await
122    }
123}