Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Mar 23, 2023
2 parents 378769e + 7c083c0 commit d57deee
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 29 deletions.
49 changes: 38 additions & 11 deletions src/main/java/com/rultor/agents/github/qtn/QnMerge.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package com.rultor.agents.github.qtn;

import com.jcabi.aspects.Immutable;
import com.jcabi.github.Check;
import com.jcabi.github.Comment;
import com.jcabi.github.Issue;
import com.jcabi.github.Pull;
Expand Down Expand Up @@ -72,21 +73,29 @@ public Req understand(final Comment.Smart comment,
final Issue.Smart issue = new Issue.Smart(comment.issue());
final Req req;
if (issue.isPull() && issue.isOpen()) {
new Answer(comment).post(
true,
String.format(
QnMerge.PHRASES.getString("QnMerge.start"),
home.toASCIIString()
)
);
Logger.info(
this, "merge request found in %s#%d, comment #%d",
issue.repo().coordinates(), issue.number(), comment.number()
);
req = QnMerge.pack(
comment,
issue.repo().pulls().get(issue.number())
);
if (QnMerge.allChecksSuccessful(issue.pull())) {
new Answer(comment).post(
true,
String.format(
QnMerge.PHRASES.getString("QnMerge.start"),
home.toASCIIString()
)
);
req = QnMerge.pack(
comment,
issue.repo().pulls().get(issue.number())
);
} else {
new Answer(comment).post(
false,
QnMerge.PHRASES.getString("QnMerge.checks-are-failed")
);
req = Req.EMPTY;
}
} else {
new Answer(comment).post(
false,
Expand Down Expand Up @@ -164,4 +173,22 @@ private static Req pack(final Comment.Smart comment,
return req;
}

/**
* Checks if all checks are successful.
* @param pull Pull
* @return True if all checks are successful
* @throws IOException If fails
*/
private static boolean allChecksSuccessful(final Pull pull)
throws IOException {
boolean result = true;
for (final Check check : pull.checks().all()) {
if (!check.successful()) {
result = false;
break;
}
}
return result;
}

}
69 changes: 51 additions & 18 deletions src/test/java/com/rultor/agents/github/qtn/QnMergeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@
*/
package com.rultor.agents.github.qtn;

import com.jcabi.github.Check;
import com.jcabi.github.Comment;
import com.jcabi.github.Comments;
import com.jcabi.github.Pull;
import com.jcabi.github.Repo;
import com.jcabi.github.mock.MkBranches;
import com.jcabi.github.mock.MkChecks;
import com.jcabi.github.mock.MkGithub;
import com.jcabi.matchers.XhtmlMatchers;
import com.rultor.agents.github.Req;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ResourceBundle;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.xembly.Directives;
import org.xembly.Xembler;
Expand Down Expand Up @@ -75,6 +76,11 @@ final class QnMergeTest {
*/
private transient Comments comments;

/**
* Pull request.
*/
private transient Pull pull;

/**
* Initial phase for all tests.
* @throws IOException In case of error.
Expand All @@ -87,18 +93,20 @@ public void setUp() throws IOException {
final String base = "base";
branches.create(head, "abcdef4");
branches.create(base, "abcdef5");
this.pull = repo.pulls().create("", head, base);
this.comments = repo.issues()
.get(repo.pulls().create("", head, base).number())
.get(this.pull.number())
.comments();
}

/**
* QnMerge can build a request.
* @throws Exception In case of error.
*
* @throws Exception In case of error
*/
@Test
public void buildsRequest() throws Exception {
final String request = new Xembler(this.requestDirectives()).xml();
final String request = new Xembler(this.mergeRequest()).xml();
MatcherAssert.assertThat(
request,
Matchers.allOf(
Expand Down Expand Up @@ -129,37 +137,62 @@ public void buildsRequest() throws Exception {
/**
* QnMerge can not build a request because some GitHub checks
* were failed.
* @throws Exception In case of error
* @todo #1237:90min We need to implement a verification logic
* for GitHub checks to determine whether they were completed
* correctly or not. If not, Rultor should not create a merge
* request and must display a comment to the user. Once the logic
* is implemented, we can enable that test.
*
* @throws IOException In case of I/O error
* @throws URISyntaxException In case of URI error
*/
@Test
@Disabled
public void stopsBecauseCiChecksFailed() throws Exception {
final String request = new Xembler(this.requestDirectives()).xml();
public void stopsBecauseCiChecksFailed()
throws IOException, URISyntaxException {
final MkChecks checks = (MkChecks) this.pull.checks();
checks.create(Check.Status.IN_PROGRESS, Check.Conclusion.SUCCESS);
this.mergeRequest();
MatcherAssert.assertThat(
new Comment.Smart(this.comments.get(1)).body(),
Matchers.is(QnMergeTest.COMMAND)
);
MatcherAssert.assertThat(
new Comment.Smart(this.comments.get(2)).body(),
Matchers.equalTo(
Matchers.containsString(
QnMergeTest.PHRASES.getString("QnMerge.checks-are-failed")
)
);
MatcherAssert.assertThat(request, Matchers.equalTo(Req.EMPTY));
}

/**
* Request directives.
* QnMerge can build a request because GitHub checks finished successfully.
*
* @throws IOException In case of I/O error
* @throws URISyntaxException In case of URI error
*/
@Test
public void continuesBecauseCiChecksSuccessful()
throws IOException, URISyntaxException {
final MkChecks checks = (MkChecks) this.pull.checks();
checks.create(Check.Status.COMPLETED, Check.Conclusion.SUCCESS);
this.mergeRequest();
MatcherAssert.assertThat(
new Comment.Smart(this.comments.get(1)).body(),
Matchers.is(QnMergeTest.COMMAND)
);
MatcherAssert.assertThat(
new Comment.Smart(this.comments.get(2)).body(),
Matchers.containsString(
String.format(
QnMergeTest.PHRASES.getString("QnMerge.start"),
"#"
)
)
);
}

/**
* Merge request directives.
* @return Directives
* @throws IOException In case of error
* @throws URISyntaxException In case of error
*/
private Directives requestDirectives() throws IOException,
private Directives mergeRequest() throws IOException,
URISyntaxException {
return new Directives()
.add("request")
Expand Down

0 comments on commit d57deee

Please sign in to comment.