Skip to main content

interprex_test/
state.rs

1use std::{
2    collections::{BTreeMap, BTreeSet},
3    sync::Arc,
4};
5
6use bytes::Bytes;
7use interprex::{
8    AssetId, ChangeRequest, ChangeRequestNumber, CheckOutcome, CheckRun, DispatchInputs, Issue,
9    IssueNumber, Label, ProviderError, Release, Repository, RepositoryFacts, RepositorySettings,
10    Ruleset, RunId, WorkflowRun,
11};
12use tokio::sync::RwLock;
13
14#[derive(Clone, Debug, Default)]
15pub struct FakeProvider {
16    pub(crate) state: Arc<RwLock<State>>,
17}
18
19#[derive(Debug, Default)]
20pub(crate) struct State {
21    pub(crate) repositories: BTreeMap<Repository, (RepositoryFacts, RepositorySettings)>,
22    pub(crate) rulesets: BTreeMap<Repository, Vec<Ruleset>>,
23    pub(crate) secret_names: BTreeMap<Repository, Vec<String>>,
24    pub(crate) issues: BTreeMap<(Repository, IssueNumber), Issue>,
25    pub(crate) labels: BTreeMap<Repository, Vec<Label>>,
26    pub(crate) change_requests: BTreeMap<(Repository, ChangeRequestNumber), ChangeRequest>,
27    pub(crate) check_runs: BTreeMap<(Repository, String), Vec<CheckRun>>,
28    pub(crate) published_checks: Vec<(Repository, String, CheckOutcome)>,
29    pub(crate) dispatches: Vec<(Repository, String, String, DispatchInputs)>,
30    pub(crate) runs: BTreeMap<(Repository, RunId), WorkflowRun>,
31    pub(crate) cancelled_runs: Vec<(Repository, RunId)>,
32    pub(crate) releases: BTreeMap<(Repository, String), Release>,
33    pub(crate) assets: BTreeMap<(Repository, AssetId), Vec<Bytes>>,
34    pub(crate) next_release_id: u64,
35    pub(crate) next_asset_id: u64,
36}
37
38impl FakeProvider {
39    #[must_use]
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    pub async fn seed_repository(&self, facts: RepositoryFacts, settings: RepositorySettings) {
45        self.state
46            .write()
47            .await
48            .repositories
49            .insert(facts.repository.clone(), (facts, settings));
50    }
51
52    pub async fn seed_issue(&self, repository: Repository, issue: Issue) {
53        self.state
54            .write()
55            .await
56            .issues
57            .insert((repository, issue.number), issue);
58    }
59
60    /// Seeds one change request into the repository it targets.
61    ///
62    /// The head it proposes is `change_request.head`, which names the
63    /// repository holding that branch and so can be a fork of `repository`.
64    pub async fn seed_change_request(&self, repository: Repository, change_request: ChangeRequest) {
65        self.state
66            .write()
67            .await
68            .change_requests
69            .insert((repository, change_request.number), change_request);
70    }
71
72    /// Seeds observed checks, each on the commit it names, replacing whatever
73    /// was already seeded on the commits this call names.
74    ///
75    /// The commit comes from every run's own `head_sha`, so no seeded
76    /// observation can place a run on a commit it does not name.
77    pub async fn seed_check_runs(&self, repository: Repository, runs: Vec<CheckRun>) {
78        let mut state = self.state.write().await;
79        let mut replaced = BTreeSet::new();
80        for run in runs {
81            let key = (repository.clone(), run.head_sha.clone());
82            if replaced.insert(key.clone()) {
83                state.check_runs.insert(key.clone(), Vec::new());
84            }
85            state.check_runs.entry(key).or_default().push(run);
86        }
87    }
88
89    pub async fn seed_run(&self, repository: Repository, run: WorkflowRun) {
90        self.state
91            .write()
92            .await
93            .runs
94            .insert((repository, run.id), run);
95    }
96
97    pub async fn seed_release(&self, repository: Repository, release: Release) {
98        self.state
99            .write()
100            .await
101            .releases
102            .insert((repository, release.tag.clone()), release);
103    }
104
105    pub async fn published_checks(&self) -> Vec<(Repository, String, CheckOutcome)> {
106        self.state.read().await.published_checks.clone()
107    }
108
109    pub async fn dispatches(&self) -> Vec<(Repository, String, String, DispatchInputs)> {
110        self.state.read().await.dispatches.clone()
111    }
112}
113
114pub(crate) fn missing(entity: impl Into<String>) -> ProviderError {
115    ProviderError::NotFound {
116        entity: entity.into(),
117    }
118}