From 02c492d0541d132c4bb52b56fca8dbbf2982ae0e 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 Refs: #19 --- .../jenkins/github/AddTeamToCodeowners.java | 45 +++++++++++-------- .../github/AddTeamToCodeownersTest.java | 27 ++++++++++- 2 files changed, 51 insertions(+), 21 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..23d60f1 100644 --- a/src/main/java/org/openrewrite/jenkins/github/AddTeamToCodeowners.java +++ b/src/main/java/org/openrewrite/jenkins/github/AddTeamToCodeowners.java @@ -93,31 +93,38 @@ 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) { + 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)); } }; } diff --git a/src/test/java/org/openrewrite/jenkins/github/AddTeamToCodeownersTest.java b/src/test/java/org/openrewrite/jenkins/github/AddTeamToCodeownersTest.java index 1240bda..b28f7e8 100644 --- a/src/test/java/org/openrewrite/jenkins/github/AddTeamToCodeownersTest.java +++ b/src/test/java/org/openrewrite/jenkins/github/AddTeamToCodeownersTest.java @@ -58,8 +58,31 @@ void shouldAddFileIfMissing() { rewriteRun( pomXml(POM), text(null, + "* @jenkinsci/sample-plugin-developers\n", + s -> s.path(".github/CODEOWNERS") + ) + ); + } + + @Test + void shouldAddLineIfTeamNotDefinedForAllRetainingTrailingSpace() { + rewriteRun( + pomXml(POM), + text( """ - * @jenkinsci/sample-plugin-developers + # This is a comment. + * @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") ) @@ -67,7 +90,7 @@ void shouldAddFileIfMissing() { } @Test - void shouldAddLineIfTeamNotDefinedForAll() { + void shouldAddLineIfTeamNotDefinedForAllRetaining() { rewriteRun( pomXml(POM), text(