diff --git a/src/main/scala/gitbucket/core/controller/ApiController.scala b/src/main/scala/gitbucket/core/controller/ApiController.scala index 049f59b..9f4dd89 100644 --- a/src/main/scala/gitbucket/core/controller/ApiController.scala +++ b/src/main/scala/gitbucket/core/controller/ApiController.scala @@ -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 @@ with ApiOrganizationControllerBase with ApiRepositoryControllerBase with ApiUserControllerBase + with ApiIssueLabelControllerBase with RepositoryService with AccountService with ProtectedBranchService @@ -30,6 +36,7 @@ with WikiService with ActivityService with PrioritiesService + with AdminAuthenticator with OwnerAuthenticator with UsersAuthenticator with GroupManagerAuthenticator diff --git a/src/main/scala/gitbucket/core/controller/api/ApiIssueLabelControllerBase.scala b/src/main/scala/gitbucket/core/controller/api/ApiIssueLabelControllerBase.scala index 0f9e4cc..307a4db 100644 --- a/src/main/scala/gitbucket/core/controller/api/ApiIssueLabelControllerBase.scala +++ b/src/main/scala/gitbucket/core/controller/api/ApiIssueLabelControllerBase.scala @@ -109,26 +109,84 @@ * 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 diff --git a/src/main/scala/gitbucket/core/controller/api/ApiUserControllerBase.scala b/src/main/scala/gitbucket/core/controller/api/ApiUserControllerBase.scala index d5bbbd8..dc708d9 100644 --- a/src/main/scala/gitbucket/core/controller/api/ApiUserControllerBase.scala +++ b/src/main/scala/gitbucket/core/controller/api/ApiUserControllerBase.scala @@ -70,7 +70,7 @@ * 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 + */ } diff --git a/src/main/scala/gitbucket/core/service/IssuesService.scala b/src/main/scala/gitbucket/core/service/IssuesService.scala index 86103d2..79223bf 100644 --- a/src/main/scala/gitbucket/core/service/IssuesService.scala +++ b/src/main/scala/gitbucket/core/service/IssuesService.scala @@ -475,6 +475,25 @@ 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, diff --git a/src/main/scala/gitbucket/core/service/LabelsService.scala b/src/main/scala/gitbucket/core/service/LabelsService.scala index 4bbd5d8..a0f7dc8 100644 --- a/src/main/scala/gitbucket/core/service/LabelsService.scala +++ b/src/main/scala/gitbucket/core/service/LabelsService.scala @@ -3,6 +3,7 @@ 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 @@ ) } + 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 =