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

Commit

Permalink
add all projects feature
Browse files Browse the repository at this point in the history
  • Loading branch information
oltruong committed Dec 28, 2017
1 parent 11da565 commit 84ecfc8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A hubot script that communicates with Gitlab
See [`src/gitlab-connector.coffee`](src/gitlab-connector.coffee) for full documentation.

## Features
- Show all projects
- Search projects by name
- Display branches of a given project
- Trigger a pipeline
Expand Down
44 changes: 27 additions & 17 deletions src/gitlab-connector.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ findRightBranch = (res, gitlabClient, project, branchName, err, response, body)

filter_branch_names = (item for item in branch_names when item.indexOf(branchName) != -1)
if filter_branch_names.length == 0
branch_names_info=branch_names.join('\n')
branch_names_info = branch_names.join('\n')
res.reply "Sorry no branch found for #{branchName}. Here are the branches" + '\n' + "#{branch_names_info}"
return

Expand Down Expand Up @@ -115,23 +115,30 @@ parseTrigger = (res, project, branch, err, response, body) ->
res.reply "Pipeline #{data.id} created on branch #{branch} of project #{project.name}. See #{project.web_url}/pipelines/#{data.id}"

getProjects = (gitlabClient, res, command) ->
if (command.length != 2)
if (command.length == 1)
gitlabClient.findProjects() (err, response, body) ->
readProjects(res, err, response, body)
else if (command.length == 2)
searchName = command[1]
gitlabClient.findFilteredProjects(searchName) (err, response, body) ->
readProjects(res, err, response, body)
else
res.reply "Correct usage is gitlab projects \<searchName\>"
return
searchName = command[1]
gitlabClient.findProjects(searchName) (err, response, body) ->
if err
res.send "Encountered an error :( #{err}"
return
if response.statusCode isnt 200
res.send "Request didn't come back HTTP 200 :( #{response.statusCode} #{body}"
return
data = JSON.parse body
project_info = buildListInfo(data,formatProject)
res.reply "#{data.length} projects found matching name #{searchName}" + '\n' + project_info.join('\n\n\n')

readProjects = (res, err, response, body)->
if err
res.send "Encountered an error :( #{err}"
return
if response.statusCode isnt 200
res.send "Request didn't come back HTTP 200 :( #{response.statusCode} #{body}"
return
data = JSON.parse body
project_info = buildListInfo(data, formatProject)
res.reply "#{data.length} projects found." + '\n' + project_info.join('\n\n\n')

formatProject = (project) ->
"- #{project.name}, id:#{project.id}" + '\n' + " #{project.description}"+ '\n' + " web url: #{project.web_url}, group: #{project.namespace.name}, last activity: #{project.last_activity_at}"
"- #{project.name}, id:#{project.id}" + '\n' + " #{project.description}" + '\n' + " web url: #{project.web_url}, group: #{project.namespace.name}, last activity: #{project.last_activity_at}"


getBranches = (gitlabClient, res, command) ->
Expand All @@ -147,7 +154,7 @@ getBranches = (gitlabClient, res, command) ->
res.send "Request didn't come back HTTP 200 :( #{response.statusCode} #{body}"
return
data = JSON.parse body
branch_infos = buildListInfo(data,formatBranch)
branch_infos = buildListInfo(data, formatBranch)
res.reply "#{data.length} branches found" + '\n' + branch_infos.join('\n\n')

buildListInfo = (data, callback) ->
Expand Down Expand Up @@ -181,7 +188,7 @@ sendUnknownCommand = (res, command) ->
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"
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 = [HELP_PROJECT, HELP_PIPELINE, HELP_BRANCH, HELP_VERSION, HELP_DEFAULT].join('\n')
Expand All @@ -201,9 +208,12 @@ class GitlabClient
getProject: (projectId) ->
request.call(this).path('/api/v4/projects/' + projectId).get()

findProjects: (searchName) ->
findFilteredProjects: (searchName) ->
request.call(this).path('/api/v4/projects?search=' + searchName).get()

findProjects: () ->
request.call(this).path('/api/v4/projects').get()

getBranches: (projectId) ->
request.call(this).path('/api/v4/projects/' + projectId + '/repository/branches').get()

Expand Down
35 changes: 30 additions & 5 deletions test/gitlab-connector-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,32 @@ describe 'gitlab branches', ->
'@alice 2 branches found\n- develop\n last commit \"c0e0062e\", title \"my commit\" by \"John Doe\" created at \"2017-12-13T10:03:59.000+01:00\"\n\n- master\n last commit \"c0e0062f\", title \"my first commit\" by \"Henry Doe\" created at \"2017-12-01T10:03:59.000+01:00\"']
]

describe 'gitlab project', ->
describe 'gitlab projects', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
nock('http://gitlab.com')
.get('/api/v4/projects')
.reply 200, '[{"id":123,"name":"toto", "description":"Wonderful project", "web_url": "http://example.com/toto/toto-client", "namespace":{"name":"totogroup"}, "last_activity_at": "2017-12-07T13:48:40.953Z"},{"id":246,"name":"toto2", "description":"Wonderful project returns", "web_url": "http://example.com/toto/toto-client2", "namespace":{"name":"totogroup"}, "last_activity_at": "2017-12-09T13:48:40.953Z"}]'
process.env.HUBOT_GITLAB_URL = "http://gitlab.com"
process.env.HUBOT_GITLAB_TOKEN = "secretToken"
co =>
@room.user.say('alice', '@hubot gitlab projects')
new Promise((resolve, reject) ->
setTimeout(resolve, 1000);
)
afterEach ->
@room.destroy()
nock.cleanAll()

it 'responds to gitlab projects', ->
expect(@room.messages).to.eql [
['alice', '@hubot gitlab projects']
['hubot',
'@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', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
Expand All @@ -111,11 +136,11 @@ describe 'gitlab project', ->
@room.destroy()
nock.cleanAll()

it 'responds to gitlab projects', ->
it 'responds to gitlab projects search', ->
expect(@room.messages).to.eql [
['alice', '@hubot gitlab projects toto']
['hubot',
'@alice 2 projects found matching name toto\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']
'@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-connector commands without http connection', ->
Expand All @@ -130,12 +155,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\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 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']
]
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\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 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"]
]

0 comments on commit 84ecfc8

Please sign in to comment.