-
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.
Browse files
Browse the repository at this point in the history
* #6 - `Stars-up` bot: work in progress * #6 - Changing puzzles format according to pdd * #6 - Integration tests for StarsUp bot * #6 - Another more integration tests for StarsUp bot * #6 - StarsUp big methods decomposition
- Loading branch information
Showing
65 changed files
with
1,811 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,7 @@ public interface Bot { | |
enum Type { | ||
FOUND_ON_HACKERNEWS, | ||
FOUND_ON_REDDIT, | ||
STARS_UP, | ||
FORKS_UP, | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package dev.iakunin.codexiabot.bot; | ||
|
||
import dev.iakunin.codexiabot.bot.entity.StarsUpResult; | ||
import dev.iakunin.codexiabot.bot.repository.StarsUpResultRepository; | ||
import dev.iakunin.codexiabot.codexia.CodexiaModule; | ||
import dev.iakunin.codexiabot.codexia.entity.CodexiaProject; | ||
import dev.iakunin.codexiabot.codexia.entity.CodexiaReview; | ||
import dev.iakunin.codexiabot.github.GithubModule; | ||
import dev.iakunin.codexiabot.github.entity.GithubRepo; | ||
import dev.iakunin.codexiabot.github.entity.GithubRepoStat; | ||
import dev.iakunin.codexiabot.github.entity.GithubRepoStat.GithubApi; | ||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
import java.util.Deque; | ||
import java.util.stream.Collectors; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.javatuples.Pair; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Component | ||
@Slf4j | ||
@AllArgsConstructor(onConstructor_={@Autowired}) | ||
public class StarsUp { | ||
|
||
private static final Bot.Type BOT_TYPE = Bot.Type.STARS_UP; | ||
|
||
private final GithubModule githubModule; | ||
|
||
private final CodexiaModule codexiaModule; | ||
|
||
private final StarsUpResultRepository starsUpResultRepository; | ||
|
||
@Scheduled(cron="${app.cron.bot.stars-up:-}") | ||
public void run() { | ||
log.info("Running {}", this.getClass().getName()); | ||
|
||
this.githubModule.findAllInCodexia() | ||
.stream() | ||
.map( | ||
repo -> Pair.with(repo, this.getLastProcessedStatId(repo)) | ||
) | ||
.map( | ||
pair -> this.githubModule.findAllGithubApiStat( | ||
pair.getValue0(), | ||
pair.getValue1() | ||
) | ||
) | ||
.filter(statList -> statList.size() >= 2) | ||
.filter( | ||
statList -> this.shouldReviewBeSubmitted( | ||
(GithubApi) statList.getFirst().getStat(), | ||
(GithubApi) statList.getLast().getStat() | ||
) | ||
) | ||
.forEach(this::processStatList); | ||
|
||
log.info("Exiting from {}", this.getClass().getName()); | ||
} | ||
|
||
private Long getLastProcessedStatId(GithubRepo repo) { | ||
return this.starsUpResultRepository | ||
.findFirstByGithubRepoOrderByIdDesc(repo) | ||
.map( | ||
starsUpResult -> starsUpResult.getGithubRepoStat().getId() | ||
) | ||
.orElse(0L); | ||
} | ||
|
||
// @todo #6 add test case with transaction rollback | ||
@Transactional | ||
protected void processStatList(Deque<GithubRepoStat> deque) { | ||
final CodexiaReview review = this.createReview(deque.getFirst(), deque.getLast()); | ||
|
||
this.starsUpResultRepository.save( | ||
new StarsUpResult() | ||
.setGithubRepo(deque.getLast().getGithubRepo()) | ||
.setGithubRepoStat(deque.getLast()) | ||
); | ||
this.codexiaModule.sendReview(review); | ||
this.codexiaModule.sendMeta( | ||
review.getCodexiaProject(), | ||
"stars-count", | ||
this.codexiaModule | ||
.findAllReviews(review.getCodexiaProject(), review.getAuthor()) | ||
.stream() | ||
.map(CodexiaReview::getReason) | ||
.collect(Collectors.joining(",")) | ||
); | ||
} | ||
|
||
private CodexiaReview createReview(GithubRepoStat first, GithubRepoStat last) { | ||
final GithubApi firstStat = (GithubApi) first.getStat(); | ||
final GithubApi lastStat = (GithubApi) last.getStat(); | ||
|
||
return new CodexiaReview() | ||
.setText( | ||
String.format( | ||
"The repo gained %d stars since %s (was: %d stars, now: %d stars). " + | ||
"See the stars history [here](https://star-history.t9t.io/#%s).", | ||
lastStat.getStars() - firstStat.getStars(), | ||
ZonedDateTime.of(first.getCreatedAt(), ZoneOffset.UTC).toString(), | ||
firstStat.getStars(), | ||
lastStat.getStars(), | ||
first.getGithubRepo().getFullName() | ||
) | ||
) | ||
.setAuthor(BOT_TYPE.name()) | ||
.setReason(String.valueOf(lastStat.getStars())) | ||
.setCodexiaProject(this.getCodexiaProject(last)); | ||
} | ||
|
||
private CodexiaProject getCodexiaProject(GithubRepoStat last) { | ||
return this.codexiaModule | ||
.findCodexiaProject(last.getGithubRepo()) | ||
.orElseThrow( | ||
() -> new RuntimeException( | ||
String.format( | ||
"Unable to find CodexiaProject for githubRepoId='%s'", | ||
last.getGithubRepo().getId() | ||
) | ||
) | ||
); | ||
} | ||
|
||
private boolean shouldReviewBeSubmitted(GithubApi first, GithubApi last) { | ||
final int increase = last.getStars() - first.getStars(); | ||
|
||
if (increase < 10) { | ||
return false; | ||
} | ||
|
||
return increase >= (first.getStars() * 0.05); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/dev/iakunin/codexiabot/bot/entity/StarsUpResult.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,21 @@ | ||
package dev.iakunin.codexiabot.bot.entity; | ||
|
||
import dev.iakunin.codexiabot.common.entity.AbstractEntity; | ||
import dev.iakunin.codexiabot.github.entity.GithubRepo; | ||
import dev.iakunin.codexiabot.github.entity.GithubRepoStat; | ||
import javax.persistence.Entity; | ||
import javax.persistence.ManyToOne; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
@Entity | ||
@Data | ||
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true) | ||
public final class StarsUpResult extends AbstractEntity { | ||
|
||
@ManyToOne | ||
private GithubRepo githubRepo; | ||
|
||
@ManyToOne | ||
private GithubRepoStat githubRepoStat; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/dev/iakunin/codexiabot/bot/repository/StarsUpResultRepository.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,11 @@ | ||
package dev.iakunin.codexiabot.bot.repository; | ||
|
||
import dev.iakunin.codexiabot.bot.entity.StarsUpResult; | ||
import dev.iakunin.codexiabot.github.entity.GithubRepo; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface StarsUpResultRepository extends JpaRepository<StarsUpResult, Long> { | ||
|
||
Optional<StarsUpResult> findFirstByGithubRepoOrderByIdDesc(GithubRepo repo); | ||
} |
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
Oops, something went wrong.
12b0267
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
discovered insrc/main/java/dev/iakunin/codexiabot/codexia/CodexiaModule.java
and submitted as #19. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.12b0267
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-ba4d7ce3
discovered insrc/main/java/dev/iakunin/codexiabot/codexia/sdk/CodexiaClient.java
and submitted as #20. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.12b0267
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-5af20361
discovered insrc/main/java/dev/iakunin/codexiabot/codexia/sdk/CodexiaClient.java
and submitted as #21. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.12b0267
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-af8b3d4f
discovered insrc/main/java/dev/iakunin/codexiabot/common/entity/AbstractEntity.java
and submitted as #22. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.12b0267
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-57333c2d
discovered insrc/main/java/dev/iakunin/codexiabot/bot/FoundOnReddit.java
and submitted as #23. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.12b0267
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-cae02d85
discovered insrc/main/java/dev/iakunin/codexiabot/bot/FoundOnHackernews.java
and submitted as #24. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.12b0267
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-4d9a1ad5
discovered insrc/main/java/dev/iakunin/codexiabot/bot/StarsUp.java
and submitted as #25. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.12b0267
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-facd1e77
discovered insrc/test/java/dev/iakunin/codexiabot/codexia/cron/ProjectsHealthCheckIntegrationTest.java
and submitted as #26. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.12b0267
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-3551727a
discovered insrc/test/java/dev/iakunin/codexiabot/dbunit/CustomPostgresqlDataTypeFactory.java
and submitted as #27. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.