From c1584bb80f12f12abc046acb016d7bfdaba61a79 Mon Sep 17 00:00:00 2001 From: Steve Hill Date: Sat, 19 Aug 2023 15:23:10 -0700 Subject: [PATCH] Generate CODEOWNERS with newline Ensure only the .github/CODEOWNERS file is modified. Refs: #19 --- .../jenkins/github/AddTeamToCodeowners.java | 53 ++++++++----- .../github/AddTeamToCodeownersTest.java | 77 +++++++++++++++---- 2 files changed, 93 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/openrewrite/jenkins/github/AddTeamToCodeowners.java b/src/main/java/org/openrewrite/jenkins/github/AddTeamToCodeowners.java index bf4a123..403ac93 100644 --- a/src/main/java/org/openrewrite/jenkins/github/AddTeamToCodeowners.java +++ b/src/main/java/org/openrewrite/jenkins/github/AddTeamToCodeowners.java @@ -84,7 +84,8 @@ public Collection 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()); } @@ -93,31 +94,41 @@ public Collection generate(Scanned acc, ExecutionContext c public TreeVisitor getVisitor(Scanned acc) { return new PlainTextVisitor() { @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 lines = new LinkedList<>(); List 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)); } }; } @@ -133,7 +144,7 @@ public Scanned(TeamNameGenerator 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(); diff --git a/src/test/java/org/openrewrite/jenkins/github/AddTeamToCodeownersTest.java b/src/test/java/org/openrewrite/jenkins/github/AddTeamToCodeownersTest.java index 1240bda..ca17dac 100644 --- a/src/test/java/org/openrewrite/jenkins/github/AddTeamToCodeownersTest.java +++ b/src/test/java/org/openrewrite/jenkins/github/AddTeamToCodeownersTest.java @@ -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; @@ -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( @@ -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() ) ); } @@ -104,7 +131,7 @@ void shouldHandleMultiModule() { different-plugin - """.stripIndent()), + """), mavenProject("plugin", pomXml(""" @@ -122,7 +149,7 @@ void shouldHandleMultiModule() { - """.stripIndent())), + """)), mavenProject("different-plugin", pomXml(""" @@ -140,24 +167,42 @@ void shouldHandleMultiModule() { - """.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() + ) + ); + } }