File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\fsp\WorkspaceSnapshotSCM.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
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.fsp;
25
26 import hudson.scm.PollingResult;
27 import hudson.scm.SCM;
28 import hudson.scm.ChangeLogParser;
29 import hudson.scm.SCMDescriptor;
30 import hudson.scm.SCMRevisionState;
31 import hudson.model.AbstractProject;
32 import hudson.model.TaskListener;
33 import hudson.model.AbstractBuild;
34 import hudson.model.BuildListener;
35 import jenkins.model.Jenkins;
36 import hudson.model.Result;
37 import hudson.model.PermalinkProjectAction.Permalink;
38 import hudson.Launcher;
39 import hudson.FilePath;
40 import hudson.WorkspaceSnapshot;
41 import hudson.PermalinkList;
42
43 import java.io.IOException;
44 import java.io.File;
45
46 import org.kohsuke.stapler.DataBoundConstructor;
47
48 /**
49 * {@link SCM} that inherits the workspace from another build through {@link WorkspaceSnapshot}
50 *
51 * @author Kohsuke Kawaguchi
52 */
53 public class WorkspaceSnapshotSCM extends SCM {
54 /**
55 * The job name from which we inherit the workspace.
56 */
57 public String jobName;
58 /**
59 * The permalink name indicating the build from which to inherit the workspace.
60 */
61 public String permalink;
62
63 @DataBoundConstructor
64 public WorkspaceSnapshotSCM(String jobName, String permalink) {
65 this.jobName = jobName;
66 this.permalink = permalink;
67 }
68
69 /**
70 * {@link Exception} indicating that the resolution of the job/permalink failed.
71 */
72 private final class ResolvedFailedException extends Exception {
73 private ResolvedFailedException(String message) {
74 super(message);
75 }
76 }
77
78 private static class Snapshot {
79 final WorkspaceSnapshot snapshot;
80 final AbstractBuild<?,?> owner;
81 private Snapshot(WorkspaceSnapshot snapshot, AbstractBuild<?,?> owner) {
82 this.snapshot = snapshot;
83 this.owner = owner;
84 }
85
86 void restoreTo(FilePath dst,TaskListener listener) throws IOException, InterruptedException {
87 snapshot.restoreTo(owner,dst,listener);
88 }
89 }
90 /**
91 * Obtains the {@link WorkspaceSnapshot} object that this {@link SCM} points to,
92 * or throws {@link ResolvedFailedException} upon failing.
93 *
94 * @return never null.
95 */
96 public Snapshot resolve() throws ResolvedFailedException {
97 Jenkins h = Jenkins.getInstance();
98 AbstractProject<?,?> job = h.getItemByFullName(jobName, AbstractProject.class);
99 if(job==null) {
100 if(h.getItemByFullName(jobName)==null) {
101 AbstractProject nearest = AbstractProject.findNearest(jobName);
102 throw new ResolvedFailedException(Messages.WorkspaceSnapshotSCM_NoSuchJob(jobName,nearest.getFullName()));
103 } else
104 throw new ResolvedFailedException(Messages.WorkspaceSnapshotSCM_IncorrectJobType(jobName));
105 }
106
107 PermalinkList permalinks = job.getPermalinks();
108 Permalink p = permalinks.get(permalink);
109 if(p==null)
110 throw new ResolvedFailedException(Messages.WorkspaceSnapshotSCM_NoSuchPermalink(permalink,jobName));
111
112 AbstractBuild<?,?> b = (AbstractBuild<?,?>)p.resolve(job);
113 if(b==null)
114 throw new ResolvedFailedException(Messages.WorkspaceSnapshotSCM_NoBuild(permalink,jobName));
115
116 WorkspaceSnapshot snapshot = b.getAction(WorkspaceSnapshot.class);
117 if(snapshot==null)
118 throw new ResolvedFailedException(Messages.WorkspaceSnapshotSCM_NoWorkspace(jobName,permalink));
119
120 return new Snapshot(snapshot,b);
121 }
122
123 public SCMRevisionState calcRevisionsFromBuild(AbstractBuild<?, ?> build, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
124 return null;
125 }
126
127 protected PollingResult compareRemoteRevisionWith(AbstractProject project, Launcher launcher, FilePath workspace, TaskListener listener, SCMRevisionState baseline) throws IOException, InterruptedException {
128 return PollingResult.NO_CHANGES;
129 }
130
131 public boolean checkout(AbstractBuild build, Launcher launcher, FilePath workspace, BuildListener listener, File changelogFile) throws IOException, InterruptedException {
132 try {
133 resolve().restoreTo(workspace,listener);
134 return true;
135 } catch (ResolvedFailedException e) {
136 listener.error(e.getMessage()); // stack trace is meaningless
137 build.setResult(Result.FAILURE);
138 return false;
139 }
140 }
141
142 public ChangeLogParser createChangeLogParser() {
143 return null;
144 }
145
146 public SCMDescriptor<?> getDescriptor() {
147 return null;
148 }
149 }
150