Skip to main content

ephemeris_forge_github/api/http/
variables.rs

1use ephemeris_forge::{RepoRef, encode};
2use reqwest::Method;
3
4use super::environments::path as environment_path;
5use crate::api::HttpApi;
6use crate::api::error::ForgeError;
7use crate::api::model::{Variable, VariablePage};
8use crate::api::operations::VariablesApi;
9use crate::api::request::{VariableBody, VariableScope};
10
11/// GitHub serves at most 30 variables per page.
12const VARIABLES_PAGE_SIZE: usize = 30;
13
14impl VariablesApi for HttpApi {
15    async fn create_variable(
16        &self,
17        repository: &RepoRef,
18        scope: &VariableScope,
19        variable: &VariableBody,
20    ) -> Result<(), ForgeError> {
21        self.empty(
22            Method::POST,
23            &route(repository, scope),
24            Some(encode(variable)),
25        )
26        .await
27    }
28
29    async fn delete_variable(
30        &self,
31        repository: &RepoRef,
32        scope: &VariableScope,
33        name: &str,
34    ) -> Result<(), ForgeError> {
35        self.empty(
36            Method::DELETE,
37            &format!("{}/{name}", route(repository, scope)),
38            None,
39        )
40        .await
41    }
42
43    async fn update_variable(
44        &self,
45        repository: &RepoRef,
46        scope: &VariableScope,
47        variable: &VariableBody,
48    ) -> Result<(), ForgeError> {
49        self.empty(
50            Method::PATCH,
51            &format!("{}/{}", route(repository, scope), variable.name),
52            Some(encode(variable)),
53        )
54        .await
55    }
56
57    async fn variables(
58        &self,
59        repository: &RepoRef,
60        scope: &VariableScope,
61    ) -> Result<Vec<Variable>, ForgeError> {
62        self.paged_in(
63            &route(repository, scope),
64            VARIABLES_PAGE_SIZE,
65            |page: VariablePage| page.variables,
66        )
67        .await
68    }
69}
70
71fn route(repository: &RepoRef, scope: &VariableScope) -> String {
72    match scope {
73        VariableScope::Repository => repository.path("actions/variables"),
74        VariableScope::Environment(environment) => {
75            format!("{}/variables", environment_path(repository, environment))
76        }
77    }
78}