aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/jenkinsci/plugins/bbprb/BitbucketAdditionalParameterEnvironmentContributor.java
blob: 2e9d8bc5f166de035f76397974057852a6b333fc (plain)
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
package org.jenkinsci.plugins.bbprb;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.*;

import java.io.IOException;

@Extension
public class BitbucketAdditionalParameterEnvironmentContributor
    extends EnvironmentContributor {
  @Override
  public void buildEnvironmentFor(Run run, EnvVars envVars,
                                  TaskListener taskListener)
      throws IOException, InterruptedException {

    BitbucketCause cause = (BitbucketCause)run.getCause(BitbucketCause.class);
    if (cause == null) {
      return;
    }

    putEnvVar(envVars, "destinationRepository",
              cause.getDestinationRepository());
    putEnvVar(envVars, "pullRequestAuthor", cause.getPullRequestAuthor());
    putEnvVar(envVars, "pullRequestId", cause.getPullRequestId());
    putEnvVar(envVars, "pullRequestTitle", cause.getPullRequestTitle());
    putEnvVar(envVars, "sourceBranch", cause.getSourceBranch());
    putEnvVar(envVars, "sourceRepository", cause.getSourceRepository());
    putEnvVar(envVars, "targetBranch", cause.getTargetBranch());
  }

  private static void putEnvVar(EnvVars envs, String name, String value) {
    envs.put(name, getString(value, ""));
  }

  private static String getString(String actual, String d) {
    return actual == null ? d : actual;
  }
}