Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dep updates; StalledDiscussionFlow progress #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bot.enable.pr-review=false
bot.enable.state-machine=false
bot.enable.create-draft=true

BOT_STALLED_DISCUSSION_FLOW_POLL_DURATION=86400s

## Stuff required by the github app framework

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>2.7.5.Final</quarkus.platform.version>
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
<quarkus-github-app.version>1.8.4</quarkus-github-app.version>
<quarkus.platform.version>2.16.6.Final</quarkus.platform.version>
<surefire-plugin.version>3.0.0</surefire-plugin.version>
<quarkus-github-app.version>1.16.0</quarkus-github-app.version>

</properties>
<dependencyManagement>
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/bf2/arch/bot/ArchBotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ public class ArchBotConfig {
/**
* Github login name of the bot itself.
*/
@JsonDeserialize
String botUserLogin;

/**
* The time, in minutes, to wait between checking for stalled discussions.
*/
@JsonDeserialize
long stalledDiscussionPollTimeMins;

/**
Expand All @@ -31,7 +33,8 @@ public class ArchBotConfig {
/**
* The URL at which the site is published.
*/
String publishedUrl = "https://architecture.appservices.tech";
@JsonDeserialize
String publishedUrl;

@Override
public String toString() {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/bf2/arch/bot/CreateDraftRecordFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.quarkiverse.githubapp.ConfigFile;
import io.quarkiverse.githubapp.event.IssueComment;
import org.bf2.arch.bot.model.record.RecordPage;
import org.bf2.arch.bot.model.record.RecordId;
import org.bf2.arch.bot.model.record.RecordPage;
import org.bf2.arch.bot.model.record.RecordType;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kohsuke.github.GHBranch;
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHContent;
Expand All @@ -32,6 +28,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.quarkiverse.githubapp.ConfigFile;
import io.quarkiverse.githubapp.event.IssueComment;
import io.smallrye.common.constraint.NotNull;
import io.smallrye.common.constraint.Nullable;

/**
* Flow for creating a Draft record (AP, ADR, PADR), and possibly superseding an existing record.
*
Expand Down
72 changes: 39 additions & 33 deletions src/main/java/org/bf2/arch/bot/StalledDiscussionFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
*/
package org.bf2.arch.bot;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Optional;
import java.util.Set;

import io.quarkiverse.githubapp.runtime.github.GitHubService;
import io.quarkus.scheduler.Scheduled;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.kohsuke.github.GHDirection;
import org.kohsuke.github.GHIssue;
Expand All @@ -34,6 +34,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.quarkiverse.githubapp.ConfigFile;
import io.quarkiverse.githubapp.GitHubConfigFileProvider;
import io.quarkiverse.githubapp.runtime.github.GitHubService;
import io.quarkus.scheduler.Scheduled;

@ApplicationScoped
public class StalledDiscussionFlow {

Expand All @@ -52,23 +57,27 @@ public class StalledDiscussionFlow {
private Date lastRan = new Date(0);

GitHub client;
//ArchBotConfig config;
ArchBotConfig config;

@Inject
GitHubService service;

@Inject
void init(GitHubService service) {
void init(GitHubConfigFileProvider configFileProvider) throws IOException {
if (!enabled) {
LOG.debug("Ignoring init: disabled due to {}=false", ENABLE);
} else if (installationId != null) {
// TODO parameterise this installationId
client = service.getInstallationClient(installationId);
// TODO load the config

Optional<ArchBotConfig> oconfig = configFileProvider.fetchConfigFile(client.getRepository(repositoryPath),
Util.CONFIG_REPO_PATH, ConfigFile.Source.DEFAULT, ArchBotConfig.class);
config = oconfig.orElseThrow();
} else {
throw new RuntimeException("installation id is requied");
}
}



/**
* When
* every N hours
Expand All @@ -89,7 +98,7 @@ void init(GitHubService service) {
* If the PR has been opened for > Y hours then "stalled-discussion"
*/
// TODO similar method as this, but for OVERDUE
@Scheduled(every="60s")
@Scheduled(every = "{BOT_STALLED_DISCUSSION_FLOW_POLL_DURATION}")
public void checkForStalledDiscussions() throws IOException {
if (!enabled) {
LOG.debug("Ignoring scheduled trigger: disabled due to {}=false", ENABLE);
Expand All @@ -98,7 +107,9 @@ public void checkForStalledDiscussions() throws IOException {
long now = System.currentTimeMillis();
long thresh = now - 24*40*60*1000L;
LOG.info("Checking for stalled discussions");
// TODO need a parameter for this installation id

LOG.debug("Updating installation client to get new token (expires every 1h)");
client = service.getInstallationClient(installationId);

var results = client.searchIssues()
.isOpen()
Expand All @@ -112,13 +123,7 @@ public void checkForStalledDiscussions() throws IOException {
.order(GHDirection.ASC)
.list();
LOG.info("Top-level query found {} PRs", results.getTotalCount());
int processed = 0;
for (GHIssue issue : results) {
// if () {
// lastRan = new Date();
// break;
// }
processed++;
try {
GHPullRequest pullRequest = Util.findPullRequest(issue);
if (pullRequest == null) {
Expand All @@ -132,15 +137,16 @@ public void checkForStalledDiscussions() throws IOException {
// https://docs.github.com/en/rest/pulls/comments#list-review-comments-in-a-repository
// but the client doesn't expose them
Date lastCommentDate;
LOG.info("COMMENTS COUNT: {}", pullRequest.listReviewComments().toList().size());
var mostRecent = pullRequest.listReviewComments().toList().stream()
// .filter(pr -> {
// try {
// return !Util.isThisBot(config, pr.getUser());
// } catch (IOException e) {
// return true;
// }
// })
.map(comment -> {
.filter(pr -> {
try {
return !Util.isThisBot(config, pr.getUser());
} catch (IOException e) {
return true;
}
})
.map(comment -> {
try {
LOG.info("Comment: {}", comment.getBody());
LOG.info("User: {}", comment.getUser().getLogin());
Expand All @@ -151,16 +157,16 @@ public void checkForStalledDiscussions() throws IOException {
}).max(Date::compareTo);

lastCommentDate = mostRecent.orElseGet(() -> {
try {
return pullRequest.getUpdatedAt();
} catch (IOException e) {
try {
return pullRequest.getCreatedAt();
} catch (IOException ex) {
return new Date(0);
return pullRequest.getUpdatedAt();
} catch (IOException e) {
try {
return pullRequest.getCreatedAt();
} catch (IOException ex) {
return new Date(0);
}
}
}
});
});
LOG.info("PR#{}: Last comment time {}", pullRequest.getNumber(), lastCommentDate);

if (lastCommentDate.getTime() < thresh) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/bf2/arch/bot/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.jetbrains.annotations.NotNull;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHLabel;
import org.kohsuke.github.GHPullRequest;
Expand All @@ -30,6 +29,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.smallrye.common.constraint.NotNull;

/**
* Common helper functions
*/
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
quarkus.log.level=DEBUG
quarkus.log.level=DEBUG
bot.enable.stalled-discussion=true
bot.enable.create-draft=true
BOT_STALLED_DISCUSSION_FLOW_POLL_DURATION=86400s
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public void renderTemplateTest() throws IOException {
"- \"me\"\n" +
"tags:\n" +
"- \"bar\"\n" +
"applies_padrs: null\n" +
"applies_patterns: null\n" +
"---\n" +
"Hello, world\n", rendered.toContentString());
}
Expand All @@ -92,6 +94,8 @@ public void renderSupersededTemplateTest() throws IOException {
"- \"me\"\n" +
"tags:\n" +
"- \"bar\"\n" +
"applies_padrs: null\n" +
"applies_patterns: null\n" +
"---\n" +
"Hello, world\n", rendered.toContentString());
}
Expand Down Expand Up @@ -137,4 +141,4 @@ public void testGetPage() throws IOException {

// TODO test supersededContent

}
}
3 changes: 2 additions & 1 deletion src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ FqD6xNtjmuaS5enErcCAMbZtzA7TNzvGaVO+xB/GfQ2QHS8/mrTesvQsTUZwC+ji\
E0/FAoGATJvuAfgy9uiKR7za7MigYVacE0u4aD1sF7v6D4AFqBOGquPQQhePSdz9\
G/UUwySoo+AQ+rd2EPhyexjqXBhRGe+EDGFVFivaQzTT8/5bt/VddbTcw2IpmXYj\
LW6V8BbcP5MRhd2JQSRh16nWwSQJ2BdpUZFwayEEQ6UcrMfqvA0=\
-----END RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
BOT_STALLED_DISCUSSION_FLOW_POLL_DURATION=86400s