summaryrefslogtreecommitdiff
path: root/ci-lib-native/src/dbctx_ext.rs
blob: 44436fc81e133f635b31d5b2f89340e38c7e8576 (plain)
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
use crate::io::ArtifactDescriptor;
use crate::notifier::{RemoteNotifier, NotifierConfig};
use tokio::fs::{File, OpenOptions};

use ci_lib_core::dbctx::DbCtx;

pub fn notifiers_by_repo(ctx: &DbCtx, repo_id: u64) -> Result<Vec<RemoteNotifier>, String> {
    let remotes = ctx.remotes_by_repo(repo_id)?;

    let mut notifiers: Vec<RemoteNotifier> = Vec::new();

    for remote in remotes.into_iter() {
        match remote.remote_api.as_str() {
            "github" => {
                let mut notifier_path = ctx.config_path.clone();
                notifier_path.push(&remote.notifier_config_path);

                let notifier = RemoteNotifier {
                    remote_path: remote.remote_path,
                    notifier: NotifierConfig::github_from_file(&notifier_path)
                        .expect("can load notifier config")
                };
                notifiers.push(notifier);
            },
            "email" => {
                let mut notifier_path = ctx.config_path.clone();
                notifier_path.push(&remote.notifier_config_path);

                let notifier = RemoteNotifier {
                    remote_path: remote.remote_path,
                    notifier: NotifierConfig::email_from_file(&notifier_path)
                        .expect("can load notifier config")
                };
                notifiers.push(notifier);
            }
            other => {
                eprintln!("unknown remote api kind: {:?}, remote is {:?}", other, &remote)
            }
        }
    }

    Ok(notifiers)
}

pub async fn reserve_artifact(ctx: &DbCtx, run_id: u64, name: &str, desc: &str) -> Result<ArtifactDescriptor, String> {
    let artifact_id = {
        let created_time = ci_lib_core::now_ms();
        let conn = ctx.conn.lock().unwrap();
        conn
            .execute(
                "insert into artifacts (run_id, name, desc, created_time) values (?1, ?2, ?3, ?4)",
                (run_id, name, desc, created_time)
            )
            .map_err(|e| {
                format!("{:?}", e)
            })?;

        conn.last_insert_rowid() as u64
    };

    ArtifactDescriptor::new(run_id, artifact_id).await
}