Skip to main content

ephemeris_forge_github/reconcile/
variables.rs

1use ephemeris_forge::{Mode, RepoRef};
2
3use super::Client;
4use super::environments::reads_environments;
5use crate::api::request::{VariableBody, VariableScope};
6use crate::api::{Api, ForgeError};
7use crate::operation::variables::Operation;
8use crate::{EnvironmentName, Environments, Live, Variables};
9
10impl<A: Api> Client<A> {
11    pub(super) async fn read_variables(
12        &self,
13        repository: &RepoRef,
14        config: Option<&Variables>,
15        environments: Option<&Environments>,
16        live: &mut Live,
17    ) -> Result<(), ForgeError> {
18        let Some(config) = config else {
19            return Ok(());
20        };
21        let owned = config.mode == Mode::Owned;
22        if owned || !config.repository.is_empty() {
23            live.variables = self
24                .api
25                .variables(repository, &VariableScope::Repository)
26                .await?;
27        }
28        let wanted: Vec<&EnvironmentName> = config
29            .environments
30            .iter()
31            .filter(|(_, variables)| owned || !variables.is_empty())
32            .map(|(environment, _)| environment)
33            .collect();
34        if wanted.is_empty() {
35            return Ok(());
36        }
37        let existing: Vec<String> = if environments.is_some_and(reads_environments) {
38            live.environments
39                .iter()
40                .map(|found| found.name.clone())
41                .collect()
42        } else {
43            self.api
44                .environments(repository)
45                .await?
46                .into_iter()
47                .map(|found| found.name)
48                .collect()
49        };
50        for environment in wanted {
51            if existing.iter().any(|name| name == environment.as_str()) {
52                let variables = self
53                    .api
54                    .variables(
55                        repository,
56                        &VariableScope::Environment(environment.to_string()),
57                    )
58                    .await?;
59                live.environment_variables
60                    .insert(environment.to_string(), variables);
61            }
62        }
63        Ok(())
64    }
65
66    pub(super) async fn apply_variables(
67        &self,
68        repository: &RepoRef,
69        operation: &Operation,
70    ) -> Result<(), ForgeError> {
71        match operation {
72            Operation::Create { scope, name, value } => {
73                let body = VariableBody {
74                    name: name.clone(),
75                    value: value.clone(),
76                };
77                self.api.create_variable(repository, scope, &body).await
78            }
79            Operation::Update { scope, name, value } => {
80                let body = VariableBody {
81                    name: name.clone(),
82                    value: value.clone(),
83                };
84                self.api.update_variable(repository, scope, &body).await
85            }
86            Operation::Delete { scope, name } => {
87                self.api.delete_variable(repository, scope, name).await
88            }
89        }
90    }
91}