Skip to content

Commit

Permalink
feat(#23): Analysis prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Apr 10, 2024
1 parent c32b5e8 commit 2bc808f
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import git.tracehub.codereview.action.github.FixedReviews;
import git.tracehub.codereview.action.github.GhRequest;
import git.tracehub.codereview.action.github.JsonReviews;
import git.tracehub.codereview.action.github.PullChanges;
import git.tracehub.codereview.action.github.WithComments;
import git.tracehub.codereview.action.prompt.AnalysisPrompt;
import git.tracehub.codereview.action.prompt.SystemPrompt;
import javax.json.JsonArray;
import lombok.RequiredArgsConstructor;
import org.cactoos.Proc;
Expand All @@ -54,6 +57,14 @@ public void exec(final Pull pull) throws Exception {
new GhRequest(this.token),
pull
).value();
Logger.info(Entry.class, "found reviews: %s", reviews);
Logger.info(this, "found reviews: %s", reviews);
final String prompt = new AnalysisPrompt(
new PullChanges(pull),
new Pull.Smart(pull).title(),
reviews
).asString();
Logger.info(this, "compiled user prompt: %s", prompt);
final String system = new SystemPrompt().asString();
Logger.info(this, "compiled system prompt: %s", system);
}
}
4 changes: 0 additions & 4 deletions src/main/java/git/tracehub/codereview/action/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ public final class Entry {
*
* @param args Application arguments
* @throws Exception if something went wrong.
* @todo #2:30min Develop a prompt to the language model.
* After information is collected (pull request itself, its files,
* and reviews) we can feed it into the model asking what is the quality
* of the following code review.
* @todo #51:45min Box SkipIfTooSmall.java into more major routine.
* SkipIfTooSmall.java is a good routine, but it should be encapsulated
* by something that logically is bigger than that. For now it can
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package git.tracehub.codereview.action.prompt;

import java.util.List;
import javax.json.JsonArray;
import javax.json.JsonObject;
import lombok.RequiredArgsConstructor;
import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.list.ListOf;

/**
* Analysis prompt.
*
* @since 0.0.0
*/
@RequiredArgsConstructor
public final class AnalysisPrompt implements Text {

/**
* Pull request.
*/
private final Scalar<JsonArray> changes;

/**
* Pull request title.
*/
private final String title;

/**
* Pull request reviews.
*/
private final JsonArray reviews;

@Override
public String asString() throws Exception {
final List<String> composed = new ListOf<>();
this.changes.value()
.forEach(
change -> {
final JsonObject json = change.asJsonObject();
composed.add(
String.join(
"\n",
String.format(
"filename: %s",
json.getString("filename")
),
"patch:",
json.getString("patch"),
String.format(
"additions: %s",
json.getInt("additions")
),
String.format(
"deletions: %s",
json.getInt("deletions")
),
String.format(
"changes: %s",
json.getInt("changes")
)
)
);
});
return String.join(
"\n",
"Please analyze how thorough the code review was.",
"In the end of analysis suggest a review score, like \"excellent",
" review\", \"poor review\" or something in the middle",
"Pull Request: ",
String.format(
"PR title: %s",
this.title
),
"PR changes:",
composed.toString(),
"Code review: ",
this.reviews.toString()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package git.tracehub.codereview.action.prompt;

import org.cactoos.Text;

/**
* System prompt.
*
* @since 0.0.0
* @checkstyle LineLengthCheck (20 lines)
*/
public final class SystemPrompt implements Text {

@Override
public String asString() {
return "You are a software quality analysis expert tasked with reviewing incoming code reviews that developers are submit on GitHub Pull requests platform";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* Prompts.
*
* @since 0.0.0
*/
package git.tracehub.codereview.action.prompt;
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package git.tracehub.codereview.action.prompt;

import com.jcabi.github.Pull;
import git.tracehub.codereview.action.extentions.PullFiles;
import git.tracehub.codereview.action.extentions.PullFilesExtension;
import git.tracehub.codereview.action.github.Authored;
import git.tracehub.codereview.action.github.PullChanges;
import java.io.InputStreamReader;
import javax.json.Json;
import org.cactoos.io.ResourceOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Test case for {@link AnalysisPrompt}.
*
* @since 0.0.0
*/
final class AnalysisPromptTest {

@Test
@PullFiles("git/tracehub/codereview/action/github/small.json")
@ExtendWith(PullFilesExtension.class)
void compilesPrompt(final Pull mock) throws Exception {
final String prompt = new AnalysisPrompt(
new PullChanges(mock),
"feat(#1): xsl changes",
new Authored(
() -> Json.createReader(
new InputStreamReader(
new ResourceOf(
"git/tracehub/codereview/action/github/comments.json"
).stream()
)
).readArray()
).value()
).asString();
final String expected = String.join(
"\n",
"Please analyze how thorough the code review was.",
"In the end of analysis suggest a review score, like \"excellent",
" review\", \"poor review\" or something in the middle",
"Pull Request: ",
"PR title: feat(#1): xsl changes",
"PR changes:",
"[filename: eo-maven-plugin/src/main/resources/org/eolang/maven/pre/to-java.xsl",
"patch:",
"@@ -419,9 +419,6 @@ SOFTWARE.\n <xsl:when test=\"$method='&amp;'\">\n <xsl:text>σ</xsl:text>\n </xsl:when>\n- <xsl:when test=\"$method='&lt;'\">\n- <xsl:text>ν</xsl:text>\n- </xsl:when>\n <xsl:otherwise>\n <xsl:value-of select=\"eo:attr-name($method)\"/>\n </xsl:otherwise>",
"additions: 0",
"deletions: 3",
"changes: 3]",
"Code review: ",
"[\"h1alexbel: @hizmailovich first comment\",\"h1alexbel: @hizmailovich second comment\",\"h1alexbel: @hizmailovich This sentence might be connected with the previous one: \\\"The subject of this article is caching.\\\"\"]"
);
MatcherAssert.assertThat(
String.format(
"Compiled prompt (%s) does not match with expected (%s)",
prompt,
expected
),
prompt,
new IsEqual<>(expected)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package git.tracehub.codereview.action.prompt;

import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link SystemPrompt}.
*
* @since 0.0.0
* @checkstyle LineLengthCheck (20 lines)
*/
final class SystemPromptTest {

@Test
void compilesSystemPrompt() {
final String prompt = new SystemPrompt().asString();
final String expected = "You are a software quality analysis expert tasked with reviewing incoming code reviews that developers are submit on GitHub Pull requests platform";
MatcherAssert.assertThat(
String.format(
"Compiled prompt %s does not match with expected %s",
prompt,
expected
),
prompt,
new IsEqual<>(expected)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Tracehub.git
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* Tests for prompts.
*
* @since 0.0.0
*/
package git.tracehub.codereview.action.prompt;

0 comments on commit 2bc808f

Please sign in to comment.