1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::{
  shared_state::AstroRunSharedState, Action, Actions, ExecutionContext, ExecutionContextBuilder,
  GithubAuthorization, JobId, Plugin, PluginManager, Result, Runner,
};
use std::sync::Arc;

#[derive(Clone)]
pub struct AstroRun {
  runner: Arc<Box<dyn Runner>>,
  github_auth: Option<GithubAuthorization>,
  pub(crate) shared_state: AstroRunSharedState,
}

impl AstroRun {
  pub fn builder() -> AstroRunBuilder {
    AstroRunBuilder::new()
  }

  pub fn cancel(&self, job_id: &JobId) -> Result<()> {
    self.shared_state.cancel(job_id)
  }

  pub fn register_plugin<P: Plugin + 'static>(&self, plugin: P) -> &Self {
    self.shared_state.register_plugin(plugin);

    self
  }

  pub fn unregister_plugin(&self, plugin_name: &'static str) -> &Self {
    self.shared_state.unregister_plugin(plugin_name);

    self
  }

  pub fn register_action<T>(&self, name: impl Into<String>, action: T) -> &Self
  where
    T: crate::actions::Action + 'static,
  {
    self.shared_state.register_action(name, action);

    self
  }

  pub fn unregister_action(&self, name: &str) -> &Self {
    self.shared_state.unregister_action(name);

    self
  }

  pub fn plugins(&self) -> PluginManager {
    self.shared_state.plugins()
  }

  pub fn actions(&self) -> Actions {
    self.shared_state.actions()
  }

  pub fn execution_context(&self) -> ExecutionContextBuilder {
    let shared_state = self.shared_state.clone();
    let mut builder = ExecutionContext::builder()
      .runner(self.runner.clone())
      .shared_state(shared_state);

    if let Some(github_auth) = &self.github_auth {
      builder = builder.github_auth(github_auth.clone());
    }

    builder
  }
}

pub struct AstroRunBuilder {
  runner: Option<Box<dyn Runner>>,
  shared_state: AstroRunSharedState,
  github_auth: Option<GithubAuthorization>,
}

impl AstroRunBuilder {
  pub fn new() -> Self {
    AstroRunBuilder {
      runner: None,
      github_auth: None,
      shared_state: AstroRunSharedState::new(),
    }
  }

  pub fn runner<T>(mut self, runner: T) -> Self
  where
    T: Runner + 'static,
  {
    self.runner = Some(Box::new(runner));
    self
  }

  pub fn plugin<P: Plugin + 'static>(self, plugin: P) -> Self {
    self.shared_state.register_plugin(plugin);
    self
  }

  pub fn action(self, name: impl Into<String>, action: impl Action + 'static) -> Self {
    self.shared_state.register_action(name, action);
    self
  }

  pub fn github_personal_token(mut self, token: impl Into<String>) -> Self {
    self.github_auth = Some(GithubAuthorization::PersonalAccessToken(token.into()));
    self
  }

  pub fn github_app(mut self, app_id: u64, private_key: impl Into<String>) -> Self {
    self.github_auth = Some(GithubAuthorization::GithubApp {
      app_id,
      private_key: private_key.into(),
    });

    self
  }

  pub fn build(self) -> AstroRun {
    let runner = self.runner.unwrap();

    AstroRun {
      runner: Arc::new(runner),
      shared_state: self.shared_state,
      github_auth: self.github_auth,
    }
  }
}