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
use crate::CodeBuildResult;
use rusoto_codebuild::{
    BatchGetBuildsInput, BatchGetProjectsInput, Build, CodeBuild, CodeBuildClient, ListBuildsInput,
    ListProjectsInput, Project,
};
use rusoto_core::Region;
use std::collections::HashMap;

pub struct Aws {
    codebuild_client: CodeBuildClient,
}

impl Aws {
    pub fn fetch_builds(
        &self,
        next_token: Option<String>,
    ) -> (Option<String>, Vec<CodeBuildResult>) {
        let projects = self.get_projects(None);
        let fetch_result = self.get_builds(next_token);
        let mut result = Vec::new();

        for build in fetch_result.1 {
            let project_name = build.project_name.clone().unwrap();

            let project_details = projects
                .iter()
                .find(|x| x.name.clone().unwrap() == project_name);

            if project_details.is_none() {
                continue;
            }

            let mut tags = HashMap::new();
            for tag in project_details.unwrap().tags.clone().unwrap() {
                tags.insert(tag.key.unwrap(), tag.value.unwrap());
            }

            let mut codebuild_result = CodeBuildResult::from(build);
            codebuild_result.tags = tags;

            result.push(codebuild_result);
        }

        (fetch_result.0, result)
    }

    pub fn fetch_all_builds(&self) -> Vec<CodeBuildResult> {
        let mut result = self.fetch_builds(None);
        let mut builds = result.1;

        while result.0.is_some() {
            result = self.fetch_builds(result.0);
            builds.append(&mut result.1);
        }

        builds
    }

    fn get_projects(&self, next_token: Option<String>) -> Vec<Project> {
        let mut projects = Vec::new();

        let result = self
            .codebuild_client
            .list_projects(ListProjectsInput {
                next_token,
                ..Default::default()
            })
            .sync()
            .unwrap();

        projects.append(
            &mut self
                .codebuild_client
                .batch_get_projects(BatchGetProjectsInput {
                    names: result.projects.unwrap(),
                })
                .sync()
                .unwrap()
                .projects
                .unwrap(),
        );

        if result.next_token.is_some() {
            projects.append(&mut self.get_projects(result.next_token));
        }

        projects
    }

    fn get_builds(&self, next_token: Option<String>) -> (Option<String>, Vec<Build>) {
        let result = self
            .codebuild_client
            .list_builds(ListBuildsInput {
                next_token,
                ..Default::default()
            })
            .sync()
            .unwrap();

        let builds = self
            .codebuild_client
            .batch_get_builds(BatchGetBuildsInput {
                ids: result.ids.unwrap(),
            })
            .sync()
            .unwrap()
            .builds
            .unwrap();

        (result.next_token, builds)
    }
}

impl Default for Aws {
    fn default() -> Self {
        Self {
            codebuild_client: CodeBuildClient::new(Region::default()),
        }
    }
}