Skip to content

Commit

Permalink
GH-120 Add title length validation for Pull Requests (#127)
Browse files Browse the repository at this point in the history
* Add title length validation for Pull Requests

* Add title length validation tests in GitHubReviewUtilTest
  • Loading branch information
vLuckyyy authored Oct 17, 2023
1 parent 3259acb commit 25663fc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public String createReview(Guild guild, String url, JDA jda) {

GitHubPullRequest pullRequest = result.get();
if (!this.checkPullRequestTitle(pullRequest)) {
return "Pull request title is not valid, please use GH-<number> as title";
return "Pull request title is not valid, please use GH-<number> as title and keep it under 100 characters";
}

long messageId = this.createReviewForumPost(guild, pullRequest);
Expand All @@ -59,7 +59,8 @@ public String createReview(Guild guild, String url, JDA jda) {
public boolean checkPullRequestTitle(GitHubPullRequest url) throws IOException {
String pullRequestTitleFromUrl = GitHubReviewUtil.getPullRequestTitleFromUrl(url, this.appConfig.githubToken);

return GitHubReviewUtil.isPullRequestTitleValid(pullRequestTitleFromUrl);
return GitHubReviewUtil.isPullRequestTitleValid(pullRequestTitleFromUrl) &&
GitHubReviewUtil.isTitleLengthValid(pullRequestTitleFromUrl);
}

public long createReviewForumPost(Guild guild, GitHubPullRequest pullRequest) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public final class GitHubReviewUtil {

private static final String GITHUB_PULL_REQUEST_TITLE_CONVENTION = "^(GH)-\\d+ .+$";
private static final int MAX_TITLE_LENGTH = 100;

private static final OkHttpClient HTTP_CLIENT = new OkHttpClient();
private static final Gson GSON = new Gson();
Expand All @@ -29,6 +30,10 @@ public static boolean isPullRequestTitleValid(String title) {
return title.matches(GITHUB_PULL_REQUEST_TITLE_CONVENTION);
}

public static boolean isTitleLengthValid(String title) {
return title.length() <= MAX_TITLE_LENGTH;
}

public static List<String> getReviewers(GitHubPullRequest pullRequest, String githubToken) {
Request request = new Request.Builder()
.url(pullRequest.toApiUrl())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ Stream<DynamicTest> testInvalidPullRequestTitles() {
}));
}

@Test
@DisplayName("Test isTitleLengthValid with a valid title")
void testValidTitleLength() {
String title = "GH-123 This is a valid title";
assertTrue(GitHubReviewUtil.isTitleLengthValid(title), "Valid Title: " + title);
}

@Test
@DisplayName("Test isTitleLengthValid with an invalid title")
void testInvalidTitleLength() {
String title = "GH-123 This is a very long title. It is so long that it should be " +
"considered invalid, but we are testing it all the same to ensure that our title length validation works as expected.";
assertFalse(GitHubReviewUtil.isTitleLengthValid(title), "Invalid Title: " + title);
}

@Test
@DisplayName("Test getReviewers functionality")
void testGetReviewers() throws Exception {
Expand Down Expand Up @@ -187,4 +202,4 @@ private void setMockResponse(String jsonResponse) {
.setBody(jsonResponse)
.addHeader("Content-Type", "application/json"));
}
}
}

0 comments on commit 25663fc

Please sign in to comment.