package com.cloudbees.plugins.credentials;

import hudson.model.InvisibleAction;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.Run;
import jenkins.model.RunAction2;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class CredentialsParametersAction extends InvisibleAction implements RunAction2, Iterable<CredentialsParameterValue> {

    private final Map<String, CredentialsParameterValue> values;

    public CredentialsParametersAction() {
        this(new HashMap<>());
    }

    private CredentialsParametersAction(Map<String, CredentialsParameterValue> values) {
        this.values = values;
    }

    static @CheckForNull CredentialsParametersAction forRun(@Nonnull Run<?, ?> run) {
        final CredentialsParametersAction existingAction = run.getAction(CredentialsParametersAction.class);
        if (existingAction != null) {
            return existingAction;
        }
        if (run.getAction(ParametersAction.class) == null) {
            return null;
        }
        run.addAction(new CredentialsParametersAction());
        // onAttached copies existing ParametersAction
        return run.getAction(CredentialsParametersAction.class);
    }

    public void add(@Nonnull CredentialsParameterValue value) {
        values.put(value.getName(), value);
    }

    public void addFromParameterValues(@Nonnull Collection<ParameterValue> values) {
        values.stream()
                .filter(CredentialsParameterValue.class::isInstance)
                .map(CredentialsParameterValue.class::cast)
                .forEach(this::add);
    }

    @CheckForNull CredentialsParameterValue findParameterByName(@Nonnull String name) {
        return values.get(name);
    }

    @CheckForNull CredentialsParameterValue findParameterByValue(@Nonnull String value) {
        return values.values().stream().filter(v -> value.equals(v.getValue())).findFirst().orElse(null);
    }

    @Override
    public void onAttached(Run<?, ?> r) {
        final ParametersAction action = r.getAction(ParametersAction.class);
        if (action != null) {
            for (ParameterValue value : action.getParameters()) {
                if (value instanceof CredentialsParameterValue) {
                    add((CredentialsParameterValue) value);
                }
            }
        }
    }

    @Override
    public void onLoad(Run<?, ?> r) {
        // this space intentionally left blank
    }

    @Override
    public @Nonnull Iterator<CredentialsParameterValue> iterator() {
        return values.values().iterator();
    }
}

@@ -37,16 +37,90 @@
 tials;%0A%0A
+import hudson.EnvVars;%0Aimport hudson.model.EnvironmentContributingAction;%0A
 import h
@@ -544,16 +544,47 @@
 Action2,
+ EnvironmentContributingAction,
  Iterabl
@@ -2677,32 +2677,306 @@
 ft blank%0A    %7D%0A%0A
+    @Override%0A    public void buildEnvironment(@Nonnull Run%3C?, ?%3E run, @Nonnull EnvVars env) %7B%0A        for (Map.Entry%3CString, CredentialsParameterValue%3E entry : values.entrySet()) %7B%0A            env.putIfNotNull(entry.getKey(), entry.getValue().getValue());%0A        %7D%0A    %7D%0A%0A
     @Override%0A  

Add credential input params to build environment