Skip to content

Commit

Permalink
Merge pull request #8 from oleg-nenashev/feature/JENKINS-43535
Browse files Browse the repository at this point in the history
[JENKINS-43535] - Minimize usage of AbstractProject/AbstractBuild
  • Loading branch information
oleg-nenashev committed Apr 27, 2017
2 parents 095fe77 + 94a7952 commit daec08d
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 7 deletions.
33 changes: 33 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,33 @@
Changelog
===

### 1.25

Release date: Coming soon

* [JENKINS-43845](https://issues.jenkins-ci.org/browse/JENKINS-43845) -
Deprecate obsolete utility methods in the library.
Methods have been moved to the [EnvInject API Plugin](https://plugins.jenkins.io/envinject-api).
* [JENKINS-43535](https://issues.jenkins-ci.org/browse/JENKINS-43535) -
Make `EnvInjectAction` API compatible with non-`AbstractProject` job types like Jenkins Pipeline

#### Developer notes

* Starting from this version, the library should not be directly used by plugins
* Use dependency on the [EnvInject API Plugin](https://plugins.jenkins.io/envinject-api) instead.
* Remove explicit dependency on the EnvInject Library.
* Replace all usages of the
`org.jenkinsci.lib.envinject.service` package by the new methods offered by the plugin.

# 1.24

Release date: _Jul 01, 2016_

* [JENKINS-36184](https://issues.jenkins-ci.org/browse/JENKINS-36184) -
Remove implicit dependency on the [Matrix Project Plugin](https://plugins.jenkins.io/matrix-project),
which has been detached from the core.
It was causing regressions in Jenkins installations without this plugin.

### Changes before 1.24

See the commit history
22 changes: 22 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,22 @@
The MIT License

Copyright (c) 2012-2017 Gregory Boissinot, Oleg Nenashev, and other Jenkins contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

20 changes: 20 additions & 0 deletions README.md
@@ -0,0 +1,20 @@
Jenkins EnvInject Library
===

Provides basic API for declaring and retrieving environment variables in Jenkins builds.

## Usage

Starting from `1.25` this library is provided via in the [EnvInject API Plugin](https://plugins.jenkins.io/envinject-api) and should not be used as a direct dependency in Jenkins plugins.
The library guarantees backward compatibility of API and does not contribute any user-visible components by default.

You can see usage examples in the [EnvInject Plugin](https://plugins.jenkins.io/envinject).

## Documentation

* [Changelog](CHANGELOG.md)
* [EnvInject API Plugin Documentation](https://github.com/jenkinsci/envinject-api-plugin/) - migrated API documentation

## License

[MIT License](https://opensource.org/licenses/mit-license.php)
40 changes: 33 additions & 7 deletions src/main/java/org/jenkinsci/lib/envinject/EnvInjectAction.java
Expand Up @@ -2,8 +2,7 @@

import com.google.common.collect.Maps;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Job;
import hudson.model.Run;
import org.apache.commons.collections.map.UnmodifiableMap;
import org.jenkinsci.lib.envinject.service.EnvInjectSavable;
Expand All @@ -16,31 +15,58 @@
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.RunAction2;

//TODO: Convert to RunAction2
/**
* @author Gregory Boissinot
*/
public class EnvInjectAction implements Action, StaplerProxy {
public class EnvInjectAction implements RunAction2, StaplerProxy {

public static final String URL_NAME = "injectedEnvVars";

protected transient @CheckForNull Map<String, String> envMap;

private @Nonnull AbstractBuild build;
private transient @Nonnull Run<?, ?> build;

@Override
public void onAttached(Run<?, ?> run) {
build = run;
}

@Override
public void onLoad(Run<?, ?> run) {
build = run;
}

/**
* Backward compatibility
*/
private transient Map<String, String> resultVariables;
private transient File rootDir;
private transient @CheckForNull Set<String> sensibleVariables;

/**
* Constructs action for the specified environment variables.
* @param build Build
* @param envMap Environment Map
* @deprecated The action implements {@link RunAction2} now, hence passing build is not required anymore.
* Use {@link #EnvInjectAction(java.util.Map)}.
*/
@Deprecated
public EnvInjectAction(@Nonnull AbstractBuild build,
@CheckForNull Map<String, String> envMap) {
this.build = build;
this.envMap = envMap;
}

/**
* Constructs action for the specified environment variables.
* @param envMap Environment Map
* @since 0.25
*/
public EnvInjectAction(@CheckForNull Map<String, String> envMap) {
this.envMap = envMap;
}

public void overrideAll(Map<String, String> all) {
overrideAll(Collections.<String>emptySet(), all);
Expand Down Expand Up @@ -114,13 +140,13 @@ public String transformEntry(String key, String value) {
}


private Map<String, String> getEnvironment(@CheckForNull AbstractBuild build) throws EnvInjectException {
private Map<String, String> getEnvironment(@CheckForNull Run<?, ?> build) throws EnvInjectException {

if (build == null) {
return null;
}

AbstractProject project = build.getProject();
Job<?, ?> project = build.getParent();
if (project == null) {
return null;
}
Expand Down
Expand Up @@ -9,10 +9,15 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* @author Gregory Boissinot
* @deprecated The actual version of this API class is located in EnvInject API Plugin
*/
@Deprecated
@Restricted(NoExternalUse.class)
public class EnvInjectActionRetriever {

//Returns the abstract class Action due to a class loading issue
Expand Down
Expand Up @@ -4,10 +4,15 @@
import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.Hudson;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* @author Gregory Boissinot
* @deprecated The actual version of this API class is located in EnvInject API Plugin
*/
@Deprecated
@Restricted(NoExternalUse.class)
public class EnvInjectDetector {

public boolean isEnvInjectActivated(AbstractBuild build) {
Expand Down
Expand Up @@ -7,10 +7,15 @@
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* @author Gregory Boissinot
* @deprecated The actual version of this API class is located in EnvInject API Plugin
*/
@Deprecated
@Restricted(NoExternalUse.class)
public class EnvInjectSavable {

private static final String ENVINJECT_TXT_FILENAME = "injectedEnvVars.txt";
Expand Down
Expand Up @@ -16,10 +16,15 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

/**
* @author Gregory Boissinot
* @deprecated The actual version of this API class is located in EnvInject API Plugin
*/
@Deprecated
@Restricted(NoExternalUse.class)
public class EnvVarsResolver implements Serializable {

public Map<String, String> getPollingEnvVars(AbstractProject project, /*can be null*/ Node node) throws EnvInjectException {
Expand Down

0 comments on commit daec08d

Please sign in to comment.