-
Notifications
You must be signed in to change notification settings - Fork 614
Expand file tree
/
Copy pathNoteHookTriggerHandlerImplTest.java
More file actions
177 lines (165 loc) · 8.5 KB
/
Copy pathNoteHookTriggerHandlerImplTest.java
File metadata and controls
177 lines (165 loc) · 8.5 KB
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
package com.dabsquared.gitlabjenkins.trigger.handler.note;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.CommitBuilder.commit;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.MergeRequestObjectAttributesBuilder.mergeRequestObjectAttributes;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.NoteHookBuilder.noteHook;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.NoteObjectAttributesBuilder.noteObjectAttributes;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.ProjectBuilder.project;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.UserBuilder.user;
import static com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterConfig.BranchFilterConfigBuilder.branchFilterConfig;
import static com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilterFactory.newMergeRequestLabelFilter;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.State;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterFactory;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterType;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.FreeStyleProject;
import hudson.plugins.git.GitSCM;
import hudson.util.OneShotEvent;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;
/**
* @author Nikolay Ustinov
*/
public class NoteHookTriggerHandlerImplTest {
@ClassRule
public static JenkinsRule jenkins = new JenkinsRule();
@Rule
public TemporaryFolder tmp = new TemporaryFolder();
private NoteHookTriggerHandler noteHookTriggerHandler;
@Before
public void setup() {
noteHookTriggerHandler = new NoteHookTriggerHandlerImpl("ci-run");
}
@Test
public void note_ciSkip() throws IOException, InterruptedException {
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
buildTriggered.signal();
return true;
}
});
Date currentDate = new Date();
project.setQuietPeriod(0);
noteHookTriggerHandler.handle(
project,
noteHook()
.withObjectAttributes(noteObjectAttributes()
.withId(1)
.withNote("ci-run")
.withAuthorId(1)
.withProjectId(1)
.withCreatedAt(currentDate)
.withUpdatedAt(currentDate)
.withUrl("https://gitlab.org/test/merge_requests/1#note_1")
.build())
.withMergeRequest(mergeRequestObjectAttributes()
.withDescription("[ci-skip]")
.build())
.build(),
true,
BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(false));
}
@Test
public void note_build() throws IOException, InterruptedException, GitAPIException, ExecutionException {
Git.init().setDirectory(tmp.getRoot()).call();
tmp.newFile("test");
Git git = Git.open(tmp.getRoot());
git.add().addFilepattern("test");
RevCommit commit = git.commit().setMessage("test").call();
ObjectId head = git.getRepository().resolve(Constants.HEAD);
String repositoryUrl = tmp.getRoot().toURI().toString();
final OneShotEvent buildTriggered = new OneShotEvent();
FreeStyleProject project = jenkins.createFreeStyleProject();
project.setScm(new GitSCM(repositoryUrl));
project.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
buildTriggered.signal();
return true;
}
});
Date currentDate = new Date();
project.setQuietPeriod(0);
noteHookTriggerHandler.handle(
project,
noteHook()
.withObjectAttributes(noteObjectAttributes()
.withId(1)
.withNote("ci-run")
.withAuthorId(1)
.withProjectId(1)
.withCreatedAt(currentDate)
.withUpdatedAt(currentDate)
.withUrl("https://gitlab.org/test/merge_requests/1#note_1")
.build())
.withMergeRequest(mergeRequestObjectAttributes()
.withTargetBranch("refs/heads/"
+ git.nameRev().add(head).call().get(head))
.withState(State.opened)
.withIid(1)
.withTitle("test")
.withTargetProjectId(1)
.withSourceProjectId(1)
.withSourceBranch("feature")
.withTargetBranch("master")
.withLastCommit(commit().withAuthor(
user().withName("test").build())
.withId(commit.getName())
.build())
.withSource(project()
.withName("test")
.withNamespace("test-namespace")
.withHomepage("https://gitlab.org/test")
.withUrl("git@gitlab.org:test.git")
.withSshUrl("git@gitlab.org:test.git")
.withHttpUrl("https://gitlab.org/test.git")
.build())
.withTarget(project()
.withName("test")
.withNamespace("test-namespace")
.withHomepage("https://gitlab.org/test")
.withUrl("git@gitlab.org:test.git")
.withSshUrl("git@gitlab.org:test.git")
.withHttpUrl("https://gitlab.org/test.git")
.withWebUrl("https://gitlab.org/test.git")
.build())
.build())
.withUser(user().withId(1)
.withName("User")
.withUsername("user")
.withEmail("user@gitlab.com")
.withAvatarUrl(
"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon")
.build())
.build(),
true,
BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
newMergeRequestLabelFilter(null));
buildTriggered.block(10000);
assertThat(buildTriggered.isSignaled(), is(true));
}
}