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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/*
* Copyright 2011 gitblit.com.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gitblit.tests;
import groovy.lang.Binding;
import groovy.util.GroovyScriptEngine;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.ReceiveCommand;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.gitblit.GitBlitException;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.StringUtils;
/**
* Test class for Groovy scripts. Mostly this is to facilitate development.
*
* @author James Moger
*
*/
public class GroovyScriptTest extends GitblitUnitTest {
private static final AtomicBoolean started = new AtomicBoolean(false);
@BeforeClass
public static void startGitblit() throws Exception {
started.set(GitBlitSuite.startGitblit());
}
@AfterClass
public static void stopGitblit() throws Exception {
if (started.get()) {
GitBlitSuite.stopGitblit();
}
}
@Test
public void testFogbugz() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
commands.add(new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
commands.add(new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master2"));
RepositoryModel repository = repositories().getRepositoryModel("helloworld.git");
repository.customFields = new HashMap<String,String>();
repository.customFields.put( "fogbugzUrl", "http://bugs.test.com" );
repository.customFields.put( "fogbugzRepositoryId", "1" );
repository.customFields.put( "fogbugzCommitMessageRegex", "\\s*[Bb][Uu][Gg][(Zz)(Ss)]*\\s*[(IDs)]*\\s*[#:; ]+((\\d+[ ,:;#]*)+)" );
test("fogbugz.groovy", gitblit, logger, clientLogger, commands, repository);
}
@Test
public void testSendHtmlMail() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
commands.add(new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
commands.add(new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master2"));
RepositoryModel repository = repositories().getRepositoryModel("helloworld.git");
repository.mailingLists.add("list@helloworld.git");
test("sendmail-html.groovy", gitblit, logger, clientLogger, commands, repository);
assertEquals(1, logger.messages.size());
assertEquals(1, gitblit.messages.size());
MockMail m = gitblit.messages.get(0);
assertEquals(5, m.toAddresses.size());
assertTrue(m.message.contains("BIT"));
assertTrue(m.message.contains("<html>"));
}
@Test
public void testSendMail() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
commands.add(new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
commands.add(new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master2"));
RepositoryModel repository = repositories().getRepositoryModel("helloworld.git");
repository.mailingLists.add("list@helloworld.git");
test("sendmail.groovy", gitblit, logger, clientLogger, commands, repository);
assertEquals(1, logger.messages.size());
assertEquals(1, gitblit.messages.size());
MockMail m = gitblit.messages.get(0);
assertEquals(5, m.toAddresses.size());
assertTrue(m.message.contains("BIT"));
}
@Test
public void testProtectRefsCreateBranch() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
}
@Test
public void testProtectRefsCreateTag() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/tags/v1.0"));
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
assertEquals(0, logger.messages.size());
}
@Test
public void testProtectRefsFastForward() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
commands.add(new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
assertEquals(0, logger.messages.size());
}
@Test
public void testProtectRefsDeleteMasterBranch() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
ReceiveCommand command = new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), ObjectId.zeroId(),
"refs/heads/master");
commands.add(command);
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
assertEquals(0, logger.messages.size());
}
@Test
public void testProtectRefsDeleteOtherBranch() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
commands.add(new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), ObjectId.zeroId(),
"refs/heads/other"));
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
assertEquals(0, logger.messages.size());
}
@Test
public void testProtectRefsDeleteTag() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
ReceiveCommand command = new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), ObjectId.zeroId(),
"refs/tags/v1.0");
commands.add(command);
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
assertEquals(0, logger.messages.size());
}
@Test
public void testBlockPush() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
commands.add(new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
try {
test("blockpush.groovy", gitblit, logger, clientLogger, commands, repository);
assertTrue("blockpush should have failed!", false);
} catch (GitBlitException e) {
assertTrue(e.getMessage().contains("failed"));
}
}
@Test
public void testClientLogging() throws Exception {
MockGitblit gitblit = new MockGitblit();
MockLogger logger = new MockLogger();
MockClientLogger clientLogger = new MockClientLogger();
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
commands.add(new ReceiveCommand(ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
.fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
File groovyDir = repositories().getHooksFolder();
File tempScript = File.createTempFile("testClientLogging", "groovy", groovyDir);
tempScript.deleteOnExit();
BufferedWriter writer = new BufferedWriter(new FileWriter(tempScript));
writer.write("clientLogger.info('this is a test message')\n");
writer.flush();
writer.close();
test(tempScript.getName(), gitblit, logger, clientLogger, commands, repository);
assertTrue("Message Missing", clientLogger.messages.contains("this is a test message"));
}
private void test(String script, MockGitblit gitblit, MockLogger logger, MockClientLogger clientLogger,
List<ReceiveCommand> commands, RepositoryModel repository) throws Exception {
UserModel user = new UserModel("mock");
String gitblitUrl = GitBlitSuite.url;
File groovyDir = repositories().getHooksFolder();
GroovyScriptEngine gse = new GroovyScriptEngine(groovyDir.getAbsolutePath());
Binding binding = new Binding();
binding.setVariable("gitblit", gitblit);
binding.setVariable("repository", repository);
binding.setVariable("user", user);
binding.setVariable("commands", commands);
binding.setVariable("url", gitblitUrl);
binding.setVariable("logger", logger);
binding.setVariable("clientLogger", clientLogger);
Object result = gse.run(script, binding);
if (result instanceof Boolean) {
if (!((Boolean) result)) {
throw new GitBlitException(MessageFormat.format(
"Groovy script {0} has failed! Hook scripts aborted.", script));
}
}
}
class MockGitblit {
List<MockMail> messages = new ArrayList<MockMail>();
public Repository getRepository(String name) throws Exception {
return GitBlitSuite.getHelloworldRepository();
}
public List<String> getStrings(String key) {
return Arrays.asList("alpha@aaa.com", "beta@bee.com", "gamma@see.com");
}
public List<String> getRepositoryTeams(RepositoryModel repository) {
return Arrays.asList("testteam");
}
public TeamModel getTeamModel(String name) {
TeamModel model = new TeamModel(name);
model.mailingLists.add("list@" + name + ".com");
return model;
}
public String getString(String key, String dv) {
return dv;
}
public boolean getBoolean(String key, boolean dv) {
return dv;
}
public int getInteger(String key, int defaultValue) {
return defaultValue;
}
public void sendMail(String subject, String message, Collection<String> toAddresses) {
messages.add(new MockMail(subject, message, toAddresses));
}
public void sendHtmlMail(String subject, String message, Collection<String> toAddresses) {
messages.add(new MockMail(subject, message, toAddresses));
}
}
class MockLogger {
List<String> messages = new ArrayList<String>();
public void info(String message) {
messages.add(message);
}
}
class MockClientLogger {
List<String> messages = new ArrayList<String>();
public void info(String message) {
messages.add(message);
}
public void error(String message) {
messages.add(message);
}
public void error(String message, Throwable t) {
PrintWriter writer = new PrintWriter(new StringWriter());
if (!StringUtils.isEmpty(message)) {
writer.append(message);
writer.append('\n');
}
t.printStackTrace(writer);
messages.add(writer.toString());
}
}
class MockMail {
final Collection<String> toAddresses;
final String subject;
final String message;
MockMail(String subject, String message, Collection<String> toAddresses) {
this.subject = subject;
this.message = message;
this.toAddresses = toAddresses;
}
@Override
public String toString() {
return StringUtils.flattenStrings(toAddresses, ", ") + "\n\n" + subject + "\n\n"
+ message;
}
}
}
|