forked from bklarson/Jenkins-repo-plugin
/
ChangeLog.java
270 lines (249 loc) · 9.84 KB
/
ChangeLog.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* The MIT License
*
* Copyright (c) 2010, Brad Larson
*
* 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.
*/
package hudson.plugins.repo;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.plugins.repo.ChangeLogEntry.ModifiedFile;
import hudson.scm.ChangeLogParser;
import hudson.scm.RepositoryBrowser;
import hudson.util.AtomicFileWriter;
import hudson.util.XStream2;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xml.sax.SAXException;
import com.thoughtworks.xstream.io.StreamException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Utility functions to generate and parse a file listing the differences
* between builds. Differences are saved as a list of ChangeLogEntry.
*/
class ChangeLog extends ChangeLogParser {
private static Logger debug =
Logger.getLogger("hudson.plugins.repo.ChangeLog");
// TODO: Really need to add some unit tests for this class. That might
// require creating git commits, which will be tricky. See the git plugin
// for some possibilities.
@Override
@SuppressWarnings("unchecked")
public RepoChangeLogSet parse(
final Run build, final RepositoryBrowser<?> browser, final File changelogFile)
throws IOException, SAXException {
final List<ChangeLogEntry> r;
final XStream2 xs = new XStream2();
final Reader reader =
new BufferedReader(new InputStreamReader(new FileInputStream(
changelogFile), "UTF-8"));
try {
final Object obj = xs.fromXML(reader);
r = (List<ChangeLogEntry>) obj;
} finally {
reader.close();
}
return new RepoChangeLogSet(build, browser, r);
}
/**
* Generate a change log between two specified revision states and return it
* as a list of change log entries.
*
* @param currentState
* The current state of the repository
* @param previousState
* The previous state of the repository
* @param launcher
* The launcher used to run command-line programs
* @param workspace
* The FilePath of the workspace to use when computing
* differences. This path might be on a slave machine.
* @param showAllChanges
* Add --first-parent to "git log"
* @throws IOException
* is thrown if we have problems writing to the changelogFile
* @throws InterruptedException
* is thrown if we are interrupted while waiting on the git
* commands to run in a forked process.
*/
private static List<ChangeLogEntry> generateChangeLog(
@Nonnull final RevisionState currentState,
@Nullable final RevisionState previousState, final Launcher launcher,
final FilePath workspace, final boolean showAllChanges)
throws IOException,
InterruptedException {
final List<ProjectState> changes =
currentState.whatChanged(previousState);
debug.log(Level.FINEST, "generateChangeLog: changes " + changes);
if (changes == null || changes.size() == 0) {
// No changes or the first job
return null;
}
final List<String> commands = new ArrayList<String>(5);
final List<ChangeLogEntry> logs = new ArrayList<ChangeLogEntry>();
for (final ProjectState change : changes) {
debug.log(Level.FINEST, "change: " + change);
if (change.getRevision() == null) {
// This project was just added to the manifest.
logs.add(new ChangeLogEntry(change.getPath(), change
.getServerPath(), null, null, null, null, null, null,
null, "This project was added to the manifest.", null));
continue;
}
String newRevision = currentState.getRevision(change.getPath());
if (newRevision == null) {
// This project was just removed from the manifest.
logs.add(new ChangeLogEntry(change.getPath(), change
.getServerPath(), null, null, null, null, null, null,
null, "This project was removed from the manifest.",
null));
continue;
}
final FilePath gitdir = new FilePath(workspace, change.getPath());
commands.clear();
commands.add("git");
commands.add("log");
commands.add("--raw");
if (!showAllChanges) {
commands.add("--first-parent");
}
final String format = "[[<as7d9m1R_MARK_A>]]"
+ "%H[[<as7d9m1R_MARK_B>]"
+ "%an[[<as7d9m1R_MARK_B>]"
+ "%ae[[<as7d9m1R_MARK_B>]"
+ "%aD[[<as7d9m1R_MARK_B>]"
+ "%cn[[<as7d9m1R_MARK_B>]"
+ "%ce[[<as7d9m1R_MARK_B>]"
+ "%cD[[<as7d9m1R_MARK_B>]"
+ "%s\n%b[[<as7d9m1R_MARK_B>]";
commands.add("--format=\"" + format + "\"");
// TODO: make this work with the -M flag to show copied and renamed
// files.
// TODO: even better, use jgit to do the diff. It would be faster,
// more robust, etc. git was used to get this done faster, but jgit
// is definitely preferable. Most of the code can probably be copied
// from Gerrit. It might be tricky with master/slave setup.
commands.add(change.getRevision() + ".." + newRevision);
final OutputStream gitOutput = new ByteArrayOutputStream();
launcher.launch().stdout(gitOutput).pwd(gitdir).cmds(commands)
.join();
final String o = gitOutput.toString();
final String[] changelogs = o.split(
"\\[\\[<as7d9m1R_MARK_A>\\]\\]");
debug.log(Level.INFO, o);
for (final String changelog : changelogs) {
final String[] parts = changelog.split(
"\\[\\[<as7d9m1R_MARK_B>\\]");
if (parts.length < 9) {
// this is broken
continue;
}
final String revision = parts[0];
final String authorName = parts[1];
final String authorEmail = parts[2];
final String authorDate = parts[3];
final String committerName = parts[4];
final String committerEmail = parts[5];
final String committerDate = parts[6];
final String commitText = parts[7];
final String[] fileLines = parts[8].split("\n");
final List<ModifiedFile> modifiedFiles =
new ArrayList<ModifiedFile>();
for (final String fileLine : fileLines) {
if (!fileLine.startsWith(":")) {
continue;
}
final char action = fileLine.substring(37, 38).charAt(0);
final String path = fileLine.substring(39);
modifiedFiles.add(new ModifiedFile(path, action));
}
ChangeLogEntry nc = new ChangeLogEntry(change.getPath(), change
.getServerPath(), revision, authorName, authorEmail,
authorDate, committerName, committerEmail,
committerDate, commitText, modifiedFiles);
logs.add(nc);
debug.log(Level.FINEST, nc.toString());
}
}
return logs;
}
/**
* Generate a change log file containing the differences between one build
* and the next and save the result as XML in a specified file. The function
* uses git on the command line to determine the differences between
* commits.
*
* @param currentState
* The current state of the repository
* @param previousState
* The previous state of the repository
* @param changelogFile
* The file in which we will store the set of differences between
* the two states
* @param launcher
* The launcher used to run command-line programs
* @param workspace
* The FilePath of the workspace to use when computing
* differences. This path might be on a slave machine.
* @param showAllChanges
* Add --first-parent to "git log"
* @throws IOException
* is thrown if we have problems writing to the changelogFile
* @throws InterruptedException
* is thrown if we are interrupted while waiting on the git
* commands to run in a forked process.
*/
static void saveChangeLog(@Nonnull final RevisionState currentState,
@Nullable final RevisionState previousState, final File changelogFile,
final Launcher launcher, final FilePath workspace,
final boolean showAllChanges)
throws IOException, InterruptedException {
List<ChangeLogEntry> logs =
generateChangeLog(currentState, previousState, launcher,
workspace, showAllChanges);
if (logs == null) {
debug.info("No logs found");
return;
}
final XStream2 xs = new XStream2();
final AtomicFileWriter w = new AtomicFileWriter(changelogFile);
try {
w.write("<?xml version='1.0' encoding='UTF-8'?>\n");
xs.toXML(logs, w);
w.commit();
} catch (final StreamException e) {
throw new IOException("Could not save changelog", e);
} finally {
w.close();
}
}
}