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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! Check that all jobs in a build matrix run and succeeded and launch a single task afterwards.
//!
//! Travis offers no way to launch a single task when all jobs in a build finish.
//! Relevant issue: <https://github.com/travis-ci/travis-ci/issues/929>
//!
//! Sometimes such a hook is necessary, for example to publish a new version of your project only once
//! and only if all jobs succeed.
//!
//! travis-after-all is a workaround for this and allows to wait for all jobs and then run a
//! command afterwards.
//! This is a port of the original Python script: <https://github.com/dmakhno/travis_after_all>
//!
//! ## CLI usage
//!
//! You need to add the following lines to your `.travis.yml`.
//! This installs the tool and executes as an `after_success` hook:
//! (It will only work for Rust projects as it depends on Cargo, the Rust package manager)
//!
//! ```yaml
//! before_script:
//!   - |
//!      export PATH=$HOME/.cargo/bin:$PATH:$PATH &&
//!      cargo install --force travis-after-all
//!
//! after_success:
//!   - travis-after-all && echo "All fine, let's publish"
//! ```
//!
//! ## Library usage
//!
//! You can use it as a library as well to build your own hooks:
//!
//! ```rust,no_run
//! use travis_after_all::Build;
//! let build_run = Build::from_env().unwrap();
//! if build_run.is_leader() {
//!     let _ = build_run.wait_for_others().unwrap();
//!     println!("Everything done. We can work here now.");
//! }
//! ```
#![deny(missing_docs)]

extern crate reqwest;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

use std::thread;
use std::time::Duration;
use std::env;
use std::str::FromStr;
use reqwest::{RedirectPolicy, StatusCode};
use reqwest::header::UserAgent;

mod error;
mod matrix;
pub use error::Error;
pub use matrix::{Matrix, MatrixElement};

const USER_AGENT:         &'static str = concat!("travis-after-all/", env!("CARGO_PKG_VERSION"));
const TRAVIS_JOB_NUMBER : &'static str = "TRAVIS_JOB_NUMBER";
const TRAVIS_BUILD_ID:    &'static str = "TRAVIS_BUILD_ID";
const TRAVIS_API_URL:     &'static str = "https://api.travis-ci.org";
const POLLING_INTERVAL:   &'static str = "LEADER_POLLING_INTERVAL";


fn env_var(varname: &str) -> Result<String, Error> {
    env::var(varname)
        .map_err(|_| Error::from_string(format!("Missing environment variable: {}", varname)))
}

fn is_leader(job: &str) -> bool {
    job.ends_with('1')
}

/// The information of a full build
pub struct Build {
    travis_api_url: String,
    build_id: String,
    job_number: String,
    polling_interval: u64,
}

impl Build {
    /// Fetch the relevant information from the environment
    ///
    /// This reads the environment variables `TRAVIS_BUILD_ID` and `TRAVIS_JOB_NUMBER`,
    /// which are automatically set by Travis.
    ///
    /// It also reads the variable `POLLING_INTERVAL` with a default of 5.
    /// Set it to a higher value for a longer timeout.
    pub fn from_env() -> Result<Build, Error> {
        let build_id = try!(env_var(TRAVIS_BUILD_ID));
        let job_number = try!(env_var(TRAVIS_JOB_NUMBER));

        let polling_interval = match env_var(POLLING_INTERVAL) {
            Err(_) => 5,
            Ok(val) => try!(FromStr::from_str(&val))
        };

        Ok(Build {
            travis_api_url: TRAVIS_API_URL.into(),
            build_id: build_id,
            job_number: job_number,
            polling_interval: polling_interval,
        })

    }

    /// Whether or not the current environment is the build leader
    pub fn is_leader(&self) -> bool {
        is_leader(&self.job_number)
    }

    /// Fetch the build matrix for the current build
    pub fn build_matrix(&self) -> Result<Matrix, Error> {
        let url = format!("{}/builds/{}", self.travis_api_url, self.build_id);
        let mut client = reqwest::Client::new().unwrap();
        client.redirect(RedirectPolicy::limited(5));
        let mut res = client.get(&url)
            .header(UserAgent(USER_AGENT.to_string()))
            .send()
            .unwrap();

        if *res.status() == StatusCode::NotFound {
            return Err(Error::BuildNotFound);
        }

        res.json().map_err(|e| From::from(e))
    }

    /// Wait for all non-leader jobs to finish
    ///
    /// Returns an `Error::BuildNotFound` if this build is not known to Travis.
    /// Returns an `Error::FailedBuilds` if at least one non-leader build failed.
    ///
    /// This loops until it fails or succeeds, there is no way to exit the loop.
    pub fn wait_for_others(&self) -> Result<(), Error> {
        if !self.is_leader() {
            return Err(Error::NotLeader)
        }

        let dur = Duration::new(self.polling_interval, 0);
        loop {
            let matrix = try!(self.build_matrix());

            if matrix.others_finished() {
                break;
            }
            thread::sleep(dur);
        }

        let matrix = try!(self.build_matrix());
        match matrix.others_succeeded() {
            true => Ok(()),
            false => Err(Error::FailedBuilds)
        }
    }
}