Skip to content
This repository was archived by the owner on Feb 7, 2022. It is now read-only.

Commit 32ee460

Browse files
committed
let user pass his token
1 parent 2e21e33 commit 32ee460

29 files changed

+516
-155
lines changed

manifest.yml

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ buildpack: java_buildpack
77
env:
88
admin_password: MyP@$$w0rd
99
admin_username: my@dm1n
10-
github_token: replacewithyourtoken
1110
pivotaltracker_token: replacewithyourtoken
1211
spring_profiles_active: cloud
1312
refresh_mirror_after_minutes: "120"

pom.xml

+12
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@
7676
<groupId>com.github.ArthurHlt</groupId>
7777
<artifactId>pivotaltracker-java-client</artifactId>
7878
<version>0.0.20</version>
79+
<exclusions>
80+
<exclusion>
81+
<groupId>org.slf4j</groupId>
82+
<artifactId>slf4j-simple</artifactId>
83+
</exclusion>
84+
</exclusions>
7985
</dependency>
8086
<dependency>
8187
<groupId>org.quartz-scheduler</groupId>
@@ -92,6 +98,12 @@
9298
<artifactId>spring-boot-starter-test</artifactId>
9399
<scope>test</scope>
94100
</dependency>
101+
<dependency>
102+
<groupId>org.easytesting</groupId>
103+
<artifactId>fest-assert</artifactId>
104+
<version>1.4</version>
105+
<scope>test</scope>
106+
</dependency>
95107
</dependencies>
96108

97109
<dependencyManagement>

src/main/java/com/orange/clara/pivotaltrackermirror/config/GithubConfig.java

-63
This file was deleted.

src/main/java/com/orange/clara/pivotaltrackermirror/connectors/Connector.java

+2
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public interface Connector<T, P> {
2424
T convertStory(Story story);
2525

2626
P convertComment(Comment comment);
27+
28+
void loadClient(String token);
2729
}

src/main/java/com/orange/clara/pivotaltrackermirror/connectors/GithubConnector.java

+52-16
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
import com.orange.clara.pivotaltrackermirror.exceptions.ConnectorException;
66
import com.orange.clara.pivotaltrackermirror.exceptions.ConnectorPostCommentException;
77
import com.orange.clara.pivotaltrackermirror.exceptions.ConnectorPostStoryException;
8+
import com.orange.clara.pivotaltrackermirror.github.client.ProxyGithubClient;
89
import com.orange.clara.pivotaltrackermirror.model.MirrorReference;
10+
import com.orange.clara.pivotaltrackermirror.util.MarkdownSanitizer;
911
import onespot.pivotal.api.resources.Story;
1012
import org.eclipse.egit.github.core.*;
13+
import org.eclipse.egit.github.core.client.GitHubClient;
1114
import org.eclipse.egit.github.core.service.IssueService;
15+
import org.eclipse.egit.github.core.service.LabelService;
1216
import org.slf4j.Logger;
1317
import org.slf4j.LoggerFactory;
1418

1519
import java.io.IOException;
20+
import java.sql.Date;
21+
import java.text.SimpleDateFormat;
1622
import java.util.List;
1723
import java.util.regex.Matcher;
1824
import java.util.regex.Pattern;
@@ -34,12 +40,14 @@ public class GithubConnector implements Connector<Issue, Comment> {
3440
protected final static String COMMENT_ID_KEY = "Comment Id: ";
3541
protected final static String STORY_ID_PATTERN = ".*" + STORY_ID_KEY + "([0-9]+)$";
3642
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";
3745
private Logger logger = LoggerFactory.getLogger(this.getClass());
3846

3947
private IssueService issueService;
48+
private LabelService labelService;
4049

41-
public GithubConnector(IssueService issueService) {
42-
this.issueService = issueService;
50+
public GithubConnector() {
4351
}
4452

4553
protected RepositoryId getRepositoryId(String target) throws ConnectorException {
@@ -58,6 +66,7 @@ public Issue postOrUpdateStory(MirrorReference mirrorReference, Issue issue) thr
5866

5967
RepositoryId repositoryId = this.getRepositoryId(mirrorReference.getTarget());
6068
try {
69+
this.createLabelsOnGithub(repositoryId, issue.getLabels());
6170
if (mirrorReference.getUpdatedAt() == null) {
6271
logger.debug("Posting new issue on github with title '{}'.", issue.getTitle());
6372
this.waitSend();
@@ -123,6 +132,10 @@ public Issue convertStory(Story story) {
123132
List<Label> labels = Lists.newArrayList();
124133
labels.add(this.createLabelForStatus(story.currentState));
125134
labels.add(new Label().setName(story.storyType.name()));
135+
if (story.getLabels() != null) {
136+
labels.addAll(this.convertLabel(story.getLabels()));
137+
}
138+
126139
issue.setLabels(labels);
127140
issue.setBody(this.createBodyStory(story));
128141
return issue;
@@ -135,6 +148,30 @@ public Comment convertComment(onespot.pivotal.api.resources.Comment comment) {
135148
return githubComment;
136149
}
137150

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+
138175
protected void waitSend() throws ConnectorException {
139176
try {
140177
Thread.sleep(2000L);
@@ -251,22 +288,26 @@ private List<Label> convertLabel(List<onespot.pivotal.api.resources.Label> label
251288

252289
protected String createBodyComment(onespot.pivotal.api.resources.Comment comment) {
253290
List<String> commentBody = Lists.newArrayList();
254-
commentBody.add("- **Link**: " + comment.getUrl());
291+
String personName = "*Anonymous*";
292+
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_LONG_PATTERN);
255293
if (comment.getPerson() != null) {
256-
commentBody.add("- **User**: " + comment.getPerson().name);
294+
personName = comment.getPerson().name;
257295
}
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-->");
261299
return Joiner.on("\n").join(commentBody);
262300
}
263301

264302
protected String createBodyStory(Story story) {
265303
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("\nMirror: [story %s](%s) submitted on %s", story.id, story.getUrl(), dateFormat.format(Date.from(story.getCreatedAt()))));
270311
if (story.requester != null && story.requester.name != null) {
271312
storyBody.add("- **Requester**: " + story.requester.name);
272313
}
@@ -277,14 +318,9 @@ protected String createBodyStory(Story story) {
277318
if (story.estimate >= 0) {
278319
storyBody.add("- **Estimate**: " + story.estimate);
279320
}
280-
281-
storyBody.add("\n" + this.sanitizer(story.description));
282321
return Joiner.on("\n").join(storyBody);
283322
}
284323

285-
private String sanitizer(String text) {
286-
return text.replace(" @", "");
287-
}
288324

289325
public IssueService getIssueService() {
290326
return issueService;

src/main/java/com/orange/clara/pivotaltrackermirror/controllers/AbstractController.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,15 @@ protected JobStatus getTriggerState(Integer id) throws SchedulerException {
3636
return JobStatus.RUNNING;
3737
}
3838
Trigger.TriggerState triggerState = scheduler.getTriggerState(triggerKey);
39-
if (triggerState.equals(Trigger.TriggerState.BLOCKED)
40-
|| triggerState.equals(Trigger.TriggerState.ERROR)
41-
|| triggerState.equals(Trigger.TriggerState.PAUSED)
42-
|| triggerState.equals(Trigger.TriggerState.COMPLETE)) {
39+
if (mirrorReference.getLastJobStatus().equals(JobStatus.RUNNING)
40+
&& (
41+
triggerState.equals(Trigger.TriggerState.BLOCKED)
42+
|| triggerState.equals(Trigger.TriggerState.ERROR)
43+
|| triggerState.equals(Trigger.TriggerState.PAUSED)
44+
|| triggerState.equals(Trigger.TriggerState.COMPLETE)
45+
)) {
4346
return JobStatus.valueOf(scheduler.getTriggerState(triggerKey).name());
4447
}
45-
Trigger trigger = scheduler.getTrigger(triggerKey);
46-
if (trigger.getPreviousFireTime().before(mirrorReference.getUpdatedAt())
47-
|| trigger.getPreviousFireTime().equals(mirrorReference.getUpdatedAt())) {
48-
return JobStatus.COMPLETE;
49-
}
50-
return JobStatus.RUNNING;
48+
return mirrorReference.getLastJobStatus();
5149
}
5250
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.orange.clara.pivotaltrackermirror.controllers;
2+
3+
import com.google.common.collect.Lists;
4+
import com.orange.clara.pivotaltrackermirror.model.ConverterType;
5+
import com.orange.clara.pivotaltrackermirror.model.response.ConverterTypeResponse;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import java.util.List;
13+
14+
/**
15+
* Copyright (C) 2016 Orange
16+
* <p>
17+
* This software is distributed under the terms and conditions of the 'Apache-2.0'
18+
* license which can be found in the file 'LICENSE' in this package distribution
19+
* or at 'https://opensource.org/licenses/Apache-2.0'.
20+
* <p>
21+
* Author: Arthur Halet
22+
* Date: 29/07/2016
23+
*/
24+
@RestController
25+
@RequestMapping("/api/converterTypes")
26+
public class ConverterTypeController {
27+
28+
@RequestMapping(method = RequestMethod.GET, value = "", produces = MediaType.APPLICATION_JSON_VALUE)
29+
public ResponseEntity<?> getAll() {
30+
List<ConverterTypeResponse> converterTypeResponses = Lists.newArrayList();
31+
for (ConverterType converterType : ConverterType.values()) {
32+
converterTypeResponses.add(new ConverterTypeResponse(converterType));
33+
}
34+
return ResponseEntity.ok(converterTypeResponses);
35+
}
36+
}

src/main/java/com/orange/clara/pivotaltrackermirror/controllers/MirrorReferenceController.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.orange.clara.pivotaltrackermirror.exceptions.ConvertException;
66
import com.orange.clara.pivotaltrackermirror.job.MirrorJob;
77
import com.orange.clara.pivotaltrackermirror.model.MirrorReference;
8+
import com.orange.clara.pivotaltrackermirror.model.request.MirrorReferenceRequest;
89
import org.quartz.*;
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.beans.factory.annotation.Qualifier;
@@ -26,7 +27,7 @@
2627
* Date: 15/07/2016
2728
*/
2829
@RestController
29-
@RequestMapping("/api/mirrorReference")
30+
@RequestMapping("/api/mirrorReferences")
3031
public class MirrorReferenceController extends AbstractController {
3132

3233
@Autowired
@@ -37,12 +38,14 @@ public class MirrorReferenceController extends AbstractController {
3738
private Integer refreshMirrorMinutes;
3839

3940
@RequestMapping(method = RequestMethod.POST, value = "", produces = MediaType.APPLICATION_JSON_VALUE)
40-
public ResponseEntity<?> register(@RequestBody MirrorReference mirrorReference) throws ConvertException, CannotFindConverterException, SchedulerException {
41+
public ResponseEntity<?> register(@RequestBody MirrorReferenceRequest mirrorReferenceRequest) throws ConvertException, CannotFindConverterException, SchedulerException {
42+
MirrorReference mirrorReference = mirrorReferenceRequest.toMirrorReference();
4143
mirrorReference.setUpdatedAt(null);
4244
mirrorReference = this.mirrorReferenceRepo.save(mirrorReference);
4345
JobDetail job = JobBuilder.newJob(MirrorJob.class)
4446
.withIdentity(MirrorJob.JOB_KEY_NAME + mirrorReference.getId(), MirrorJob.JOB_KEY_GROUP)
4547
.usingJobData(MirrorJob.JOB_MIRROR_REFERENCE_ID_KEY, String.valueOf(mirrorReference.getId()))
48+
.usingJobData(MirrorJob.JOB_MIRROR_TOKEN_KEY, mirrorReferenceRequest.getToken())
4649
.build();
4750
Trigger trigger = TriggerBuilder.newTrigger()
4851
.withIdentity(MirrorJob.TRIGGER_KEY_NAME + mirrorReference.getId(), MirrorJob.TRIGGER_KEY_GROUP)

src/main/java/com/orange/clara/pivotaltrackermirror/controllers/TaskStatusController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Date: 18/07/2016
2424
*/
2525
@RestController
26-
@RequestMapping("/api/task")
26+
@RequestMapping("/api/tasks")
2727
public class TaskStatusController extends AbstractController {
2828

2929
@RequestMapping(method = RequestMethod.GET, value = "/{id}/status", produces = MediaType.APPLICATION_JSON_VALUE)

src/main/java/com/orange/clara/pivotaltrackermirror/controllers/UiController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class UiController extends AbstractController {
3232
private PivotalTrackerConverterFactory converterFactory;
3333

3434
@RequestMapping("/")
35-
public String index(Model model, TimeZone timeZone) throws SchedulerException, CannotFindConverterException {
35+
public String index(Model model, TimeZone timeZone) throws SchedulerException, CannotFindConverterException, IllegalAccessException, InstantiationException {
3636
List<MirrorReferenceResponse> mirrorReferenceResponseList = Lists.newArrayList();
3737
Iterable<MirrorReference> mirrorReferences = this.mirrorReferenceRepo.findAll();
3838
for (MirrorReference mirrorReference : mirrorReferences) {

src/main/java/com/orange/clara/pivotaltrackermirror/converter/AbstractPivotalTrackerConverter.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public AbstractPivotalTrackerConverter(Connector<T, P> connector) {
3030
}
3131

3232
@Override
33-
public void convert(MirrorReference mirrorReference, StoryCompleteReference storyCompleteReference) throws ConvertException {
33+
public void convert(MirrorReference mirrorReference, StoryCompleteReference storyCompleteReference, String token) throws ConvertException {
3434
logger.debug("Converting {}", storyCompleteReference.getId());
35+
this.connector.loadClient(token);
3536
T convertedStory = this.connector.convertStory(storyCompleteReference);
3637
try {
3738
T finalStory = this.sendStory(mirrorReference, convertedStory);
@@ -43,9 +44,9 @@ public void convert(MirrorReference mirrorReference, StoryCompleteReference stor
4344
}
4445

4546
@Override
46-
public void convert(MirrorReference mirrorReference, List<StoryCompleteReference> storyCompleteReferences) throws ConvertException {
47+
public void convert(MirrorReference mirrorReference, List<StoryCompleteReference> storyCompleteReferences, String token) throws ConvertException {
4748
for (StoryCompleteReference storyCompleteReference : storyCompleteReferences) {
48-
this.convert(mirrorReference, storyCompleteReference);
49+
this.convert(mirrorReference, storyCompleteReference, token);
4950
}
5051
}
5152

src/main/java/com/orange/clara/pivotaltrackermirror/converter/PivotalTrackerConverter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
* Date: 13/07/2016
1818
*/
1919
public interface PivotalTrackerConverter {
20-
void convert(MirrorReference mirrorReference, StoryCompleteReference storyCompleteReference) throws ConvertException;
20+
void convert(MirrorReference mirrorReference, StoryCompleteReference storyCompleteReference, String token) throws ConvertException;
2121

22-
void convert(MirrorReference mirrorReference, List<StoryCompleteReference> storyCompleteReferences) throws ConvertException;
22+
void convert(MirrorReference mirrorReference, List<StoryCompleteReference> storyCompleteReferences, String token) throws ConvertException;
2323

2424
String createLink(MirrorReference mirrorReference);
2525
}

0 commit comments

Comments
 (0)