Add Issue label related APIs

This commit is contained in:
KOUNOIKE
2018-10-13 09:40:18 +09:00
parent f42ff60772
commit 2eee95bd2d
5 changed files with 94 additions and 4 deletions

View File

@@ -1,7 +1,12 @@
package gitbucket.core.controller
import gitbucket.core.api._
import gitbucket.core.controller.api.{ApiOrganizationControllerBase, ApiRepositoryControllerBase, ApiUserControllerBase}
import gitbucket.core.controller.api.{
ApiIssueLabelControllerBase,
ApiOrganizationControllerBase,
ApiRepositoryControllerBase,
ApiUserControllerBase
}
import gitbucket.core.service._
import gitbucket.core.util.Implicits._
import gitbucket.core.util._
@@ -12,6 +17,7 @@ class ApiController
with ApiOrganizationControllerBase
with ApiRepositoryControllerBase
with ApiUserControllerBase
with ApiIssueLabelControllerBase
with RepositoryService
with AccountService
with ProtectedBranchService
@@ -30,6 +36,7 @@ class ApiController
with WikiService
with ActivityService
with PrioritiesService
with AdminAuthenticator
with OwnerAuthenticator
with UsersAuthenticator
with GroupManagerAuthenticator

View File

@@ -109,26 +109,84 @@ trait ApiIssueLabelControllerBase extends ControllerBase {
* vi. List labels on an issue
* https://developer.github.com/v3/issues/labels/#list-labels-on-an-issue
*/
get("/api/v3/repos/:owner/:repository/issues/:id/labels")(referrersOnly { repository =>
JsonFormat(getIssueLabels(repository.owner, repository.name, params("id").toInt).map { l =>
ApiLabel(l, RepositoryName(repository.owner, repository.name))
})
})
/*
* vii. Add labels to an issue
* https://developer.github.com/v3/issues/labels/#add-labels-to-an-issue
*/
post("/api/v3/repos/:owner/:repository/issues/:id/labels")(writableUsersOnly { repository =>
JsonFormat(for {
data <- extractFromJsonBody[Seq[String]];
issueId <- params("id").toIntOpt
} yield {
data.map { labelName =>
val label = getLabel(repository.owner, repository.name, labelName).getOrElse(
getLabel(
repository.owner,
repository.name,
createLabel(repository.owner, repository.name, labelName)
).get
)
registerIssueLabel(repository.owner, repository.name, issueId, label.labelId, true)
ApiLabel(label, RepositoryName(repository.owner, repository.name))
}
})
})
/*
* viii. Remove a label from an issue
* https://developer.github.com/v3/issues/labels/#remove-a-label-from-an-issue
*/
delete("/api/v3/repos/:owner/:repository/issues/:id/labels/:name")(writableUsersOnly { repository =>
val issueId = params("id").toInt
val labelName = params("name")
getLabel(repository.owner, repository.name, labelName) match {
case Some(label) =>
deleteIssueLabel(repository.owner, repository.name, issueId, label.labelId, true)
JsonFormat(Seq(label))
case None =>
NotFound()
}
})
/*
* ix. Replace all labels for an issue
* https://developer.github.com/v3/issues/labels/#replace-all-labels-for-an-issue
*/
put("/api/v3/repos/:owner/:repository/issues/:id/labels")(writableUsersOnly { repository =>
JsonFormat(for {
data <- extractFromJsonBody[Seq[String]];
issueId <- params("id").toIntOpt
} yield {
deleteAllIssueLabels(repository.owner, repository.name, issueId, true)
data.map { labelName =>
val label = getLabel(repository.owner, repository.name, labelName).getOrElse(
getLabel(
repository.owner,
repository.name,
createLabel(repository.owner, repository.name, labelName)
).get
)
registerIssueLabel(repository.owner, repository.name, issueId, label.labelId, true)
ApiLabel(label, RepositoryName(repository.owner, repository.name))
}
})
})
/*
* x. Remove all labels from an issue
* https://developer.github.com/v3/issues/labels/#remove-all-labels-from-an-issue
*/
delete("/api/v3/repos/:owner/:repository/issues/:id/labels")(writableUsersOnly { repository =>
val issueId = params("id").toInt
deleteAllIssueLabels(repository.owner, repository.name, issueId, true)
NoContent()
})
/*
* xi Get labels for every issue in a milestone

View File

@@ -70,7 +70,7 @@ trait ApiUserControllerBase extends ControllerBase {
* https://developer.github.com/enterprise/2.14/v3/enterprise-admin/users/#suspend-a-user
*/
/*
* ghe: vii. Unsuspend a user
* https://developer.github.com/enterprise/2.14/v3/enterprise-admin/users/#unsuspend-a-user
*/
* ghe: vii. Unsuspend a user
* https://developer.github.com/enterprise/2.14/v3/enterprise-admin/users/#unsuspend-a-user
*/
}

View File

@@ -475,6 +475,25 @@ trait IssuesService {
IssueLabels filter (_.byPrimaryKey(owner, repository, issueId, labelId)) delete
}
def deleteAllIssueLabels(owner: String, repository: String, issueId: Int, insertComment: Boolean = false)(
implicit context: Context,
s: Session
): Int = {
if (insertComment) {
IssueComments insert IssueComment(
userName = owner,
repositoryName = repository,
issueId = issueId,
action = "delete_label",
commentedUserName = context.loginAccount.map(_.userName).getOrElse("Unknown user"),
content = "All labels",
registeredDate = currentDate,
updatedDate = currentDate
)
}
IssueLabels filter (_.byIssue(owner, repository, issueId)) delete
}
def createComment(
owner: String,
repository: String,

View File

@@ -3,6 +3,7 @@ package gitbucket.core.service
import gitbucket.core.model.Label
import gitbucket.core.model.Profile._
import gitbucket.core.model.Profile.profile.blockingApi._
import gitbucket.core.util.StringUtil
trait LabelsService {
@@ -24,6 +25,11 @@ trait LabelsService {
)
}
def createLabel(owner: String, repository: String, labelName: String)(implicit s: Session): Int = {
val color = StringUtil.md5(labelName).substring(0, 6)
createLabel(owner, repository, labelName, color)
}
def updateLabel(owner: String, repository: String, labelId: Int, labelName: String, color: String)(
implicit s: Session
): Unit =