All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gitbucket.core.view.LinkConverter.scala Maven / Gradle / Ivy

There is a newer version: 4.9.0
Show newest version
package gitbucket.core.view

import gitbucket.core.controller.Context
import gitbucket.core.service.{RepositoryService, RequestCache}
import gitbucket.core.util.Implicits.RichString

trait LinkConverter { self: RequestCache =>

  /**
   * Creates a link to the issue or the pull request from the issue id.
   */
  protected def createIssueLink(repository: RepositoryService.RepositoryInfo, issueId: Int)(implicit context: Context): String = {
    val userName       = repository.repository.userName
    val repositoryName = repository.repository.repositoryName

    getIssue(userName, repositoryName, issueId.toString) match {
      case Some(issue) if (issue.isPullRequest) =>
        s"""Pull #${issueId}"""
      case Some(_) =>
        s"""Issue #${issueId}"""
      case None =>
        s"Unknown #${issueId}"
    }
  }


  /**
   * Converts issue id, username and commit id to link in the given text.
   */
  protected def convertRefsLinks(text: String, repository: RepositoryService.RepositoryInfo,
                                 issueIdPrefix: String =  "#", escapeHtml: Boolean = true)(implicit context: Context): String = {

    // escape HTML tags
    val escaped = if(escapeHtml) text.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) else text

    escaped
      // convert username/project@SHA to link
      .replaceBy("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)/([a-zA-Z0-9\\-_\\.]+)@([a-f0-9]{40})(?=(\\W|$))".r){ m =>
        getAccountByUserName(m.group(2)).map { _ =>
          s"""${m.group(2)}/${m.group(3)}@${m.group(4).substring(0, 7)}"""
        }
      }

      // convert username/project#Num to link
      .replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)/([a-zA-Z0-9\\-_\\.]+)" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r){ m =>
        getIssue(m.group(2), m.group(3), m.group(4)) match {
          case Some(issue) if (issue.isPullRequest) =>
            Some(s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""")
          case Some(_) =>
            Some(s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""")
          case None =>
            Some(s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""")
        }
      }

      // convert username@SHA to link
      .replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)@([a-f0-9]{40})(?=(\\W|$))").r ) { m =>
        getAccountByUserName(m.group(2)).map { _ =>
          s"""${m.group(2)}@${m.group(3).substring(0, 7)}"""
        }
      }

      // convert username#Num to link
      .replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r ) { m =>
        getIssue(m.group(2), repository.name, m.group(3)) match {
          case Some(issue) if(issue.isPullRequest) =>
            Some(s"""${m.group(2)}#${m.group(3)}""")
          case Some(_) =>
            Some(s"""${m.group(2)}#${m.group(3)}""")
          case None =>
            Some(s"""${m.group(2)}#${m.group(3)}""")
        }
      }

      // convert issue id to link
      .replaceBy(("(?<=(^|\\W))(GH-|" + issueIdPrefix + ")([0-9]+)(?=(\\W|$))").r){ m =>
        val prefix = if(m.group(2) == "issue:") "#" else m.group(2)
        getIssue(repository.owner, repository.name, m.group(3)) match {
          case Some(issue) if(issue.isPullRequest) =>
            Some(s"""${prefix}${m.group(3)}""")
          case Some(_) =>
            Some(s"""${prefix}${m.group(3)}""")
          case None =>
            Some(s"""${m.group(2)}${m.group(3)}""")
        }
      }

      // convert @username to link
      .replaceBy("(?<=(^|\\W))@([a-zA-Z0-9\\-_\\.]+)(?=(\\W|$))".r){ m =>
        getAccountByUserName(m.group(2)).map { _ =>
          s"""@${m.group(2)}"""
        }
      }

      // convert commit id to link
      .replaceAll("(?<=(^|[^\\w/@]))([a-f0-9]{40})(?=(\\W|$))", s"""$$2""")
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy