Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CODEOWNERS Enhancements #37

Merged
merged 1 commit into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public Collection<? extends SourceFile> generate(Scanned acc, ExecutionContext c
return Collections.emptyList();
}
PlainTextParser parser = new PlainTextParser();
return parser.parse("* " + acc.teamName())
String line = "* " + acc.teamName() + "\n";
return parser.parse(line)
.map(brandNewFile -> (PlainText) brandNewFile.withSourcePath(Paths.get(FILE_PATH)))
.collect(Collectors.toList());
}
Expand All @@ -93,31 +94,41 @@ public Collection<? extends SourceFile> generate(Scanned acc, ExecutionContext c
public TreeVisitor<?, ExecutionContext> getVisitor(Scanned acc) {
return new PlainTextVisitor<ExecutionContext>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext executionContext) {
if (acc.presentIn(text.getText())) {
return text;
public PlainText visitText(PlainText plainText, ExecutionContext executionContext) {
if (!FILE_PATH.equals(plainText.getSourcePath().toString())) {
return plainText;
}
String text = plainText.getText();
if (acc.presentIn(text)) {
return plainText;
}
boolean endsWithNewLine = text.endsWith("\n");
List<String> lines = new LinkedList<>();
List<String> after = new LinkedList<>();
Scanner scanner = new Scanner(text.getText());
int atPos = 0;
boolean lastComment = true;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (atPos == 0 && line.contains("@")) {
atPos = line.indexOf("@");
try (Scanner scanner = new Scanner(text)) {
int atPos = 0;
boolean lastComment = true;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (atPos == 0 && line.contains("@")) {
atPos = line.indexOf("@");
}
if (lastComment && line.startsWith("#")) {
lines.add(line);
} else {
lastComment = false;
after.add(line);
}
}
if (lastComment && line.startsWith("#")) {
lines.add(line);
} else {
lastComment = false;
after.add(line);
int spaces = Math.max(1, atPos - 1);
lines.add("*" + StringUtils.repeat(" ", spaces) + acc.teamName());
lines.addAll(after);
String updated = String.join("\n", lines);
if (endsWithNewLine) {
updated += "\n";
}
return plainText.withText(updated);
}
int spaces = Math.max(1, atPos - 1);
lines.add("*" + StringUtils.repeat(" ", spaces) + acc.teamName());
lines.addAll(after);
return text.withText(String.join("\n", lines));
}
};
}
Expand All @@ -133,7 +144,7 @@ public Scanned(TeamNameGenerator<TeamNameInput> generator) {
}

boolean presentIn(String text) {
Pattern p = Pattern.compile("^\\*\\s+" + teamName() + "$");
Pattern p = Pattern.compile("^\\*\\s+" + teamName() + "\\s*$");
try (Scanner s = new Scanner(text)) {
while (s.hasNextLine()) {
String line = s.nextLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -59,15 +61,40 @@ void shouldAddFileIfMissing() {
pomXml(POM),
text(null,
"""
* @jenkinsci/sample-plugin-developers
""".stripIndent(),
s -> s.path(".github/CODEOWNERS")
* @jenkinsci/sample-plugin-developers
""",
s -> s.path(".github/CODEOWNERS").noTrim()
)
);
}

@Test
void shouldAddLineIfTeamNotDefinedForAllRetainingTrailingSpace() {
rewriteRun(
pomXml(POM),
text(
"""
# This is a comment.
* @global-owner1 @global-owner2
*.js @js-owner #This is an inline comment.
/build/logs/ @doctocat

""",
"""
# This is a comment.
* @jenkinsci/sample-plugin-developers
* @global-owner1 @global-owner2
*.js @js-owner #This is an inline comment.
/build/logs/ @doctocat

""",
s -> s.path(".github/CODEOWNERS").noTrim()
)
);
}

@Test
void shouldAddLineIfTeamNotDefinedForAll() {
void shouldAddLineIfTeamNotDefinedForAllRetaining() {
rewriteRun(
pomXml(POM),
text(
Expand All @@ -76,15 +103,15 @@ void shouldAddLineIfTeamNotDefinedForAll() {
* @global-owner1 @global-owner2
*.js @js-owner #This is an inline comment.
/build/logs/ @doctocat
""".stripIndent(),
""",
"""
# This is a comment.
* @jenkinsci/sample-plugin-developers
* @global-owner1 @global-owner2
*.js @js-owner #This is an inline comment.
/build/logs/ @doctocat
""".stripIndent(),
s -> s.path(".github/CODEOWNERS")
""",
s -> s.path(".github/CODEOWNERS").noTrim()
)
);
}
Expand All @@ -104,7 +131,7 @@ void shouldHandleMultiModule() {
<module>different-plugin</module>
</modules>
</project>
""".stripIndent()),
"""),
mavenProject("plugin",
pomXml("""
<project>
Expand All @@ -122,7 +149,7 @@ void shouldHandleMultiModule() {
</repository>
</repositories>
</project>
""".stripIndent())),
""")),
mavenProject("different-plugin",
pomXml("""
<project>
Expand All @@ -140,24 +167,42 @@ void shouldHandleMultiModule() {
</repository>
</repositories>
</project>
""".stripIndent()))),
"""))),
text(
null,
"""
* @jenkinsci/sample-plugin-developers
""".stripIndent(),
s -> s.path(".github/CODEOWNERS")
""",
s -> s.path(".github/CODEOWNERS").noTrim()
));
}

@Test
void shouldNoOpIfTeamAlreadyDefinedForAll() {
@ParameterizedTest
@ValueSource(strings = {
"* @jenkinsci/sample-plugin-developers",
"\n* @jenkinsci/sample-plugin-developers ",
"\n* @jenkinsci/sample-plugin-developers\n",
})
void shouldNoOpIfTeamAlreadyDefinedForAll(String content) {
rewriteRun(
pomXml(POM),
text(
"* @jenkinsci/sample-plugin-developers",
s -> s.path(".github/CODEOWNERS")
content,
s -> s.path(".github/CODEOWNERS").noTrim()
)
);
}

@Test
void shouldNotModifyNonCodeowners() {
rewriteRun(
pomXml(POM),
text("*.iml",
s -> s.path(".gitignore")),
text(
"* @jenkinsci/sample-plugin-developers",
s -> s.path(".github/CODEOWNERS").noTrim()
)
);
}
}