From 4e1b559f26c6001a46d9240d25ed0bee1fbf73c4 Mon Sep 17 00:00:00 2001 From: Christoph Burgmer Date: Fri, 11 Feb 2022 22:42:19 +0100 Subject: [PATCH] Don't fail in sad path where input version cannot be deduced, but warn instead --- src/build_facts/concourse/builds.clj | 14 +++++++------ test/build_facts/concourse/builds_test.clj | 23 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/build_facts/concourse/builds.clj b/src/build_facts/concourse/builds.clj index 2ab390e..1d2fffa 100644 --- a/src/build_facts/concourse/builds.clj +++ b/src/build_facts/concourse/builds.clj @@ -3,7 +3,8 @@ [api :as api] [transform :as transform]] [clj-yaml.core :as yaml] - [clojure.java.io :as io])) + [clojure.java.io :as io] + [clojure.tools.logging :as log])) (defn config-for [concourse-target] (let [flyrc (io/file (System/getProperty "user.home") ".flyrc") @@ -42,12 +43,13 @@ (defn- triggering-build [resources {:keys [input-name from-previous-jobs versions-with-context]}] (when-let [input-version (build->triggered-input-version resources input-name)] - (let [version-with-context (->> versions-with-context + (if-let [version-with-context (->> versions-with-context (filter #(= input-version (:version %))) - first) - builds-with-input (concat @(:input-to version-with-context) - @(:output-of version-with-context))] - (map #(triggering-build-in-builds-with-same-resource-version % builds-with-input) from-previous-jobs)))) + first)] + (let [builds-with-input (concat @(:input-to version-with-context) + @(:output-of version-with-context))] + (map #(triggering-build-in-builds-with-same-resource-version % builds-with-input) from-previous-jobs)) + (log/warnf "No triggered-by information could be deduced as version history did not contain the relevant version %s" (prn-str input-version))))) (defn- with-build-info [config inputs-and-versions {:keys [id] :as build}] (let [plan (delay (api/build-plan config id)) diff --git a/test/build_facts/concourse/builds_test.clj b/test/build_facts/concourse/builds_test.clj index efcc700..bfad11d 100644 --- a/test/build_facts/concourse/builds_test.clj +++ b/test/build_facts/concourse/builds_test.clj @@ -797,6 +797,29 @@ :first_occurrence false}) (some-plan 4) (some-events 4)) + (let [[[_, [build]]] (sut/concourse-builds {:base-url "http://concourse:8000" + :bearer-token "fake-token" + :team-name "my-team"})] + (is (nil? (:triggered-by build)))))) + + (testing "should fail gracefully if version cannot be found" + (fake/with-fake-routes-in-isolation (serve-up (valid-session) + (all-jobs (a-job "my-team" "my-pipeline" "my-job" + {:inputs [{:name :git :trigger true :passed ["previous-job"]}]}) + (a-job "my-team" "my-pipeline" "previous-job")) + (some-builds "my-team" "my-pipeline" "my-job" + {:id 4 + :name "42" + :status "aborted" + :start_time (unix-time-in-s 2016 1 1 10 0 0) + :end_time (unix-time-in-s 2016 1 1 10 0 1)}) + (some-resources 4 + {:name "git" + :version {:ref "abcd1234"} + :first_occurrence true}) + (some-plan 4) + (some-events 4) + (some-resource-versions "my-team" "my-pipeline" "git")) (let [[[_, [build]]] (sut/concourse-builds {:base-url "http://concourse:8000" :bearer-token "fake-token" :team-name "my-team"})]