-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…racting SendReviews cron (#38)
- Loading branch information
Showing
28 changed files
with
497 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/main/java/dev/iakunin/codexiabot/codexia/cron/SendReviews.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package dev.iakunin.codexiabot.codexia.cron; | ||
|
||
import dev.iakunin.codexiabot.codexia.entity.CodexiaReview; | ||
import dev.iakunin.codexiabot.codexia.entity.CodexiaReviewNotification; | ||
import dev.iakunin.codexiabot.codexia.repository.CodexiaReviewNotificationRepository; | ||
import dev.iakunin.codexiabot.codexia.repository.CodexiaReviewRepository; | ||
import dev.iakunin.codexiabot.codexia.sdk.CodexiaClient; | ||
import feign.FeignException; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@AllArgsConstructor(onConstructor_={@Autowired}) | ||
public final class SendReviews { | ||
|
||
private static final int REVIEW_ALREADY_EXISTS_STATUS = 404; | ||
|
||
private final CodexiaReviewRepository codexiaReviewRepository; | ||
|
||
private final CodexiaReviewNotificationRepository codexiaReviewNotificationRepository; | ||
|
||
private final CodexiaClient codexiaClient; | ||
|
||
@Scheduled(cron="${app.cron.codexia.send-reviews:-}") | ||
public void run() { | ||
log.info("Running {}", this.getClass().getName()); | ||
|
||
this.codexiaReviewRepository | ||
.findAllWithoutNotifications() | ||
.forEach(this::process); | ||
|
||
log.info("Exiting from {}", this.getClass().getName()); | ||
} | ||
|
||
private void process(CodexiaReview review) { | ||
final CodexiaReviewNotification savedNotification = this.saveNewNotification(review); | ||
final ResponseEntity<String> response; | ||
try { | ||
response = this.codexiaClient.createReview( | ||
review.getCodexiaProject().getExternalId(), | ||
review.getText(), | ||
review.getUuid().toString() | ||
); | ||
} catch (FeignException e) { | ||
log.warn( | ||
"Exception occurred during review creation in Codexia; externalId='{}'", | ||
review.getCodexiaProject().getExternalId(), | ||
e | ||
); | ||
this.saveExceptionalNotification(savedNotification, e); | ||
return; | ||
} | ||
this.saveSuccessfulNotification(savedNotification, response); | ||
} | ||
|
||
private CodexiaReviewNotification saveNewNotification(CodexiaReview review) { | ||
return this.codexiaReviewNotificationRepository.save( | ||
new CodexiaReviewNotification() | ||
.setCodexiaReview(review) | ||
.setStatus(CodexiaReviewNotification.Status.NEW) | ||
); | ||
} | ||
|
||
private void saveSuccessfulNotification( | ||
CodexiaReviewNotification notification, | ||
ResponseEntity<String> response | ||
) { | ||
this.codexiaReviewNotificationRepository.save( | ||
notification | ||
.setStatus(CodexiaReviewNotification.Status.SUCCESS) | ||
.setResponseCode(response.getStatusCodeValue()) | ||
.setResponse(response.toString()) | ||
); | ||
} | ||
|
||
private void saveExceptionalNotification( | ||
CodexiaReviewNotification notification, | ||
FeignException e | ||
) { | ||
this.codexiaReviewNotificationRepository.save( | ||
notification | ||
.setStatus( | ||
// @todo #19 rewrite via custom Feign exceptions | ||
e.status() == REVIEW_ALREADY_EXISTS_STATUS | ||
? CodexiaReviewNotification.Status.SUCCESS | ||
: CodexiaReviewNotification.Status.ERROR | ||
) | ||
.setResponseCode(e.status()) | ||
.setResponse(e.content() != null ? e.contentUTF8() : "") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
c743679
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
6-02060b89
disappeared fromsrc/main/java/dev/iakunin/codexiabot/codexia/CodexiaModule.java
, that's why I closed #19. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.c743679
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
19-703b27a3
discovered insrc/main/java/dev/iakunin/codexiabot/codexia/CodexiaModule.java
and submitted as #39. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.c743679
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
19-ec246873
discovered insrc/main/java/dev/iakunin/codexiabot/codexia/CodexiaModule.java
and submitted as #40. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.c743679
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
19-799ed7e1
discovered insrc/main/java/dev/iakunin/codexiabot/codexia/cron/SendReviews.java
and submitted as #41. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.