Skip to content

Commit

Permalink
Merge pull request #10 from gsmet/update-body-properly
Browse files Browse the repository at this point in the history
Make sure we carry the updated body everywhere
  • Loading branch information
gsmet committed Nov 24, 2023
2 parents 95f9e3b + 1dec74f commit cdcbd99
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.quarkiverse.githubapp.event.IssueComment;
import io.quarkus.bot.release.util.Issues;
import io.quarkus.bot.release.util.Outputs;
import io.quarkus.bot.release.util.UpdatedIssueBody;

public class GetWorkflowRunIdAction {

Expand All @@ -17,7 +18,9 @@ public class GetWorkflowRunIdAction {

@Action("get-workflow-run-id")
void getWorkflowRunId(Commands commands, @IssueComment.Created GHEventPayload.IssueComment issueCommentPayload) {
UpdatedIssueBody updatedIssueBody = new UpdatedIssueBody(issueCommentPayload.getIssue().getBody());

commands.setOutput(Outputs.WORKFLOW_RUN_ID,
issues.extractReleaseStatus(issueCommentPayload.getIssue().getBody()).getWorkflowRunId().toString());
issues.extractReleaseStatus(updatedIssueBody).getWorkflowRunId().toString());
}
}
119 changes: 68 additions & 51 deletions src/main/java/io/quarkus/bot/release/ReleaseAction.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.quarkus.bot.release.ReleaseInformation;
import io.quarkus.bot.release.ReleaseStatus;
import io.quarkus.bot.release.util.Command;
import io.quarkus.bot.release.util.UpdatedIssueBody;

@Singleton
@Unremovable
Expand All @@ -24,12 +25,14 @@ public boolean shouldPause(ReleaseInformation releaseInformation, ReleaseStatus
}

@Override
public boolean shouldContinue(ReleaseInformation releaseInformation, ReleaseStatus releaseStatus, GHIssueComment issueComment) {
public boolean shouldContinue(ReleaseInformation releaseInformation, ReleaseStatus releaseStatus,
GHIssueComment issueComment) {
return Command.YES.matches(issueComment.getBody());
}

@Override
public int run(Context context, Commands commands, ReleaseInformation releaseInformation, GHIssue issue) throws IOException, InterruptedException {
public int run(Context context, Commands commands, ReleaseInformation releaseInformation, GHIssue issue,
UpdatedIssueBody updatedIssueBody) throws IOException, InterruptedException {
issue.comment(":white_check_mark: Core release is approved, proceeding...");
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import io.quarkiverse.githubaction.Context;
import io.quarkus.arc.Unremovable;
import io.quarkus.bot.release.ReleaseInformation;
import io.quarkus.bot.release.util.UpdatedIssueBody;

@Singleton
@Unremovable
public class CoreReleasePrepare implements StepHandler {

@Override
public int run(Context context, Commands commands, ReleaseInformation releaseInformation, GHIssue issue) throws IOException, InterruptedException {
public int run(Context context, Commands commands, ReleaseInformation releaseInformation, GHIssue issue,
UpdatedIssueBody updatedIssueBody) throws IOException, InterruptedException {
throw new IllegalStateException("Testing error handling...");
}
}
8 changes: 5 additions & 3 deletions src/main/java/io/quarkus/bot/release/step/Prerequisites.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.quarkus.bot.release.util.Issues;
import io.quarkus.bot.release.util.Outputs;
import io.quarkus.bot.release.util.Processes;
import io.quarkus.bot.release.util.UpdatedIssueBody;

@Singleton
@Unremovable
Expand All @@ -31,7 +32,8 @@ public class Prerequisites implements StepHandler {
Processes processes;

@Override
public int run(Context context, Commands commands, ReleaseInformation releaseInformation, GHIssue issue) throws IOException, InterruptedException {
public int run(Context context, Commands commands, ReleaseInformation releaseInformation, GHIssue issue,
UpdatedIssueBody updatedIssueBody) throws IOException, InterruptedException {
List<String> command = new ArrayList<String>();
command.add("./prerequisites.java");
command.add("--branch=" + releaseInformation.getBranch());
Expand All @@ -48,7 +50,7 @@ public int run(Context context, Commands commands, ReleaseInformation releaseInf
}

releaseInformation.setVersion(Files.readString(Path.of("work", "newVersion")).trim());
issues.appendReleaseInformation(issue.getBody(), releaseInformation);
issues.appendReleaseInformation(updatedIssueBody, releaseInformation);

StringBuilder comment = new StringBuilder();
comment.append("We are going to release the following release:\n\n");
Expand All @@ -57,7 +59,7 @@ public int run(Context context, Commands commands, ReleaseInformation releaseInf
comment.append("- This is a `maintenance` release.\n");
}
if (Files.exists(Path.of("work", "preview"))) {
comment.append("- This is a preview release (e.g.`Alpha`, `Beta`, `CR`).\n");
comment.append("- This is a preview release (e.g. `Alpha`, `Beta`, `CR`).\n");
}
comment.append(
"\nPlease add a `" + Command.YES.getFullCommand() + "` comment if you want to pursue with the release.\n");
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/io/quarkus/bot/release/step/StepHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
import io.quarkiverse.githubaction.Context;
import io.quarkus.bot.release.ReleaseInformation;
import io.quarkus.bot.release.ReleaseStatus;
import io.quarkus.bot.release.util.UpdatedIssueBody;

public interface StepHandler {

int run(Context context, Commands commands, ReleaseInformation releaseInformation, GHIssue issue) throws IOException, InterruptedException;
int run(Context context, Commands commands, ReleaseInformation releaseInformation, GHIssue issue,
UpdatedIssueBody updatedIssueBody) throws IOException, InterruptedException;

default boolean shouldPause(ReleaseInformation releaseInformation, ReleaseStatus releaseStatus) {
return false;
}

default boolean shouldContinue(ReleaseInformation releaseInformation, ReleaseStatus releaseStatus, GHIssueComment issueComment) {
default boolean shouldContinue(ReleaseInformation releaseInformation, ReleaseStatus releaseStatus,
GHIssueComment issueComment) {
return false;
}
}
34 changes: 17 additions & 17 deletions src/main/java/io/quarkus/bot/release/util/Issues.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public ReleaseInformation extractReleaseInformationFromForm(String description)
return new ReleaseInformation(null, branch, qualifier, major);
}

public ReleaseInformation extractReleaseInformation(String body) {
if (body == null || body.isBlank()) {
public ReleaseInformation extractReleaseInformation(UpdatedIssueBody updatedIssueBody) {
if (updatedIssueBody.isBlank()) {
throw new IllegalStateException("Unable to extract release information as body is empty");
}

Matcher releaseInformationMatcher = RELEASE_INFORMATION_PATTERN.matcher(body);
Matcher releaseInformationMatcher = RELEASE_INFORMATION_PATTERN.matcher(updatedIssueBody.getBody());
if (!releaseInformationMatcher.find()) {
throw new IllegalStateException("Invalid release information in body:\n" + body);
throw new IllegalStateException("Invalid release information in body:\n" + updatedIssueBody);
}

try {
Expand All @@ -99,28 +99,28 @@ public ReleaseInformation extractReleaseInformation(String body) {
}
}

public String appendReleaseInformation(String body, ReleaseInformation releaseInformation) {
public String appendReleaseInformation(UpdatedIssueBody updatedIssueBody, ReleaseInformation releaseInformation) {
try {
String descriptor = RELEASE_INFORMATION_MARKER + "\n" + objectMapper.writeValueAsString(releaseInformation) + END_OF_MARKER;

if (body == null || !body.contains(RELEASE_INFORMATION_MARKER)) {
return (body != null ? body + "\n\n" : "") + descriptor;
if (!updatedIssueBody.contains(RELEASE_INFORMATION_MARKER)) {
return updatedIssueBody.append(descriptor);
}

return RELEASE_INFORMATION_PATTERN.matcher(body).replaceFirst(descriptor);
return updatedIssueBody.replace(RELEASE_INFORMATION_PATTERN, descriptor);
} catch (Exception e) {
throw new IllegalStateException("Unable to update the release information descriptor", e);
}
}

public ReleaseStatus extractReleaseStatus(String body) {
if (body == null || body.isBlank()) {
throw new IllegalStateException("Invalid release status in body:\n" + body);
public ReleaseStatus extractReleaseStatus(UpdatedIssueBody updatedIssueBody) {
if (updatedIssueBody.isBlank()) {
throw new IllegalStateException("Unable to extract release status as body is empty");
}

Matcher releaseStatusMatcher = RELEASE_STATUS_PATTERN.matcher(body);
Matcher releaseStatusMatcher = RELEASE_STATUS_PATTERN.matcher(updatedIssueBody.getBody());
if (!releaseStatusMatcher.find()) {
throw new IllegalStateException("Invalid release status in body:\n" + body);
throw new IllegalStateException("Invalid release status in body:\n" + updatedIssueBody);
}

try {
Expand All @@ -130,15 +130,15 @@ public ReleaseStatus extractReleaseStatus(String body) {
}
}

public String appendReleaseStatus(String body, ReleaseStatus releaseStatus) {
public String appendReleaseStatus(UpdatedIssueBody updatedIssueBody, ReleaseStatus releaseStatus) {
try {
String descriptor = RELEASE_STATUS_MARKER + "\n" + objectMapper.writeValueAsString(releaseStatus) + END_OF_MARKER;

if (body == null || !body.contains(RELEASE_STATUS_MARKER)) {
return (body != null ? body + "\n\n" : "") + descriptor;
if (!updatedIssueBody.contains(RELEASE_STATUS_MARKER)) {
return updatedIssueBody.append(descriptor);
}

return RELEASE_STATUS_PATTERN.matcher(body).replaceFirst(descriptor);
return updatedIssueBody.replace(RELEASE_STATUS_PATTERN, descriptor);
} catch (Exception e) {
throw new IllegalStateException("Unable to update the release status descriptor", e);
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/io/quarkus/bot/release/util/UpdatedIssueBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.bot.release.util;

import java.util.regex.Pattern;

public class UpdatedIssueBody {

private String body;

public UpdatedIssueBody(String body) {
this.body = body;
}

public String getBody() {
return body;
}

public boolean contains(String section) {
if (isBlank()) {
return false;
}

return body.contains(section);
}

public String append(String append) {
this.body = (this.body != null ? this.body + "\n\n" : "") + append;
return this.body;
}

public String replace(Pattern pattern, String replacement) {
this.body = pattern.matcher(body).replaceFirst(replacement);
return this.body;
}

public boolean isBlank() {
return body == null || body.isBlank();
}

@Override
public String toString() {
return body;
}
}
27 changes: 13 additions & 14 deletions src/test/java/io/quarkus/bot/release/IssuesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

import org.junit.jupiter.api.Test;

import io.quarkus.arc.Arc;
import io.quarkus.bot.release.step.Prerequisites;
import io.quarkus.bot.release.step.Step;
import io.quarkus.bot.release.step.StepStatus;
import io.quarkus.bot.release.util.Issues;
import io.quarkus.bot.release.util.UpdatedIssueBody;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
Expand Down Expand Up @@ -59,7 +58,7 @@ void testExtractReleaseInformationFromForm() {

@Test
void testAppendReleaseInformation() {
assertThat(issues.appendReleaseInformation("", new ReleaseInformation(null, "3.6", null, false))).isEqualTo("""
assertThat(issues.appendReleaseInformation(new UpdatedIssueBody(""), new ReleaseInformation(null, "3.6", null, false))).isEqualTo("""
<!-- quarkus-release/release-information:
Expand All @@ -70,15 +69,15 @@ void testAppendReleaseInformation() {
major: false
-->""");

assertThat(issues.appendReleaseInformation("""
assertThat(issues.appendReleaseInformation(new UpdatedIssueBody("""
This is a comment.
<!-- quarkus-release/release-information:
---
branch: "3.6"
qualifier: null
major: false
-->""", new ReleaseInformation("3.7.1", "3.7", "CR1", true))).isEqualTo("""
-->"""), new ReleaseInformation("3.7.1", "3.7", "CR1", true))).isEqualTo("""
This is a comment.
<!-- quarkus-release/release-information:
Expand All @@ -92,7 +91,7 @@ void testAppendReleaseInformation() {

@Test
void testExtractReleaseInformation() {
assertThat(issues.extractReleaseInformation("""
assertThat(issues.extractReleaseInformation(new UpdatedIssueBody("""
This is a comment.
<!-- quarkus-release/release-information:
Expand All @@ -108,9 +107,9 @@ void testExtractReleaseInformation() {
currentStep: "APPROVE_RELEASE"
currentStepStatus: "STARTED"
workflowRunId: 123
-->""")).isEqualTo(new ReleaseInformation(null, "4.0", "CR1", true));
-->"""))).isEqualTo(new ReleaseInformation(null, "4.0", "CR1", true));

assertThat(issues.extractReleaseInformation("""
assertThat(issues.extractReleaseInformation(new UpdatedIssueBody("""
This is a comment.
<!-- quarkus-release/release-information:
Expand All @@ -126,12 +125,12 @@ void testExtractReleaseInformation() {
currentStep: "APPROVE_RELEASE"
currentStepStatus: "STARTED"
workflowRunId: 123
-->""")).isEqualTo(new ReleaseInformation("4.0.0.CR1", "4.0", "CR1", true));
-->"""))).isEqualTo(new ReleaseInformation("4.0.0.CR1", "4.0", "CR1", true));
}

@Test
void testAppendReleaseStatus() {
assertThat(issues.appendReleaseStatus("", new ReleaseStatus(Status.STARTED, Step.APPROVE_CORE_RELEASE, StepStatus.STARTED, 123L))).isEqualTo("""
assertThat(issues.appendReleaseStatus(new UpdatedIssueBody(""), new ReleaseStatus(Status.STARTED, Step.APPROVE_CORE_RELEASE, StepStatus.STARTED, 123L))).isEqualTo("""
<!-- quarkus-release/release-status:
Expand All @@ -142,7 +141,7 @@ void testAppendReleaseStatus() {
workflowRunId: 123
-->""");

assertThat(issues.appendReleaseStatus("""
assertThat(issues.appendReleaseStatus(new UpdatedIssueBody("""
This is a comment.
<!-- quarkus-release/release-information:
Expand All @@ -158,7 +157,7 @@ void testAppendReleaseStatus() {
currentStep: "APPROVE_CORE_RELEASE"
currentStepStatus: "STARTED"
workflowRunId: 123
-->""", new ReleaseStatus(Status.COMPLETED, Step.CORE_RELEASE_PREPARE, StepStatus.COMPLETED, 145L))).isEqualTo("""
-->"""), new ReleaseStatus(Status.COMPLETED, Step.CORE_RELEASE_PREPARE, StepStatus.COMPLETED, 145L))).isEqualTo("""
This is a comment.
<!-- quarkus-release/release-information:
Expand All @@ -179,7 +178,7 @@ void testAppendReleaseStatus() {

@Test
void testExtractReleaseStatus() {
assertThat(issues.extractReleaseStatus("""
assertThat(issues.extractReleaseStatus(new UpdatedIssueBody("""
This is a comment.
<!-- quarkus-release/release-information:
Expand All @@ -195,6 +194,6 @@ void testExtractReleaseStatus() {
currentStep: "APPROVE_CORE_RELEASE"
currentStepStatus: "STARTED"
workflowRunId: 123
-->""")).isEqualTo(new ReleaseStatus(Status.STARTED, Step.APPROVE_CORE_RELEASE, StepStatus.STARTED, 123L));
-->"""))).isEqualTo(new ReleaseStatus(Status.STARTED, Step.APPROVE_CORE_RELEASE, StepStatus.STARTED, 123L));
}
}

0 comments on commit cdcbd99

Please sign in to comment.