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
129
// Copyright 2016 Kitware, Inc.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate git_workarea;
use self::git_workarea::CommitId;

use super::commit::Commit;
use super::context::CheckGitContext;
use super::error::Result;

#[derive(Debug, Default)]
/// The results of a check.
pub struct CheckResult {
    warnings: Vec<String>,
    alerts: Vec<String>,
    errors: Vec<String>,

    allow: bool,
    pass: bool,
}

impl CheckResult {
    /// Create a new results structure.
    pub fn new() -> Self {
        CheckResult {
            warnings: vec![],
            alerts: vec![],
            errors: vec![],

            allow: false,
            pass: true,
        }
    }

    /// Adds a warning message to the results.
    pub fn add_warning<S: ToString>(&mut self, warning: S) -> &mut Self {
        self.warnings.push(warning.to_string());

        self
    }

    /// Adds an alert to the results.
    ///
    /// These messages should be brought to the attention of those maintaining the deployment of
    /// the checks.
    pub fn add_alert<S: ToString>(&mut self, alert: S, should_block: bool) -> &mut Self {
        self.alerts.push(alert.to_string());
        self.pass = self.pass && !should_block;

        self
    }

    /// Adds a error message to the results.
    ///
    /// Also marks the checks as having failed.
    pub fn add_error<S: ToString>(&mut self, error: S) -> &mut Self {
        self.errors.push(error.to_string());
        self.pass = false;

        self
    }

    /// Allows the checks to pass no matter what.
    pub fn whitelist(&mut self) -> &mut Self {
        self.allow = true;

        self
    }

    /// The warnings from the checks.
    pub fn warnings(&self) -> &Vec<String> {
        &self.warnings
    }

    /// The alerts from the checks.
    pub fn alerts(&self) -> &Vec<String> {
        &self.alerts
    }

    /// The errors from the checks.
    pub fn errors(&self) -> &Vec<String> {
        &self.errors
    }

    /// Whether the checks will allow the commit no matter what.
    pub fn allowed(&self) -> bool {
        self.allow
    }

    /// Whether the checks passed or failed.
    pub fn pass(&self) -> bool {
        self.pass
    }

    /// Combine two results together.
    pub fn combine(self, other: Self) -> Self {
        CheckResult {
            warnings: self.warnings.into_iter().chain(other.warnings.into_iter()).collect(),
            alerts: self.alerts.into_iter().chain(other.alerts.into_iter()).collect(),
            errors: self.errors.into_iter().chain(other.errors.into_iter()).collect(),

            allow: self.allow || other.allow,
            pass: self.pass && other.pass,
        }
    }
}

/// Interface for checks which run for each commit.
pub trait Check: Sync {
    /// The name of the check.
    fn name(&self) -> &str;

    /// Run the check.
    fn check(&self, ctx: &CheckGitContext, commit: &Commit) -> Result<CheckResult>;
}

/// Interface for checks which runs once for the entire branch.
pub trait BranchCheck: Sync {
    /// The name of the check.
    fn name(&self) -> &str;

    /// Run the check.
    fn check(&self, ctx: &CheckGitContext, commit: &CommitId) -> Result<CheckResult>;
}