diff --git a/src/main/scala/gitbucket/core/api/CreateAPullRequest.scala b/src/main/scala/gitbucket/core/api/CreateAPullRequest.scala index e02d065..112e721 100644 --- a/src/main/scala/gitbucket/core/api/CreateAPullRequest.scala +++ b/src/main/scala/gitbucket/core/api/CreateAPullRequest.scala @@ -15,3 +15,11 @@ base: String, maintainer_can_modify: Option[Boolean] ) + +case class UpdateAPullRequest( + title: Option[String], + body: Option[String], + state: Option[String], + base: Option[String], + maintainer_can_modify: Option[Boolean], +) diff --git a/src/main/scala/gitbucket/core/controller/api/ApiPullRequestControllerBase.scala b/src/main/scala/gitbucket/core/controller/api/ApiPullRequestControllerBase.scala index aa5e8da..1774f82 100644 --- a/src/main/scala/gitbucket/core/controller/api/ApiPullRequestControllerBase.scala +++ b/src/main/scala/gitbucket/core/controller/api/ApiPullRequestControllerBase.scala @@ -161,8 +161,28 @@ /* * v. Update a pull request - * https://developer.github.com/v3/pulls/#update-a-pull-request + * https://docs.github.com/en/rest/reference/pulls#update-a-pull-request */ + patch("/api/v3/repos/:owner/:repository/pulls/:id")(referrersOnly { repository => + (for { + issueId <- params("id").toIntOpt + account <- context.loginAccount + settings = context.settings + data <- extractFromJsonBody[UpdateAPullRequest] + } yield { + updatePullRequestsByApi( + repository, + issueId, + account, + settings, + data.title, + data.body, + data.state, + data.base + ) + JsonFormat(getApiPullRequest(repository, issueId)) + }) getOrElse NotFound() + }) /* * vi. List commits on a pull request diff --git a/src/main/scala/gitbucket/core/service/PullRequestService.scala b/src/main/scala/gitbucket/core/service/PullRequestService.scala index 8ab531c..0b211a5 100644 --- a/src/main/scala/gitbucket/core/service/PullRequestService.scala +++ b/src/main/scala/gitbucket/core/service/PullRequestService.scala @@ -14,7 +14,7 @@ import gitbucket.core.util.Implicits._ import gitbucket.core.util.JGitUtil import gitbucket.core.util.StringUtil._ -import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo} +import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo, getBranches} import gitbucket.core.view import gitbucket.core.view.helpers import org.eclipse.jgit.api.Git @@ -58,6 +58,15 @@ .map(pr => pr.isDraft) .update(false) + def updateBaseBranch(owner: String, repository: String, issueId: Int, baseBranch: String, commitIdTo: String)( + implicit s: Session + ): Unit = { + PullRequests + .filter(_.byPrimaryKey(owner, repository, issueId)) + .map(pr => pr.branch -> pr.commitIdTo) + .update((baseBranch, commitIdTo)) + } + def getPullRequestCountGroupByUser(closed: Boolean, owner: Option[String], repository: Option[String])( implicit s: Session ): List[PullRequestCount] = @@ -293,6 +302,76 @@ } } + def updatePullRequestsByApi( + repository: RepositoryInfo, + issueId: Int, + loginAccount: Account, + settings: SystemSettings, + title: Option[String], + body: Option[String], + state: Option[String], + base: Option[String] + )( + implicit s: Session, + c: JsonFormat.Context + ): Unit = { + getPullRequest(repository.owner, repository.name, issueId).foreach { + case (issue, pr) => + if (Repositories.filter(_.byRepository(pr.userName, pr.repositoryName)).exists.run) { + // Update base branch + base.foreach { _base => + if (pr.branch != _base) { + Using.resource(Git.open(getRepositoryDir(repository.owner, repository.name))) { git => + getBranches(git, repository.repository.defaultBranch, origin = true) + .find(_.name == _base) + .foreach(br => updateBaseBranch(repository.owner, repository.name, issueId, br.name, br.commitId)) + } + createComment( + repository.owner, + repository.name, + loginAccount.userName, + issue.issueId, + pr.branch + "\r\n" + _base, + "change_base_branch" + ) + } + } + // Update title and content + title.foreach { _title => + updateIssue(repository.owner, repository.name, issueId, _title, body) + if (issue.title != _title) { + createComment( + repository.owner, + repository.name, + loginAccount.userName, + issue.issueId, + issue.title + "\r\n" + _title, + "change_title" + ) + } + } + // Update state + val action = (state, issue.closed) match { + case (Some("open"), true) => + updateClosed(repository.owner, repository.name, issueId, closed = false) + "reopened" + case (Some("closed"), false) => + updateClosed(repository.owner, repository.name, issueId, closed = true) + "closed" + case _ => "edited" + } + // Call web hook + callPullRequestWebHookByRequestBranch( + action, + getRepository(repository.owner, repository.name).get, + pr.requestBranch, + loginAccount, + settings + ) + } + } + } + def getPullRequestByRequestCommit( userName: String, repositoryName: String, diff --git a/src/main/twirl/gitbucket/core/issues/commentlist.scala.html b/src/main/twirl/gitbucket/core/issues/commentlist.scala.html index 7d0397d..a0e36a0 100644 --- a/src/main/twirl/gitbucket/core/issues/commentlist.scala.html +++ b/src/main/twirl/gitbucket/core/issues/commentlist.scala.html @@ -243,6 +243,17 @@ } + case "change_base_branch" => { +
+
+ + @helpers.avatar(comment.commentedUserName, 16) + @helpers.user(comment.commentedUserName, styleClass="username strong") + change base branch from @convertLineSeparator(comment.content, "LF").split("\n")(0) to @convertLineSeparator(comment.content, "LF").split("\n")(1) + @gitbucket.core.helper.html.datetimeago(comment.registeredDate) +
+
+ } case _ => { @showFormattedComment(comment) }