Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Commit

Permalink
Show and accept merge requests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
oltruong committed Jan 10, 2018
1 parent 220e135 commit f763a67
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See [`src/gitlab-connector.coffee`](src/gitlab-connector.coffee) for full docume
- Search projects by name
- Display branches of a given project
- Trigger a pipeline
- Show or accept merge requests
- Display version

## Installation
Expand Down
9 changes: 9 additions & 0 deletions src/gitlab-client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class GitlabClient
getBranches: (projectId) ->
request.call(this).path('/api/v4/projects/' + projectId + '/repository/branches').get()

getMergeRequests: (projectId, filter) ->
filterPath=""
if (filter !="")
filterPath="?"+filter
request.call(this).path('/api/v4/projects/' + projectId + '/merge_requests'+filterPath).get()

acceptMergeRequest: (projectId, merge_iid) ->
request.call(this).path('/api/v4/projects/' + projectId + '/merge_requests/'+merge_iid+'/merge').put()

triggerPipeline: (projectId, params) ->
request.call(this).header('Content-type', 'application/json').path('/api/v4/projects/' + projectId + '/trigger/pipeline').post(params)

Expand Down
2 changes: 2 additions & 0 deletions src/gitlab-connector.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ createPipeline = require("./parser-pipeline")
getVersion = require("./parser-version")
getProjects = require("./parser-project")
getBranches = require("./parser-branch")
mergeRequests = require("./parser-merge-requests")
help = require("./parser-help")

module.exports = (robot) ->
Expand All @@ -33,6 +34,7 @@ module.exports = (robot) ->
when "pipeline" then createPipeline(gitlabClient, res, command)
when "projects" then getProjects(gitlabClient, res, command)
when "branches" then getBranches(gitlabClient, res, command)
when "merge" then mergeRequests(gitlabClient, res, command)
when "version" then getVersion(gitlabClient, res)
when "help" then help.sendHelp(res)
else
Expand Down
10 changes: 6 additions & 4 deletions src/parser-help.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ help.sendHelp = (res) ->
help.sendUnknownCommand = (res, command) ->
res.reply "Sorry, I did not understand command '" + command + "'. Here are all the available commands:" + '\n' + HELP

HELP_BRANCH = "gitlab branches projectId - shows the branches for the project whose id is projectId"
HELP_PIPELINE = "gitlab pipeline trigger projectId branchName - triggers a pipeline on a branch matching branchName for the project whose id is projectId"
HELP_MERGE_REQUEST = "gitlab merge request projectId filter - shows the merge requests for the project whose id is projectId with filter(optional, e.g. state=opened)"
HELP_MERGE_REQUEST_ACCEPT = "gitlab merge request projectId accept merge_iid - accepts the merge request iid for the project whose id is projectId"
HELP_VERSION = "gitlab version - returns version"
HELP_DEFAULT = "gitlab help - displays all available commands"
HELP_PIPELINE = "gitlab pipeline trigger projectId branchName - triggers a pipeline on a branch matching branchName for the project with Id projectId"
HELP_PROJECT = "gitlab projects searchName - shows the projects whose name contains searchName (optional)"
HELP_BRANCH = "gitlab branches projectId - shows the branches for the project with Id projectId"
HELP_DEFAULT = "gitlab help - displays all available commands"

HELP = [HELP_PROJECT, HELP_PIPELINE, HELP_BRANCH, HELP_VERSION, HELP_DEFAULT].join('\n')
HELP = [HELP_BRANCH,HELP_PROJECT,HELP_MERGE_REQUEST,HELP_MERGE_REQUEST_ACCEPT, HELP_PIPELINE, HELP_VERSION, HELP_DEFAULT].join('\n')

module.exports = help
39 changes: 39 additions & 0 deletions src/parser-merge-requests.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
utils = require("./utils")

getMergeRequests = (gitlabClient, res, command) ->
if (command.length == 3)
projectId = command[2]
gitlabClient.getMergeRequests(projectId,"") (err, response, body) ->
utils.parseResult(res, err, response, returnMergeRequests, body)
else if (command.length == 4)
projectId = command[2]
gitlabClient.getMergeRequests(projectId,command[3]) (err, response, body) ->
utils.parseResult(res, err, response, returnMergeRequests, body)
else if (command.length == 4)
projectId = command[2]
gitlabClient.getMergeRequests(projectId,command[3]) (err, response, body) ->
utils.parseResult(res, err, response, returnMergeRequests, body)
else if (command.length == 5 && command[3]=="accept")
projectId = command[2]
gitlabClient.acceptMergeRequest(projectId,command[4]) (err, response, body) ->
utils.parseResult(res, err, response, confirmMergeRequest, body)
else
res.reply "Correct usage is gitlab merge requests \<projectId\> \<filter>\ (optional, e.g. state=opened) or gitlab merge requests \<projectId\> accept \<merge iid\>"
return

returnMergeRequests = (res, body)->
data = JSON.parse body
merge_infos = utils.buildListInfo(data, formatMerge)
res.reply "#{data.length} merge requests found" + '\n' + merge_infos.join('\n\n')


formatMerge = (mergeRequest) ->
"- id: #{mergeRequest.iid}, #{mergeRequest.title}, from #{mergeRequest.source_branch} to #{mergeRequest.target_branch}" + '\n' + " state: #{mergeRequest.state.toUpperCase()}, updated at \"#{mergeRequest.updated_at}\", author: #{mergeRequest.author.name}" + '\n' + " upvotes: #{mergeRequest.upvotes}, downvotes: #{mergeRequest.downvotes}" + '\n' + " #{mergeRequest.web_url}"

confirmMergeRequest = (res, body)->
data = JSON.parse body
res.reply "merge request #{data.iid} is now #{data.state}. See #{data.web_url}"



module.exports = getMergeRequests
3 changes: 1 addition & 2 deletions test/parser-branch-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ chai = require 'chai'
expect = chai.expect
nock = require('nock')
co = require('co')

helper = new Helper('../src/gitlab-connector.coffee')


describe 'gitlab branches', ->
describe 'branches', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
Expand Down
4 changes: 2 additions & 2 deletions test/parser-help-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ describe 'gitlab-connector commands without http connection', ->
expect(@room.messages).to.eql [
['bob', '@hubot gitlab help']
['hubot',
'@bob Here are all the available commands:\ngitlab projects searchName - shows the projects whose name contains searchName (optional)\ngitlab pipeline trigger projectId branchName - triggers a pipeline on a branch matching branchName for the project with Id projectId\ngitlab branches projectId - shows the branches for the project with Id projectId\ngitlab version - returns version\ngitlab help - displays all available commands']
'@bob Here are all the available commands:\ngitlab branches projectId - shows the branches for the project whose id is projectId\ngitlab projects searchName - shows the projects whose name contains searchName (optional)\ngitlab merge request projectId filter - shows the merge requests for the project whose id is projectId with filter(optional, e.g. state=opened)\ngitlab merge request projectId accept merge_iid - accepts the merge request iid for the project whose id is projectId\ngitlab pipeline trigger projectId branchName - triggers a pipeline on a branch matching branchName for the project whose id is projectId\ngitlab version - returns version\ngitlab help - displays all available commands']
]
it 'responds to an unkown command', ->
@room.user.say('averell', '@hubot gitlab whatever').then =>
expect(@room.messages).to.eql [
['averell', '@hubot gitlab whatever']
['hubot',
"@averell Sorry, I did not understand command 'whatever'. Here are all the available commands:\ngitlab projects searchName - shows the projects whose name contains searchName (optional)\ngitlab pipeline trigger projectId branchName - triggers a pipeline on a branch matching branchName for the project with Id projectId\ngitlab branches projectId - shows the branches for the project with Id projectId\ngitlab version - returns version\ngitlab help - displays all available commands"]
"@averell Sorry, I did not understand command 'whatever'. Here are all the available commands:\ngitlab branches projectId - shows the branches for the project whose id is projectId\ngitlab projects searchName - shows the projects whose name contains searchName (optional)\ngitlab merge request projectId filter - shows the merge requests for the project whose id is projectId with filter(optional, e.g. state=opened)\ngitlab merge request projectId accept merge_iid - accepts the merge request iid for the project whose id is projectId\ngitlab pipeline trigger projectId branchName - triggers a pipeline on a branch matching branchName for the project whose id is projectId\ngitlab version - returns version\ngitlab help - displays all available commands"]
]
85 changes: 85 additions & 0 deletions test/parser-merge-test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
Helper = require('hubot-test-helper')
chai = require 'chai'
expect = chai.expect
nock = require('nock')
co = require('co')

helper = new Helper('../src/gitlab-connector.coffee')


describe 'merge requests', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
nock('http://gitlab.com')
.get('/api/v4/projects/123/merge_requests')
.reply 200, '[{"iid":68, "title":"Merge 1", "upvotes":0,"downvotes":1,"target_branch":"acceptance","source_branch":"dev", "updated_at":"2018-01-04T16:04:54.598Z", "author":{"name":"Bob"},"web_url":"http://gitlab.com/toto/merge_requests/68", "state": "opened"},{"iid":78, "title":"Merge production","target_branch":"production","source_branch":"acceptance", "upvotes":3,"downvotes":0, "updated_at":"2018-01-05T16:04:54.598Z", "author":{"name":"Jack"},"web_url":"http://gitlab.com/toto/merge_requests/78", "state": "merged"}]'
process.env.HUBOT_GITLAB_URL = "http://gitlab.com"
process.env.HUBOT_GITLAB_TOKEN = "secretToken"
co =>
@room.user.say('alice', '@hubot gitlab merge requests 123')
new Promise((resolve, reject) ->
setTimeout(resolve, 1000)
)
afterEach ->
@room.destroy()
nock.cleanAll()

it 'responds to gitlab merge requests', ->
expect(@room.messages).to.eql [
['alice', '@hubot gitlab merge requests 123']
['hubot',
'@alice 2 merge requests found\n- id: 68, Merge 1, from dev to acceptance\n state: OPENED, updated at \"2018-01-04T16:04:54.598Z\", author: Bob\n upvotes: 0, downvotes: 1\n http://gitlab.com/toto/merge_requests/68\n\n- id: 78, Merge production, from acceptance to production\n state: MERGED, updated at "2018-01-05T16:04:54.598Z", author: Jack\n upvotes: 3, downvotes: 0\n http://gitlab.com/toto/merge_requests/78']
]


describe 'merge requests with filter', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
nock('http://gitlab.com')
.get('/api/v4/projects/123/merge_requests?state=closed')
.reply 200, '[{"iid":68, "title":"Merge 1", "upvotes":0,"downvotes":1,"target_branch":"acceptance","source_branch":"dev", "updated_at":"2018-01-04T16:04:54.598Z", "author":{"name":"Bob"},"web_url":"http://gitlab.com/toto/merge_requests/68", "state": "closed"},{"iid":78, "title":"Merge production","target_branch":"production","source_branch":"acceptance", "upvotes":3,"downvotes":0, "updated_at":"2018-01-05T16:04:54.598Z", "author":{"name":"Jack"},"web_url":"http://gitlab.com/toto/merge_requests/78", "state": "closed"}]'
process.env.HUBOT_GITLAB_URL = "http://gitlab.com"
process.env.HUBOT_GITLAB_TOKEN = "secretToken"
co =>
@room.user.say('alice', '@hubot gitlab merge requests 123 state=closed')
new Promise((resolve, reject) ->
setTimeout(resolve, 1000)
)
afterEach ->
@room.destroy()
nock.cleanAll()

it 'responds to gitlab merge requests', ->
expect(@room.messages).to.eql [
['alice', '@hubot gitlab merge requests 123 state=closed']
['hubot',
'@alice 2 merge requests found\n- id: 68, Merge 1, from dev to acceptance\n state: CLOSED, updated at \"2018-01-04T16:04:54.598Z\", author: Bob\n upvotes: 0, downvotes: 1\n http://gitlab.com/toto/merge_requests/68\n\n- id: 78, Merge production, from acceptance to production\n state: CLOSED, updated at "2018-01-05T16:04:54.598Z", author: Jack\n upvotes: 3, downvotes: 0\n http://gitlab.com/toto/merge_requests/78']
]


describe 'merge requests: accept', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
nock('http://gitlab.com')
.put('/api/v4/projects/123/merge_requests/68/merge')
.reply 200, '{"iid":68, "title":"Merge 1", "upvotes":0,"downvotes":1,"target_branch":"acceptance","source_branch":"dev", "updated_at":"2018-01-04T16:04:54.598Z", "author":{"name":"Bob"},"web_url":"http://gitlab.com/toto/merge_requests/68", "state": "merged"}'
process.env.HUBOT_GITLAB_URL = "http://gitlab.com"
process.env.HUBOT_GITLAB_TOKEN = "secretToken"
co =>
@room.user.say('alice', '@hubot gitlab merge requests 123 accept 68')
new Promise((resolve, reject) ->
setTimeout(resolve, 1000)
)
afterEach ->
@room.destroy()
nock.cleanAll()

it 'responds to gitlab merge requests', ->
expect(@room.messages).to.eql [
['alice', '@hubot gitlab merge requests 123 accept 68']
['hubot',
'@alice merge request 68 is now merged. See http://gitlab.com/toto/merge_requests/68']
]
2 changes: 1 addition & 1 deletion test/parser-pipeline-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ co = require('co')

helper = new Helper('../src/gitlab-connector.coffee')

describe 'gitlab pipeline trigger', ->
describe 'pipeline trigger', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
Expand Down
4 changes: 2 additions & 2 deletions test/parser-project-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ co = require('co')
helper = new Helper('../src/gitlab-connector.coffee')


describe 'gitlab projects', ->
describe 'projects', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
Expand All @@ -32,7 +32,7 @@ describe 'gitlab projects', ->
'@alice 2 projects found.\n- toto, id:123\n Wonderful project\n web url: http://example.com/toto/toto-client, group: totogroup, last activity: 2017-12-07T13:48:40.953Z\n\n\n- toto2, id:246\n Wonderful project returns\n web url: http://example.com/toto/toto-client2, group: totogroup, last activity: 2017-12-09T13:48:40.953Z']
]

describe 'gitlab project search', ->
describe 'project search', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
Expand Down
2 changes: 1 addition & 1 deletion test/parser-version-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ co = require('co')

helper = new Helper('../src/gitlab-connector.coffee')

describe 'gitlab version', ->
describe 'version', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
Expand Down

0 comments on commit f763a67

Please sign in to comment.