package jetbrains.buildServer.runAs.server;

import java.util.*;
import jetbrains.buildServer.runAs.common.Constants;
import jetbrains.buildServer.serverSide.*;
import jetbrains.buildServer.serverSide.parameters.types.PasswordsProvider;
import jetbrains.buildServer.util.StringUtil;
import org.jetbrains.annotations.NotNull;

public class RunAsPasswordsProvider implements PasswordsProvider {
  private final RunAsConfiguration myRunAsConfiguration;

  public RunAsPasswordsProvider(@NotNull final RunAsConfiguration runAsConfiguration) {
    myRunAsConfiguration = runAsConfiguration;
  }

  @NotNull
  @Override
  public Collection<Parameter> getPasswordParameters(@NotNull final SBuild sBuild) {
    final ArrayList<Parameter> passwords = new ArrayList<Parameter>();

    final SRunningBuild build = sBuild.getAgent().getRunningBuild();

    if(myRunAsConfiguration.getIsUiSupported()) {
      if (build != null) {
        final SBuildType buildType = build.getBuildType();
        if (buildType != null) {
          for (SBuildRunnerDescriptor runner : buildType.getBuildRunners()) {
            final String password = runner.getParameters().get(Constants.PASSWORD);
            if (!StringUtil.isEmpty(password)) {
              passwords.add(new SimpleParameter(Constants.PASSWORD + "_" + runner.getId(), password));
            }
          }
        }
      }

      for (SBuildFeatureDescriptor buildFeature : sBuild.getBuildFeaturesOfType(Constants.BUILD_FEATURE_TYPE)) {
        if (!Constants.BUILD_FEATURE_TYPE.equalsIgnoreCase(buildFeature.getType())) {
          continue;
        }

        final Map<String, String> params = buildFeature.getParameters();
        if (!params.containsKey(Constants.PASSWORD)) {
          continue;
        }

        final String password = params.get(Constants.PASSWORD);
        if (!StringUtil.isEmpty(password)) {
          passwords.add(new SimpleParameter(Constants.PASSWORD + "_" + buildFeature.getId(), password));
        }
      }
    }
    else {
      if (build != null) {
        final SBuildAgent agent = build.getAgent();
        final String password = agent.getConfigurationParameters().get(Constants.PASSWORD);
        if (!StringUtil.isEmpty(password)) {
          passwords.add(new SimpleParameter(Constants.PASSWORD + "_" + agent.getId(), password));
        }
      }
    }

    final Map<String, String> buildParams = sBuild.getBuildOwnParameters();
    final String password = buildParams.get(Constants.PASSWORD);
    if(!StringUtil.isEmpty(password)) {
      passwords.add(new SimpleParameter(Constants.PASSWORD, password));
    }

    return passwords;
  }
}

@@ -55,17 +55,76 @@
 va.util.
-*
+ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.Map
 ;%0Aimport
@@ -741,18 +741,17 @@
  SBuild 
-sB
+b
 uild) %7B%0A
@@ -826,84 +826,16 @@
 );%0A%0A
-    final SRunningBuild build = sBuild.getAgent().getRunningBuild();%0A
 %0A    if
+ 
 (myR
@@ -878,37 +878,8 @@
 ) %7B%0A
-      if (build != null) %7B%0A  
     
@@ -933,26 +933,24 @@
 pe();%0A      
-  
 if (buildTyp
@@ -966,26 +966,24 @@
 ) %7B%0A        
- 
- 
 for (SBuildR
@@ -1034,26 +1034,24 @@
 unners()) %7B%0A
-  
           fi
@@ -1126,26 +1126,24 @@
 ;%0A          
- 
- 
 if (!StringU
@@ -1159,34 +1159,32 @@
 ty(password)) %7B%0A
-  
             pass
@@ -1272,22 +1272,8 @@
 ));%0A
-            %7D%0A
     
@@ -1349,18 +1349,17 @@
 ature : 
-sB
+b
 uild.get
@@ -1928,20 +1928,16 @@
    %7D
-%0A   
  else %7B%0A
     
@@ -1936,37 +1936,8 @@
 e %7B%0A
-      if (build != null) %7B%0A  
     
@@ -1978,26 +1978,24 @@
 getAgent();%0A
-  
       final 
@@ -2070,34 +2070,32 @@
 ASSWORD);%0A      
-  
 if (!StringUtil.
@@ -2107,34 +2107,32 @@
 ty(password)) %7B%0A
-  
         password
@@ -2207,34 +2207,24 @@
 password));%0A
-        %7D%0A
       %7D%0A    
@@ -2274,10 +2274,9 @@
 s = 
-sB
+b
 uild

Fix issue of possibly getting wrong running build