5
5
import com .orange .clara .pivotaltrackermirror .exceptions .ConnectorException ;
6
6
import com .orange .clara .pivotaltrackermirror .exceptions .ConnectorPostCommentException ;
7
7
import com .orange .clara .pivotaltrackermirror .exceptions .ConnectorPostStoryException ;
8
+ import com .orange .clara .pivotaltrackermirror .github .client .ProxyGithubClient ;
8
9
import com .orange .clara .pivotaltrackermirror .model .MirrorReference ;
10
+ import com .orange .clara .pivotaltrackermirror .util .MarkdownSanitizer ;
9
11
import onespot .pivotal .api .resources .Story ;
10
12
import org .eclipse .egit .github .core .*;
13
+ import org .eclipse .egit .github .core .client .GitHubClient ;
11
14
import org .eclipse .egit .github .core .service .IssueService ;
15
+ import org .eclipse .egit .github .core .service .LabelService ;
12
16
import org .slf4j .Logger ;
13
17
import org .slf4j .LoggerFactory ;
14
18
15
19
import java .io .IOException ;
20
+ import java .sql .Date ;
21
+ import java .text .SimpleDateFormat ;
16
22
import java .util .List ;
17
23
import java .util .regex .Matcher ;
18
24
import java .util .regex .Pattern ;
@@ -34,12 +40,14 @@ public class GithubConnector implements Connector<Issue, Comment> {
34
40
protected final static String COMMENT_ID_KEY = "Comment Id: " ;
35
41
protected final static String STORY_ID_PATTERN = ".*" + STORY_ID_KEY + "([0-9]+)$" ;
36
42
protected final static String COMMENT_ID_PATTERN = "^" + COMMENT_ID_KEY + "([0-9]+)$" ;
43
+ protected final static String DATE_SHORT_PATTERN = "MMM d, yyyy z" ;
44
+ protected final static String DATE_LONG_PATTERN = "MMM d, yyyy, h.mm a z" ;
37
45
private Logger logger = LoggerFactory .getLogger (this .getClass ());
38
46
39
47
private IssueService issueService ;
48
+ private LabelService labelService ;
40
49
41
- public GithubConnector (IssueService issueService ) {
42
- this .issueService = issueService ;
50
+ public GithubConnector () {
43
51
}
44
52
45
53
protected RepositoryId getRepositoryId (String target ) throws ConnectorException {
@@ -58,6 +66,7 @@ public Issue postOrUpdateStory(MirrorReference mirrorReference, Issue issue) thr
58
66
59
67
RepositoryId repositoryId = this .getRepositoryId (mirrorReference .getTarget ());
60
68
try {
69
+ this .createLabelsOnGithub (repositoryId , issue .getLabels ());
61
70
if (mirrorReference .getUpdatedAt () == null ) {
62
71
logger .debug ("Posting new issue on github with title '{}'." , issue .getTitle ());
63
72
this .waitSend ();
@@ -123,6 +132,10 @@ public Issue convertStory(Story story) {
123
132
List <Label > labels = Lists .newArrayList ();
124
133
labels .add (this .createLabelForStatus (story .currentState ));
125
134
labels .add (new Label ().setName (story .storyType .name ()));
135
+ if (story .getLabels () != null ) {
136
+ labels .addAll (this .convertLabel (story .getLabels ()));
137
+ }
138
+
126
139
issue .setLabels (labels );
127
140
issue .setBody (this .createBodyStory (story ));
128
141
return issue ;
@@ -135,6 +148,30 @@ public Comment convertComment(onespot.pivotal.api.resources.Comment comment) {
135
148
return githubComment ;
136
149
}
137
150
151
+ @ Override
152
+ public void loadClient (String token ) {
153
+ if (issueService != null ) {
154
+ return ;
155
+ }
156
+ GitHubClient client = new ProxyGithubClient ();
157
+ if (token != null ) {
158
+ client .setOAuth2Token (token );
159
+ }
160
+ this .issueService = new IssueService (client );
161
+ this .labelService = new LabelService (client );
162
+ }
163
+
164
+ private void createLabelsOnGithub (RepositoryId repositoryId , List <Label > labels ) throws IOException {
165
+ List <Label > existingLabels = this .labelService .getLabels (repositoryId );
166
+ for (Label label : labels ) {
167
+ if (existingLabels .contains (label )) {
168
+ continue ;
169
+ }
170
+ this .labelService .createLabel (repositoryId , label );
171
+ }
172
+
173
+ }
174
+
138
175
protected void waitSend () throws ConnectorException {
139
176
try {
140
177
Thread .sleep (2000L );
@@ -251,22 +288,26 @@ private List<Label> convertLabel(List<onespot.pivotal.api.resources.Label> label
251
288
252
289
protected String createBodyComment (onespot .pivotal .api .resources .Comment comment ) {
253
290
List <String > commentBody = Lists .newArrayList ();
254
- commentBody .add ("- **Link**: " + comment .getUrl ());
291
+ String personName = "*Anonymous*" ;
292
+ SimpleDateFormat dateFormat = new SimpleDateFormat (DATE_LONG_PATTERN );
255
293
if (comment .getPerson () != null ) {
256
- commentBody . add ( "- **User**: " + comment .getPerson ().name ) ;
294
+ personName = comment .getPerson ().name ;
257
295
}
258
-
259
- commentBody .add ("\n " + this . sanitizer (comment .getText ()) + "\n " );
260
- commentBody .add (COMMENT_ID_KEY + comment .getId ());
296
+ commentBody . add ( String . format ( "%s [commented](%s) on %s:" , personName , comment . getUrl (), dateFormat . format ( Date . from ( comment . updatedAt ))));
297
+ commentBody .add ("\n " + MarkdownSanitizer . sanitize (comment .getText ()) + "\n " );
298
+ commentBody .add ("<!-- \n " + COMMENT_ID_KEY + comment .getId () + " \n -->" );
261
299
return Joiner .on ("\n " ).join (commentBody );
262
300
}
263
301
264
302
protected String createBodyStory (Story story ) {
265
303
List <String > storyBody = Lists .newArrayList ();
266
- storyBody .add ("- **Link**: " + story .getUrl ());
267
- if (story .getLabels () != null ) {
268
- storyBody .add ("- **Labels**: " + Joiner .on (", " ).join (this .convertLabel (story .getLabels ()).stream ().map (Label ::getName ).collect (Collectors .toList ())));
269
- }
304
+ SimpleDateFormat dateFormat = new SimpleDateFormat (DATE_SHORT_PATTERN );
305
+
306
+ storyBody .add (MarkdownSanitizer .sanitize (story .description ) + "\n " );
307
+
308
+ storyBody .add ("---" );
309
+
310
+ storyBody .add (String .format ("\n Mirror: [story %s](%s) submitted on %s" , story .id , story .getUrl (), dateFormat .format (Date .from (story .getCreatedAt ()))));
270
311
if (story .requester != null && story .requester .name != null ) {
271
312
storyBody .add ("- **Requester**: " + story .requester .name );
272
313
}
@@ -277,14 +318,9 @@ protected String createBodyStory(Story story) {
277
318
if (story .estimate >= 0 ) {
278
319
storyBody .add ("- **Estimate**: " + story .estimate );
279
320
}
280
-
281
- storyBody .add ("\n " + this .sanitizer (story .description ));
282
321
return Joiner .on ("\n " ).join (storyBody );
283
322
}
284
323
285
- private String sanitizer (String text ) {
286
- return text .replace (" @" , "" );
287
- }
288
324
289
325
public IssueService getIssueService () {
290
326
return issueService ;
0 commit comments