2
2
3
3
import hudson .model .AbstractBuild ;
4
4
import hudson .model .Cause ;
5
- import hudson .model .Result ;
6
5
import hudson .model .TaskListener ;
7
6
import hudson .model .queue .QueueTaskFuture ;
8
7
import hudson .plugins .git .util .BuildData ;
9
8
10
- import org .apache .commons .io .FileUtils ;
11
-
12
- import org .jenkinsci .plugins .ghprb .manager .GhprbBuildManager ;
13
- import org .jenkinsci .plugins .ghprb .manager .configuration .JobConfiguration ;
14
- import org .jenkinsci .plugins .ghprb .manager .factory .GhprbBuildManagerFactoryUtil ;
15
-
9
+ import org .jenkinsci .plugins .ghprb .extensions .GhprbCommentAppender ;
10
+ import org .jenkinsci .plugins .ghprb .extensions .GhprbExtension ;
16
11
import org .kohsuke .github .GHCommitState ;
17
12
import org .kohsuke .github .GHIssueState ;
18
13
import org .kohsuke .github .GHPullRequest ;
19
14
import org .kohsuke .github .GHUser ;
20
15
21
- import java .io .File ;
22
16
import java .io .IOException ;
23
17
import java .io .PrintStream ;
24
- import java .util .List ;
25
18
import java .util .logging .Level ;
26
19
import java .util .logging .Logger ;
27
20
@@ -123,20 +116,10 @@ public void onCompleted(AbstractBuild<?, ?> build, TaskListener listener) {
123
116
}
124
117
125
118
GHCommitState state ;
126
- if (build .getResult () == Result .SUCCESS ) {
127
- state = GHCommitState .SUCCESS ;
128
- } else if (build .getResult () == Result .UNSTABLE ) {
129
- state = GHCommitState .valueOf (GhprbTrigger .getDscp ().getUnstableAs ());
130
- } else {
131
- state = GHCommitState .FAILURE ;
132
- }
119
+ state = Ghprb .getState (build );
133
120
repo .createCommitStatus (build , state , "Build finished." , c .getPullID (), trigger .getCommitStatusContext (), listener .getLogger ());
134
121
135
-
136
- String publishedURL = GhprbTrigger .getDscp ().getPublishedURL ();
137
- if (publishedURL != null && !publishedURL .isEmpty ()) {
138
- buildResultMessage (build , listener , state , c );
139
- }
122
+ buildResultMessage (build , listener , state , c );
140
123
// close failed pull request automatically
141
124
if (state == GHCommitState .FAILURE && trigger .isAutoCloseFailedPullRequests ()) {
142
125
closeFailedRequest (listener , c );
@@ -158,89 +141,17 @@ private void closeFailedRequest(TaskListener listener, GhprbCause c) {
158
141
159
142
private void buildResultMessage (AbstractBuild <?, ?> build , TaskListener listener , GHCommitState state , GhprbCause c ) {
160
143
StringBuilder msg = new StringBuilder ();
161
- String commentFilePath = trigger .getCommentFilePath ();
162
-
163
- if (commentFilePath != null && !commentFilePath .isEmpty ()) {
164
- try {
165
- String scriptFilePathResolved = Ghprb .replaceMacros (build , commentFilePath );
166
-
167
- String content = FileUtils .readFileToString (new File (scriptFilePathResolved ));
168
- msg .append ("Build comment file: \n --------------\n " );
169
- msg .append (content );
170
- msg .append ("\n --------------\n " );
171
- } catch (IOException e ) {
172
- msg .append ("\n !!! Couldn't read commit file !!!\n " );
173
- listener .getLogger ().println ("Couldn't read comment file" );
174
- e .printStackTrace (listener .getLogger ());
175
- }
176
- }
177
-
178
- msg .append ("\n Refer to this link for build results (access rights to CI server needed): \n " );
179
- msg .append (generateCustomizedMessage (build ));
180
-
181
- int numLines = GhprbTrigger .getDscp ().getlogExcerptLines ();
182
- if (state != GHCommitState .SUCCESS && numLines > 0 ) {
183
- // on failure, append an excerpt of the build log
184
- try {
185
- // wrap log in "code" markdown
186
- msg .append ("\n \n **Build Log**\n *last " ).append (numLines ).append (" lines*\n " );
187
- msg .append ("\n ```\n " );
188
- List <String > log = build .getLog (numLines );
189
- for (String line : log ) {
190
- msg .append (line ).append ('\n' );
191
- }
192
- msg .append ("```\n " );
193
- } catch (IOException ex ) {
194
- listener .getLogger ().println ("Can't add log excerpt to commit comments" );
195
- ex .printStackTrace (listener .getLogger ());
196
- }
197
- }
198
-
199
- String buildMessage = null ;
200
- if (state == GHCommitState .SUCCESS ) {
201
- if (trigger .getMsgSuccess () != null && !trigger .getMsgSuccess ().isEmpty ()) {
202
- buildMessage = trigger .getMsgSuccess ();
203
- } else if (GhprbTrigger .getDscp ().getMsgSuccess (build ) != null
204
- && !GhprbTrigger .getDscp ().getMsgSuccess (build ).isEmpty ()) {
205
- buildMessage = GhprbTrigger .getDscp ().getMsgSuccess (build );
206
- }
207
- } else if (state == GHCommitState .FAILURE ) {
208
- if (trigger .getMsgFailure () != null && !trigger .getMsgFailure ().isEmpty ()) {
209
- buildMessage = trigger .getMsgFailure ();
210
- } else if (GhprbTrigger .getDscp ().getMsgFailure (build ) != null
211
- && !GhprbTrigger .getDscp ().getMsgFailure (build ).isEmpty ()) {
212
- buildMessage = GhprbTrigger .getDscp ().getMsgFailure (build );
144
+
145
+ for (GhprbExtension ext : Ghprb .getJobExtensions (trigger , GhprbCommentAppender .class )){
146
+ if (ext instanceof GhprbCommentAppender ) {
147
+ msg .append (((GhprbCommentAppender ) ext ).postBuildComment (build , listener ));
213
148
}
214
149
}
215
- // Only Append the build's custom message if it has been set.
216
- if (buildMessage != null && !buildMessage .isEmpty ()) {
217
- // When the msg is not empty, append a newline first, to seperate it from the rest of the String
218
- if (!"" .equals (msg .toString ())) {
219
- msg .append ("\n " );
220
- }
221
- msg .append (buildMessage );
222
- }
223
-
150
+
224
151
if (msg .length () > 0 ) {
225
152
listener .getLogger ().println (msg );
226
153
repo .addComment (c .getPullID (), msg .toString (), build , listener );
227
154
}
228
155
}
229
156
230
- private String generateCustomizedMessage (AbstractBuild <?, ?> build ) {
231
- JobConfiguration jobConfiguration = JobConfiguration .builder ()
232
- .printStackTrace (trigger .isDisplayBuildErrorsOnDownstreamBuilds ()).build ();
233
-
234
- GhprbBuildManager buildManager = GhprbBuildManagerFactoryUtil .getBuildManager (build , jobConfiguration );
235
-
236
- StringBuilder sb = new StringBuilder ();
237
-
238
- sb .append (buildManager .calculateBuildUrl ());
239
-
240
- if (build .getResult () != Result .SUCCESS ) {
241
- sb .append (buildManager .getTestResults ());
242
- }
243
-
244
- return sb .toString ();
245
- }
246
157
}
0 commit comments