Skip to content

Commit

Permalink
Merge pull request #1139 from shyim/add-label-functions-gitlab
Browse files Browse the repository at this point in the history
Add addLabels or removeLabels to Gitlab API
  • Loading branch information
orta committed May 11, 2021
2 parents 748a3a6 + cd3a5db commit e3b0040
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<!-- Your comment below this -->

- Fix for supporting Bitbucket Server personal repositories
- GitLab: Added `GitLabApi` to `danger.gitlab.api`. - [@shyim]
- GitLab: Added label helper functions to `danger.gitlab.api.addLabels` and `danger.gitlab.api.removeLabels`. - [@shyim]
<!-- Your comment above this -->

# 10.6.5
Expand Down
32 changes: 32 additions & 0 deletions source/platforms/gitlab/GitLabAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ class GitLabAPI {
return mr
}

updateMergeRequestInfo = async (changes: object): Promise<object> => {
const mr = this.api.MergeRequests.edit(this.repoMetadata.repoSlug, Number(this.repoMetadata.pullRequestID), changes)

this.d("updateMergeRequestInfo", mr)

return mr
}

getMergeRequestApprovals = async (): Promise<GitLabApproval> => {
this.d(`getMergeRequestApprovals for repo: ${this.repoMetadata.repoSlug} pr: ${this.repoMetadata.pullRequestID}`)
const approvals = (await this.api.MergeRequests.approvals(this.repoMetadata.repoSlug, {
Expand Down Expand Up @@ -239,6 +247,30 @@ class GitLabAPI {
const compare = (await api.compare(projectId, base, head)) as GitLabRepositoryCompare
return compare.diffs
}

addLabels = async (...labels: string[]): Promise<boolean> => {
const mr = await this.getMergeRequestInfo()
mr.labels.push(...labels)

await this.updateMergeRequestInfo({ labels: mr.labels.join(",") })

return true
}

removeLabels = async (...labels: string[]): Promise<boolean> => {
const mr = await this.getMergeRequestInfo()

for (const label of labels) {
const index = mr.labels.indexOf(label)
if (index > -1) {
mr.labels.splice(index, 1)
}
}

await this.updateMergeRequestInfo({ labels: mr.labels.join(",") })

return true
}
}

export default GitLabAPI

0 comments on commit e3b0040

Please sign in to comment.