/hudson-core/src/main/java/hudson/cli/CommandDuringBuild.java

http://github.com/hudson/hudson · Java · 82 lines · 39 code · 8 blank · 35 comment · 5 complexity · 9abff734f434f9a1fdb0b91bd8a8c929 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010, InfraDNA, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.cli;
  25. import hudson.model.Hudson;
  26. import hudson.model.Job;
  27. import hudson.model.Run;
  28. import hudson.remoting.Callable;
  29. import org.kohsuke.args4j.CmdLineException;
  30. import java.io.IOException;
  31. /**
  32. * Base class for those commands that are valid only during a build.
  33. *
  34. * @author Kohsuke Kawaguchi
  35. */
  36. public abstract class CommandDuringBuild extends CLICommand {
  37. /**
  38. * This method makes sense only when called from within the build kicked by Hudson.
  39. * We use the environment variables that Hudson sets to determine the build that is being run.
  40. */
  41. protected Run getCurrentlyBuilding() throws CmdLineException {
  42. try {
  43. CLICommand c = CLICommand.getCurrent();
  44. if (c==null) throw new IllegalStateException("Not executing a CLI command");
  45. String[] envs = c.channel.call(new GetCharacteristicEnvironmentVariables());
  46. if (envs[0]==null || envs[1]==null)
  47. throw new CmdLineException("This CLI command works only when invoked from inside a build");
  48. Job j = Hudson.getInstance().getItemByFullName(envs[0],Job.class);
  49. if (j==null) throw new CmdLineException("No such job: "+envs[0]);
  50. try {
  51. Run r = j.getBuildByNumber(Integer.parseInt(envs[1]));
  52. if (r==null) throw new CmdLineException("No such build #"+envs[1]+" in "+envs[0]);
  53. return r;
  54. } catch (NumberFormatException e) {
  55. throw new CmdLineException("Invalid build number: "+envs[1]);
  56. }
  57. } catch (IOException e) {
  58. throw new CmdLineException("Failed to identify the build being executed",e);
  59. } catch (InterruptedException e) {
  60. throw new CmdLineException("Failed to identify the build being executed",e);
  61. }
  62. }
  63. /**
  64. * Gets the environment variables that points to the build being executed.
  65. */
  66. private static final class GetCharacteristicEnvironmentVariables implements Callable<String[],IOException> {
  67. public String[] call() throws IOException {
  68. return new String[] {
  69. System.getenv("JOB_NAME"),
  70. System.getenv("BUILD_NUMBER")
  71. };
  72. }
  73. }
  74. }