Add update a pull request API (#2557)

This commit is contained in:
onukura
2020-10-02 07:42:30 +09:00
committed by GitHub
parent 61504ae9e3
commit cbddc34bfa
4 changed files with 120 additions and 2 deletions

View File

@@ -15,3 +15,11 @@ case class CreateAPullRequestAlt(
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],
)

View File

@@ -161,8 +161,28 @@ trait ApiPullRequestControllerBase extends ControllerBase {
/*
* 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

View File

@@ -14,7 +14,7 @@ import gitbucket.core.util.Directory._
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 @@ trait PullRequestService {
.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 @@ trait PullRequestService {
}
}
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,

View File

@@ -243,6 +243,17 @@
</div>
</div>
}
case "change_base_branch" => {
<div class="discussion-item discussion-item-pencil">
<div class="discussion-item-header">
<span class="discussion-item-icon"><i class="octicon octicon-pencil"></i></span>
@helpers.avatar(comment.commentedUserName, 16)
@helpers.user(comment.commentedUserName, styleClass="username strong")
change base branch from <code>@convertLineSeparator(comment.content, "LF").split("\n")(0)</code> to <code>@convertLineSeparator(comment.content, "LF").split("\n")(1)</code>
@gitbucket.core.helper.html.datetimeago(comment.registeredDate)
</div>
</div>
}
case _ => {
@showFormattedComment(comment)
}