Skip to content

Commit

Permalink
Enable issue notifications other repos (#1321)
Browse files Browse the repository at this point in the history
  • Loading branch information
coeuvre authored Feb 2, 2022
1 parent de7459d commit a47ce5a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 56 deletions.
10 changes: 6 additions & 4 deletions dashboard/server/database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,12 @@ CREATE UNIQUE INDEX ON github_team_table_header (owner, repo, table_id, id, seq)

CREATE TABLE github_repo
(
owner TEXT,
repo TEXT,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
owner TEXT,
repo TEXT,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
action_owner TEXT DEFAULT NULL,
is_team_label_enabled BOOL DEFAULT FALSE,
PRIMARY KEY (owner, repo)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import build.bazel.dashboard.github.issue.GithubIssue.User;
import build.bazel.dashboard.github.issuestatus.GithubIssueStatus.GithubIssueStatusBuilder;
import build.bazel.dashboard.github.issuestatus.GithubIssueStatus.Status;
import build.bazel.dashboard.github.repo.GithubRepo;
import build.bazel.dashboard.github.repo.GithubRepoService;
import build.bazel.dashboard.github.team.GithubTeam;
import build.bazel.dashboard.github.team.GithubTeamService;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -26,6 +29,7 @@
@RequiredArgsConstructor
public class GithubIssueStatusService {
private final ObjectMapper objectMapper;
private final GithubRepoService githubRepoService;
private final GithubIssueStatusRepo githubIssueStatusRepo;
private final GithubTeamService githubTeamService;

Expand All @@ -37,15 +41,11 @@ public Maybe<GithubIssueStatus> check(GithubIssue issue, Instant now) {
String owner = issue.getOwner();
String repo = issue.getRepo();

if (!(owner.equals("bazelbuild") && repo.equals("bazel"))) {
return Maybe.empty();
}

return githubIssueStatusRepo
return githubRepoService.findOne(owner, repo).flatMap(githubRepo -> githubIssueStatusRepo
.findOne(owner, repo, issue.getIssueNumber())
.flatMapSingle(status -> buildStatus(status, issue, now))
.switchIfEmpty(buildStatus(null, issue, now))
.flatMapMaybe(status -> githubIssueStatusRepo.save(status).andThen(Maybe.just(status)));
.flatMapSingle(status -> buildStatus(githubRepo, status, issue, now))
.switchIfEmpty(buildStatus(githubRepo, null, issue, now))
.flatMapMaybe(status -> githubIssueStatusRepo.save(status).andThen(Maybe.just(status))));
}

public Completable markDeleted(String owner, String repo, int issueNumber) {
Expand All @@ -59,11 +59,12 @@ public Completable markDeleted(String owner, String repo, int issueNumber) {
}

private Single<GithubIssueStatus> buildStatus(
GithubRepo repo,
@Nullable GithubIssueStatus oldStatus, GithubIssue issue, Instant now) {
return Single.fromCallable(() -> issue.parseData(objectMapper))
.flatMap(
data -> {
Status newStatus = newStatus(data);
Status newStatus = newStatus(repo, data);
Instant lastNotifiedAt = null;

if (oldStatus != null) {
Expand Down Expand Up @@ -91,14 +92,14 @@ private Single<GithubIssueStatus> buildStatus(
.nextNotifyAt(nextNotifyAt)
.checkedAt(now);

return findActionOwner(issue, data, newStatus)
return findActionOwner(repo, issue, data, newStatus)
.map(builder::actionOwner)
.defaultIfEmpty(builder)
.map(GithubIssueStatusBuilder::build);
});
}

static Status newStatus(GithubIssue.Data data) {
static Status newStatus(GithubRepo repo, GithubIssue.Data data) {
if (data.getState().equals("closed")) {
return Status.CLOSED;
}
Expand All @@ -109,7 +110,7 @@ static Status newStatus(GithubIssue.Data data) {
return Status.MORE_DATA_NEEDED;
}

if (hasTeamLabel(labels)) {
if (!repo.isTeamLabelEnabled() || hasTeamLabel(labels)) {
if (hasPriorityLabel(labels)) {
return Status.TRIAGED;
} else {
Expand Down Expand Up @@ -151,7 +152,7 @@ static Status newStatus(GithubIssue.Data data) {
return null;
}

private Maybe<String> findActionOwner(GithubIssue issue, GithubIssue.Data data, Status status) {
private Maybe<String> findActionOwner(GithubRepo repo, GithubIssue issue, GithubIssue.Data data, Status status) {
switch (status) {
case TO_BE_REVIEWED:
return Maybe.empty();
Expand All @@ -171,7 +172,8 @@ private Maybe<String> findActionOwner(GithubIssue issue, GithubIssue.Data data,
labels.stream().anyMatch(label -> label.getName().equals(team.getLabel()))
&& !team.getTeamOwner().isBlank())
.firstElement()
.map(GithubTeam::getTeamOwner);
.map(GithubTeam::getTeamOwner)
.switchIfEmpty(Maybe.fromOptional(Optional.ofNullable(repo.getActionOwner())));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ public void startNotifyIssueStatus() {

Single<String> buildTriageTeamNotificationBody() {
ListParams params = new ListParams();
params.setOwner("bazelbuild");
params.setRepo("bazel");
params.setStatus(GithubIssueStatus.Status.TO_BE_REVIEWED);
return githubIssueListService
.find(params)
Expand Down Expand Up @@ -255,12 +253,16 @@ private Maybe<String> buildIssueListItem(GithubIssueList.Item issue) {
body.append("<tr style=\"vertical-align: baseline\">");

body.append("<td>");
String repo = "";
if (!(issue.getOwner().equals("bazelbuild") && issue.getRepo().equals("bazel"))) {
repo = issue.getRepo();
}
appendLink(
body,
String.format(
"/~https://github.com/%s/%s/issues/%s",
issue.getOwner(), issue.getRepo(), issue.getIssueNumber()),
"#" + issue.getIssueNumber());
repo + "#" + issue.getIssueNumber());
body.append("</td>");

body.append("<td style=\"white-space: nowrap;\">");
Expand Down Expand Up @@ -352,8 +354,6 @@ private Completable notifyUsers() {

Single<String> buildNeedTriageMessage(GithubUser user) {
ListParams params = new ListParams();
params.setOwner("bazelbuild");
params.setRepo("bazel");
params.setStatus(GithubIssueStatus.Status.REVIEWED);
params.setActionOwner(user.getUsername());
return githubIssueListService
Expand All @@ -374,8 +374,6 @@ Single<String> buildNeedTriageMessage(GithubUser user) {

Single<String> buildFixP0BugsMessage(GithubUser user) {
ListParams params = new ListParams();
params.setOwner("bazelbuild");
params.setRepo("bazel");
params.setStatus(GithubIssueStatus.Status.TRIAGED);
params.setLabels(ImmutableList.of("P0", "type: bug"));
params.setActionOwner(user.getUsername());
Expand All @@ -398,8 +396,6 @@ Single<String> buildFixP0BugsMessage(GithubUser user) {

Single<String> buildFixP1BugsMessage(GithubUser user) {
ListParams params = new ListParams();
params.setOwner("bazelbuild");
params.setRepo("bazel");
params.setStatus(GithubIssueStatus.Status.TRIAGED);
params.setLabels(ImmutableList.of("P1", "type: bug"));
params.setActionOwner(user.getUsername());
Expand All @@ -422,8 +418,6 @@ Single<String> buildFixP1BugsMessage(GithubUser user) {

Single<String> buildFixP2BugsMessage(GithubUser user) {
ListParams params = new ListParams();
params.setOwner("bazelbuild");
params.setRepo("bazel");
params.setStatus(GithubIssueStatus.Status.TRIAGED);
params.setLabels(ImmutableList.of("P2", "type: bug"));
params.setActionOwner(user.getUsername());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package build.bazel.dashboard.github.repo;

import java.time.Instant;
import javax.annotation.Nullable;
import lombok.Builder;
import lombok.Value;

Expand All @@ -8,4 +10,8 @@
public class GithubRepo {
String owner;
String repo;
Instant createdAt;
Instant updatedAt;
@Nullable String actionOwner;
boolean isTeamLabelEnabled;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
package build.bazel.dashboard.github.repo;

import io.reactivex.rxjava3.core.Flowable;
import lombok.Builder;
import lombok.Value;

import java.time.Instant;
import io.reactivex.rxjava3.core.Maybe;

public interface GithubRepoRepo {
@Builder
@Value
class GithubRepoData {
String owner;
String repo;
Instant createdAt;
Instant updatedAt;
}
Maybe<GithubRepo> findOne(String owner, String repo);

Flowable<GithubRepoData> findAll();
Flowable<GithubRepo> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package build.bazel.dashboard.github.repo;

import static java.util.Objects.requireNonNull;

import io.r2dbc.spi.Row;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import java.time.Instant;
import lombok.RequiredArgsConstructor;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Repository;
import reactor.adapter.rxjava.RxJava3Adapter;
import reactor.core.publisher.Flux;

import java.time.Instant;
import reactor.core.publisher.Mono;

@Repository
@RequiredArgsConstructor
Expand All @@ -16,17 +20,39 @@ public class GithubRepoRepoPg implements GithubRepoRepo {
private final DatabaseClient databaseClient;

@Override
public Flowable<GithubRepoData> findAll() {
Flux<GithubRepoData> query =
public Maybe<GithubRepo> findOne(String owner, String repo) {
Mono<GithubRepo> query =
databaseClient
.sql(
"SELECT owner, repo, created_at, updated_at, action_owner, is_team_label_enabled"
+ " FROM github_repo WHERE owner = :owner AND repo = :repo")
.bind("owner", owner)
.bind("repo", repo)
.map(this::toGithubRepo)
.one();
return RxJava3Adapter.monoToMaybe(query);
}

@Override
public Flowable<GithubRepo> findAll() {
Flux<GithubRepo> query =
databaseClient
.sql("SELECT owner, repo, created_at, updated_at FROM github_repo")
.map(row -> GithubRepoData.builder()
.owner(row.get("owner", String.class))
.repo(row.get("repo", String.class))
.createdAt(row.get("created_at", Instant.class))
.updatedAt(row.get("updated_at", Instant.class))
.build())
.sql(
"SELECT owner, repo, created_at, updated_at, action_owner, is_team_label_enabled"
+ " FROM github_repo")
.map(this::toGithubRepo)
.all();
return RxJava3Adapter.fluxToFlowable(query);
}

private GithubRepo toGithubRepo(Row row) {
return GithubRepo.builder()
.owner(requireNonNull(row.get("owner", String.class)))
.repo(requireNonNull(row.get("repo", String.class)))
.createdAt(requireNonNull(row.get("created_at", Instant.class)))
.updatedAt(requireNonNull(row.get("updated_at", Instant.class)))
.actionOwner(row.get("action_owner", String.class))
.isTeamLabelEnabled(requireNonNull(row.get("is_team_label_enabled", Boolean.class)))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package build.bazel.dashboard.github.repo;

import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -9,9 +10,11 @@
public class GithubRepoService {
private final GithubRepoRepo githubRepoRepo;

public Maybe<GithubRepo> findOne(String owner, String repo) {
return githubRepoRepo.findOne(owner,repo);
}

public Flowable<GithubRepo> findAll() {
return githubRepoRepo
.findAll()
.map(data -> GithubRepo.builder().owner(data.getOwner()).repo(data.getRepo()).build());
return githubRepoRepo.findAll();
}
}

0 comments on commit a47ce5a

Please sign in to comment.