Skip to content

Conversation

@SquidXTV
Copy link
Member

About

closes #1142

Implements the functionality for PR notifications
link-gh-project: links a channel to GitHub repositories to announce new pull request
unlink-gh-project: unlinks a previous pull request notification configuration

The PullRequestNotificationRoutine handles pulling the new updates from repositories and sending a notification.

Database

This PR creates a new database table to store the notification configuration:

CREATE TABLE pr_notifications
(
    id INTEGER   NOT NULL PRIMARY KEY AUTOINCREMENT,
    channel_id BIGINT NOT NULL,
    repository_owner TEXT NOT NULL,
    repository_name TEXT NOT NULL
)

@SquidXTV SquidXTV requested a review from a team as a code owner July 31, 2024 18:12
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
13.8% Duplication on New Code (required ≤ 3%)

See analysis details on SonarCloud

try {
github = new GitHubBuilder().withOAuthToken(githubApiKey).build();
} catch (IOException e) {
logger.error("Failed to initialize GitHub API wrapper.", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message, since duplicate should be stored in a String

github = new GitHubBuilder().withOAuthToken(githubApiKey).build();
} catch (IOException e) {
logger.error("Failed to initialize GitHub API wrapper.", e);
event.reply("Failed to initialize GitHub API wrapper.").setEphemeral(true).queue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this isn't a meaningful error message to the user. Instead opt for something user friendly e.g. "An internal error occurred while linking your repository, please try again later"


try {
if (!isRepositoryAccessible(github, repositoryOwner, repositoryName)) {
event.reply("Repository is not publicly available.").setEphemeral(true).queue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opt to provide the owner and name in this error message. It could have been they made a typo. Maybe include a link to the docs on how they can set the visibility too.

}
} catch (IOException e) {
logger.error("Failed to check if GitHub repository is available.", e);
event.reply("Failed to link repository.").setEphemeral(true).queue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, opt for the user friendly message. Make it a constant above since it can be reused.


try {
saveNotificationToDatabase(channelId, repositoryOwner, repositoryName);
event.reply("Successfully linked repository.").setEphemeral(true).queue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this response a pretty embed for everyone in the project thread to see.

}
}

lastExecution = new Date();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.currentTimeMillis() please to avoid creating a new Date object each time


List<GHPullRequest> pullRequests = repository.getPullRequests(GHIssueState.OPEN);
for (GHPullRequest pr : pullRequests) {
if (pr.getCreatedAt().after(lastExecution)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pr.getCreatedAt() > lastExecution

logger.info("Failed to find channel {} to send pull request notification.", channelId);
return;
}
channel.sendMessage("New pull request from " + pr.getUser().getLogin() + ".").queue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a pretty embed. We're missing key details here such as the PR name, description, number, creation date and URL.


GitHub github;
try {
github = new GitHubBuilder().withOAuthToken(githubApiKey).build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this into the constructor and log. So we can fail fast. Or even better, do the init inside Features.java and only then bind the routine/commands if we can successfully create a GitHub object.

event.reply("Successfully linked repository.").setEphemeral(true).queue();
} catch (DatabaseException e) {
logger.error("Failed to save pull request notification to database.", e);
event.reply("Failed to link repository.").setEphemeral(true).queue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better error message please

@SquidXTV SquidXTV self-assigned this Jul 1, 2025
@SquidXTV SquidXTV force-pushed the projects-pr-notifications branch from 20b0898 to 1e1717b Compare July 1, 2025 13:21
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jul 1, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
13.8% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@SquidXTV
Copy link
Member Author

SquidXTV commented Jul 3, 2025

Closing this PR.
GitHub already supports native Discord webhook notifications through the GitHub repo settings, which makes this kinda redundant. The feature also isn't critical and likely wouldn't be used much in practice (probably).

The webhook alternative approach involves sharing the sensitive webhook link with a third party. My suggestion would be sharing them with very active #projects posts and trusted users if needed. If anything goes wrong, we can just disable the webhook.

Quick note:
When sharing webhook links in forum-based threads, you need to append a thread_id query parameter to the webhook link: https://discord.com/developers/docs/resources/webhook#execute-webhook

@SquidXTV SquidXTV closed this Jul 3, 2025
@SquidXTV SquidXTV deleted the projects-pr-notifications branch July 3, 2025 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sending PR notifications to project channels

4 participants