 (defn inputs-for-stage-instance [pipeline-instance]
   (let [revisions (:material_revisions (:build_cause pipeline-instance))]
     (map revision->input revisions)))
-
 
 
 

                          :end end
                          :outcome outcome
                          :inputs inputs}
                   triggered-by (assoc :triggered-by triggered-by))})
       job-instances))

    (format "%s (Run %s)" pipeline-run stage-run)))


(defn- previous-stage-trigger [pipeline-name pipeline-run stage-name stages]
  (when-let [previous-stage (last (take-while #(not= stage-name (:name %)) stages))]
    [{:job-name (job-name pipeline-name (:name previous-stage) (:name previous-stage))
      :build-id (build-id pipeline-run (:counter previous-stage))}]))


(defn- pipeline-build-cause [{:keys [modifications material changed]}]
  (when (and changed (= "Pipeline" (:type material)))
    (let [revision-tokens (str/split (:revision (first modifications)) #"/")
          pipeline-name (nth revision-tokens 0)
          pipeline-run (nth revision-tokens 1)
          stage-name (nth revision-tokens 2)
          stage-run (nth revision-tokens 3)]
      {:job-name (job-name pipeline-name stage-name stage-name)
       :build-id (build-id pipeline-run stage-run)})))

(defn- pipeline-material-triggers [pipeline-instance stages]
  (let [revisions (:material_revisions (:build_cause pipeline-instance))]
    (keep pipeline-build-cause revisions)))


(defn- first-stage? [stage-name stages]
  (= stage-name (:name (first stages))))

(defn- rerun? [stage-run]
  (not= "1" stage-run))

(defn build-triggers-for-stage-instance [pipeline-instance {:keys [pipeline-name pipeline-run stage-name stage-run]}]
  (let [stages (:stages pipeline-instance)]
    (if (first-stage? stage-name stages)
      (pipeline-material-triggers pipeline-instance stages)
      (when-not (rerun? stage-run)
        (previous-stage-trigger pipeline-name pipeline-run stage-name stages)))))



