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

Commit

Permalink
Add branch display, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
oltruong committed Dec 13, 2017
1 parent 0af3918 commit 2fc9db6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 28 deletions.
9 changes: 2 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
language: node_js
node_js:
- node
- lts/*
- '8'
- '7'
- '6'
node_js: node
sudo: false
cache: yarn
notifications:
Expand All @@ -21,7 +16,7 @@ deploy:
provider: releases
api_key:
secure: UQ710bDILz7B+GKtlFNY/u4jgccy+m2W+ZevAFGzsTedxAMybR6HtOCForrl5G2jUduyirCYR5u10COI/E7qrwDM/aXhC6jrCiHsb4Syhbn3S5F+sLOpiSCRfb++JGbs5urLY8JtOMDyxsn6XYiOHnx8A4cVI78PXcCotC5YZz7WPYPuT1w23qcHC6XFdSuAsJ+khYHVeXxjytmW7uYF6AtuRIwj+H3g+xzUVdUYF+L3T95nQw+0Q3ADlHJpJUUo+Jvp87e2ljYhzORQWKJ35/t3I0eR3VNIAQ71W5qkW0e/bOoE1Zd/zKxx7z8yDLCXLBrUDz/1QtzDZBn17JwsJlAOxTAIvo/kdQj8xepl0vCBFKUqF+FP8aqvZmxvAv05WKskn7DZ2z17CncizLe8T4xoGnXFPbdQGya7dg3Njw0+EKByVWfk24qCcH8i7J2NLH3yEeuTWiyMLbJRYxSOa6U34Oc5ODLp6lsWFQWkZWcd1AGNu5WsK9iMnR8i5BwFE/owV1aHaHK3Nmw6vfL9oPuS+dLoIhLnRxnopHksgp2AAHKcI/nLWvZ3HXVtzS+4eAhZwC2tJHhGlBJdnXuIEqyi835OYoZaT3NcQDWAhMAxm1/uKMf23U0QK4YUm76BqxWFBbNcCYxxJ9Ao1yFJj7RT1RhYw18fk9kHA4i/kss=
file: hubot-gitlab-connector-0.0.1.tgz
file: hubot-gitlab-connector-0.0.2.tgz
on:
repo: oltruong/hubot-gitlab-connector
tags: true
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ Then add **hubot-gitlab-connector** to your `external-scripts.json`:
]
```

Set 2 environment variables
```
HUBOT_GITLAB_URL: url of gitlab server
HUBOT_GITLAB_TOKEN: access token
```
See https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html for access tokens

## Sample Interaction

```
Expand All @@ -34,6 +41,11 @@ For all features, type
hubot gitlab help
```

## Contributing

Granted, the script does not do much for now. If you have any request, please create an issue, or better, propose a pull request.
:)

## NPM Module

https://www.npmjs.com/package/hubot-gitlab-connector
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "hubot-gitlab-connector",
"description": "A hubot script that communicates with Gitlab API",
"version": "0.0.2",
"author": "Olivier Truong <[email protected]",
"author": "Olivier Truong <[email protected]>",
"license": "MIT",
"keywords": "hubot, hubot-scripts, gitlab",
"repository": {
"type": "git",
"url": "git://github.com/hubot-scripts/hubot-gitlab-connector.git"
"url": "git://github.com/oltruong/hubot-gitlab-connector.git"
},
"bugs": {
"url": "https://github.com/hubot-scripts/hubot-gitlab-connector/issues"
"url": "https://github.com/oltruong/hubot-gitlab-connector/issues"
},
"peerDependencies": {
"hubot": "2.x"
Expand Down
41 changes: 25 additions & 16 deletions src/gitlab-connector.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,18 @@ module.exports = (robot) ->
gitlabClient = new GitlabClient(robot, url, token)

switch command[0]
when "pipeline" then pipeline(gitlabClient, res, command)
when "pipeline" then createPipeline(gitlabClient, res, command)
when "branches" then getBranches(gitlabClient, res, command)
when "version" then getVersion(gitlabClient, res)
when "help" then sendHelp(res)
else
sendUnknownCommand(res, command)
sendUnknownCommand(res, res.match[1])


# if command is "project"
#
# else if command is "help"
#
# else

pipeline = (gitlabClient, res, command) ->
createPipeline = (gitlabClient, res, command) ->
if (command.length != 4 || command[1] != 'trigger')
res.reply "Correct usage is gitlab pipeline trigger \<projectId\> \<branch\> "
return

url = process.env.HUBOT_GITLAB_URL
token = process.env.HUBOT_GITLAB_TOKEN

projectId = command[2]
branchName = command[3]

Expand All @@ -70,7 +61,8 @@ pipeline = (gitlabClient, res, command) ->
return

if filter_branch_names.length > 1
res.reply "Sorry #{filter_branch_names.length} branches found for #{branchName}. Please be more specific. Here are the branches #{filter_branch_names}"
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 @@ -102,6 +94,22 @@ pipeline = (gitlabClient, res, command) ->
data2 = JSON.parse body
res.reply "Pipeline #{data2.id} created on branch #{branch}"

getBranches = (gitlabClient, res, command) ->
if (command.length != 2)
res.reply "Correct usage is gitlab branches \<projectId\>"
return
projectId = command[1]
gitlabClient.getBranches(projectId) (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
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')

getVersion = (gitlabClient, res) ->
gitlabClient.version() (err, response, body) ->
Expand All @@ -112,7 +120,7 @@ getVersion = (gitlabClient, res) ->
res.reply "gitlab version is #{data.version}, revision #{data.revision}"

sendHelp = (res) ->
res.reply HELP
res.reply 'Here are all the available commands:' + '\n' + HELP

sendUnknownCommand = (res, command) ->
res.reply "Sorry, I did not understand command '" + command + "'. Here are all the available commands:" + '\n' + HELP
Expand All @@ -121,8 +129,9 @@ 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_BRANCH = "gitlab branches projectId - shows the branches for the project with Id projectId"

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

class GitlabClient
constructor: (@robot, @url, @token) ->
Expand Down
29 changes: 27 additions & 2 deletions test/gitlab-connector-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ describe 'gitlab version', ->
['hubot', '@alice gitlab version is 8.13.0-pre, revision 4e963fe']
]


describe 'gitlab branches', ->
beforeEach ->
@room = helper.createRoom()
nock.disableNetConnect
nock('http://gitlab.com')
.get('/api/v4/projects/123/repository/branches')
.reply 200, '[{"name":"develop", "commit":{"short_id":"c0e0062e","title":"my commit","created_at":"2017-12-13T10:03:59.000+01:00","author_name":"John Doe"} },{"name":"master", "commit":{"short_id":"c0e0062f","title":"my first commit","created_at":"2017-12-01T10:03:59.000+01:00","author_name":"Henry Doe"}}]'
process.env.HUBOT_GITLAB_URL = "http://gitlab.com"
process.env.HUBOT_GITLAB_TOKEN = "secretToken"
co =>
@room.user.say('alice', '@hubot gitlab branches 123')
new Promise((resolve, reject) ->
setTimeout(resolve, 1000);
)
afterEach ->
@room.destroy()
nock.cleanAll()

it 'responds to 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"']
]

describe 'gitlab-connector commands without http connection', ->
beforeEach ->
@room = helper.createRoom()
Expand All @@ -75,12 +100,12 @@ describe 'gitlab-connector commands without http connection', ->
expect(@room.messages).to.eql [
['bob', '@hubot gitlab help']
['hubot',
'@bob gitlab pipeline trigger projectId branchName - triggers a pipeline on a branch matching branchName for the project with Id projectId\ngitlab version - returns version\ngitlab help - displays all available commands']
'@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']
]
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 version - returns version\ngitlab help - displays all available commands']
"@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"]
]

0 comments on commit 2fc9db6

Please sign in to comment.