You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pekko.apache.org by md...@apache.org on 2023/02/21 10:04:04 UTC

[incubator-pekko-http] 02/02: Update sbt header check in CopyrightHeader

This is an automated email from the ASF dual-hosted git repository.

mdedetrich pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-pekko-http.git

commit 8a330534956c90742b1c4c886f57f711dbc8b887
Author: Matthew de Detrich <ma...@aiven.io>
AuthorDate: Mon Feb 20 23:40:25 2023 +0100

    Update sbt header check in CopyrightHeader
---
 project/CopyrightHeader.scala | 63 ++++++++++++++++++++++++-------------------
 1 file changed, 36 insertions(+), 27 deletions(-)

diff --git a/project/CopyrightHeader.scala b/project/CopyrightHeader.scala
index d914a9cd3..fa5040b89 100644
--- a/project/CopyrightHeader.scala
+++ b/project/CopyrightHeader.scala
@@ -14,7 +14,8 @@
 package akka
 
 import sbt._, Keys._
-import de.heikoseeberger.sbtheader.{ CommentCreator, HeaderPlugin }
+import de.heikoseeberger.sbtheader.{ CommentCreator, HeaderPlugin, NewLine }
+import org.apache.commons.lang3.StringUtils
 
 object CopyrightHeader extends AutoPlugin {
   import HeaderPlugin.autoImport._
@@ -27,7 +28,7 @@ object CopyrightHeader extends AutoPlugin {
     Seq(Compile, Test).flatMap { config =>
       inConfig(config)(
         Seq(
-          headerLicense := Some(HeaderLicense.Custom(headerFor(CurrentYear))),
+          headerLicense := Some(HeaderLicense.Custom(apacheHeader)),
           headerMappings := headerMappings.value ++ Map(
             HeaderFileType.scala -> cStyleComment,
             HeaderFileType.java -> cStyleComment,
@@ -36,35 +37,43 @@ object CopyrightHeader extends AutoPlugin {
     ValidatePR / additionalTasks += Compile / headerCheck,
     ValidatePR / additionalTasks += Test / headerCheck)
 
-  // We hard-code this so PR's created in year X will not suddenly in X+1.
-  // Of course we should remember to update it early in the year.
-  val CurrentYear = "2022"
-  val CopyrightPattern = "Copyright \\([Cc]\\) (\\d{4}(-\\d{4})?) (Lightbend|Typesafe) Inc. <.*>".r
-  val CopyrightHeaderPattern = s"(?s).*${CopyrightPattern}.*".r
-
-  def headerFor(year: String): String =
-    s"Copyright (C) $year Lightbend Inc. <https://www.lightbend.com>"
+  val apacheHeader: String =
+    """Licensed to the Apache Software Foundation (ASF) under one or more
+      |license agreements; and to You under the Apache License, version 2.0:
+      |
+      |  https://www.apache.org/licenses/LICENSE-2.0
+      |
+      |This file is part of the Apache Pekko project, derived from Akka.
+      |""".stripMargin
 
   val cStyleComment = HeaderCommentStyle.cStyleBlockComment.copy(commentCreator = new CommentCreator() {
-    import HeaderCommentStyle.cStyleBlockComment.commentCreator
-
-    def updateLightbendHeader(header: String): String = header match {
-      case CopyrightHeaderPattern(years, null, _) =>
-        if (years != CurrentYear)
-          CopyrightPattern.replaceFirstIn(header, headerFor(years + "-" + CurrentYear))
-        else
-          CopyrightPattern.replaceFirstIn(header, headerFor(years))
-      case CopyrightHeaderPattern(years, endYears, _) =>
-        CopyrightPattern.replaceFirstIn(header, headerFor(years.replace(endYears, "-" + CurrentYear)))
-      case _ =>
-        header
-    }
 
     override def apply(text: String, existingText: Option[String]): String = {
-      existingText
-        .map(updateLightbendHeader)
-        .getOrElse(commentCreator(text, existingText))
-        .trim
+      val formatted = existingText match {
+        case Some(currentText) if isApacheCopyrighted(currentText) || isGenerated(currentText) =>
+          currentText
+        case Some(currentText) if isOnlyLightbendCopyrightAnnotated(currentText) =>
+          HeaderCommentStyle.cStyleBlockComment.commentCreator(text, existingText) + NewLine * 2 + currentText
+        case Some(currentText) =>
+          throw new IllegalStateException(s"Unable to detect copyright for header: [${currentText}]")
+        case None =>
+          HeaderCommentStyle.cStyleBlockComment.commentCreator(text, existingText)
+      }
+      formatted.trim
     }
   })
+
+  private def isGenerated(text: String): Boolean =
+    StringUtils.contains(text, "DO NOT EDIT DIRECTLY")
+
+  private def isApacheCopyrighted(text: String): Boolean =
+    StringUtils.containsIgnoreCase(text, "licensed to the apache software foundation (asf)") ||
+    StringUtils.containsIgnoreCase(text, "www.apache.org/licenses/license-2.0")
+
+  private def isLightbendCopyrighted(text: String): Boolean =
+    StringUtils.containsIgnoreCase(text, "lightbend inc.")
+
+  private def isOnlyLightbendCopyrightAnnotated(text: String): Boolean = {
+    isLightbendCopyrighted(text) && !isApacheCopyrighted(text)
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pekko.apache.org
For additional commands, e-mail: commits-help@pekko.apache.org