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

Commit

Permalink
Add search by project
Browse files Browse the repository at this point in the history
  • Loading branch information
oltruong committed Dec 27, 2017
1 parent d34d068 commit 0432cec
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ A hubot script that communicates with Gitlab

See [`src/gitlab-connector.coffee`](src/gitlab-connector.coffee) for full documentation.

## Features
- Search projects by name
- Display branches of a given project
- Trigger a pipeline
- Display version

## Installation

In hubot project repo, run:
Expand Down
46 changes: 40 additions & 6 deletions src/gitlab-connector.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = (robot) ->

switch command[0]
when "pipeline" then createPipeline(gitlabClient, res, command)
when "projects" then getProjects(gitlabClient, res, command)
when "branches" then getBranches(gitlabClient, res, command)
when "version" then getVersion(gitlabClient, res)
when "help" then sendHelp(res)
Expand Down Expand Up @@ -69,12 +70,13 @@ findRightBranch = (res, gitlabClient, project, branchName, err, response, body)
branch_names.push branch.name for branch in data

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

if filter_branch_names.length > 1
filter_branch_info = filter_branch_names.join('\n')
res.reply "Sorry #{filter_branch_names.length} branches found for #{branchName}. Please be more specific. Here are the branches" + '\n' + "#{filter_branch_info}"
return
branch = filter_branch_names[0]
Expand Down Expand Up @@ -112,6 +114,26 @@ parseTrigger = (res, project, branch, err, response, body) ->
data = JSON.parse 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)
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')

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}"


getBranches = (gitlabClient, res, command) ->
if (command.length != 2)
res.reply "Correct usage is gitlab branches \<projectId\>"
Expand All @@ -125,9 +147,17 @@ getBranches = (gitlabClient, res, command) ->
res.send "Request didn't come back HTTP 200 :( #{response.statusCode} #{body}"
return
data = JSON.parse body
branch_infos = []
branch_infos.push "#{branch.name}, last commit \"#{branch.commit.short_id}\", title \"#{branch.commit.title}\" by \"#{branch.commit.author_name}\" created at \"#{branch.commit.created_at}\"" for branch in data
res.reply "#{data.length} branches found" + '\n' + branch_infos.join('\n')
branch_infos = buildListInfo(data,formatBranch)
res.reply "#{data.length} branches found" + '\n' + branch_infos.join('\n\n')

buildListInfo = (data, callback) ->
list = []
list.push callback(branch) for branch in data
return list

formatBranch = (branch) ->
"#{branch.name}" + '\n' + " last commit \"#{branch.commit.short_id}\", title \"#{branch.commit.title}\" by \"#{branch.commit.author_name}\" created at \"#{branch.commit.created_at}\""


getVersion = (gitlabClient, res) ->
gitlabClient.version() (err, response, body)->
Expand All @@ -151,9 +181,10 @@ 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_BRANCH = "gitlab branches projectId - shows the branches for the project with Id projectId"

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

class GitlabClient
constructor: (@robot, @url, @token) ->
Expand All @@ -170,6 +201,9 @@ class GitlabClient
getProject: (projectId) ->
request.call(this).path('/api/v4/projects/' + projectId).get()

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

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

Expand Down
32 changes: 29 additions & 3 deletions test/gitlab-connector-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe 'gitlab version', ->
new Promise((resolve, reject) ->
setTimeout(resolve, 1000);
)

afterEach ->
@room.destroy()
nock.cleanAll()
Expand Down Expand Up @@ -89,7 +90,32 @@ describe 'gitlab branches', ->
expect(@room.messages).to.eql [
['alice', '@hubot gitlab branches 123']
['hubot',
'@alice 2 branches found\ndevelop, last commit "c0e0062e", title "my commit" by "John Doe" created at "2017-12-13T10:03:59.000+01:00"\nmaster, last commit "c0e0062f", title "my first commit" by "Henry Doe" created at "2017-12-01T10:03:59.000+01:00"']
'@alice 2 branches found\ndevelop\n last commit \"c0e0062e\", title \"my commit\" by \"John Doe\" created at \"2017-12-13T10:03:59.000+01:00\"\n\nmaster\n last commit \"c0e0062f\", title \"my first commit\" by \"Henry Doe\" created at \"2017-12-01T10:03:59.000+01:00\"']
]

describe 'gitlab project', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
nock('http://gitlab.com')
.get('/api/v4/projects?search=toto')
.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 toto')
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 toto']
['hubot',
'@alice 2 projects found matching name toto\ntoto, id:123\nWonderful project\n web url: http://example.com/toto/toto-client, group: totogroup, last activity: 2017-12-07T13:48:40.953Z\n\ntoto2, id:246\nWonderful 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 @@ -104,12 +130,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 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\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 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\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 0432cec

Please sign in to comment.