-
Notifications
You must be signed in to change notification settings - Fork 0
/
issues.coffee
177 lines (155 loc) · 6.2 KB
/
issues.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Description:
# Allows Hubot to view/reply issues and add to organization
#
# Dependencies:
# "githubot": "0.4.x"
# "hubot-conversation": "^1.1.1"
# "es6-promise": "^4.1.0",
#
# Configuration:
# HUBOT_GITHUB_REPO
# The `user/repository` that you want to connect to. example: github/hubot-scripts
# HUBOT_GITHUB_USER
# The `user` that you want to connect to. example: github
# HUBOT_GITHUB_TOKEN
# You can retrieve your github token via:
# curl -i https://api.github.com/issues -d '{"scopes":["repo"]}' -u "yourusername"
# Enter your Github password when prompted. When you get a response, look for the "token" value
#
# Commands:
# hubot acciones git hub con issues
# |
# -> listar
# |
# -> ver issue <number>
# |
# -> aceptar
# |
# -> rechazar
# |
# -> responder
# |
# -> ver usuarios
# |
# -> salir
#
# Author: Julian Pérez Sampedro
endPointGitHub = "https://api.github.com/repos/BEEVA-bots-poc/access"
module.exports = (robot) ->
github = require('githubot')(robot)
conversation = require('hubot-conversation');
#Initialize Conversation in bot
switchBoard = new conversation(robot);
robot.respond /acciones git hub con issues/, (msg) ->
showDialog(msg, true)
#Initialize a dialog with bot
showDialog = (msg, initial) ->
dialog = switchBoard.startDialog(msg);
if initial
msg.reply "¿Buenos días, que deseas hacer con las issues?"
else
msg.reply "Deseas salir o realizar alguna petición más?"
dialog.addChoice /listar/i, (msg2) ->
viewIssue(msg2)
dialog.addChoice /ver issue (.*)/i, (msg2) ->
numberIssue = msg2.match[1]
viewSpecificIssue(msg2, numberIssue)
dialog.addChoice /ver usuarios/, (msg3) ->
viewUsersInSystem(msg3)
dialog.addChoice /salir/, (msg4) ->
msg4.reply "Un placer asistirte. Good bye!"
#Initialize a dialog with bot
showDialogIssue = (msg, numberIssue, issue) ->
dialog = switchBoard.startDialog(msg);
msg.reply "Deseas Aceptar, Responder, Cerrar u otras opciones disponibles?"
dialog.addChoice /aceptar/i, (msg1) ->
acceptIssue(msg1, numberIssue, issue)
dialog.addChoice /responder/i, (msg2) ->
respondIssueDialog(msg2, numberIssue)
dialog.addChoice /cerrar/, (msg3) ->
closedIssue(msg3, numberIssue)
dialog.addChoice /otras (.*)/, (msg4) ->
showDialog(msg4)
#Method to view all issues
viewIssue = (msg) ->
getIssues(null, {}).then (issues) ->
issues = issues.sort (a,b) -> a.number > b.number
texts = []
for i in issues
texts.push "[#{i.number}] #{i.title} realizada por #{i.user.login} con el contenido #{i.body}"
text = texts.join '\n'
msg.send "Tus issues son:\n#{text}"
showDialog(msg)
#Method to close issue
acceptIssue = (msg, numberIssue, issue) ->
github.get "https://api.github.com/users/#{issue.user.login}", {}, (userInfo) ->
if !userInfo.email
respondIssue(msg, numberIssue, "Debes rellenar tu email")
else
msg.http('https://raw.githubusercontent.com/BEEVA-bots-poc/access/master/CONTRIBUTORS.json').get() (err, httpRes, body) ->
users = JSON.parse body
users.contributors.push ({"name":userInfo.name,"email":userInfo.email,"git":userInfo.login})
github.get "#{endPointGitHub}/contents/CONTRIBUTORS.json", {}, (infoCommit) ->
contentCommit = new Buffer(JSON.stringify users).toString('base64')
param = {
message: "Added new User to repo",
content: contentCommit,
sha: infoCommit.sha
}
github.put "#{endPointGitHub}/contents/CONTRIBUTORS.json", param, (issue) ->
github.patch "#{endPointGitHub}/issues/#{numberIssue}", {state: "closed"}, (issue, error) ->
msg.send "OK. He aceptado la issue #{numberIssue} y he añadido el usuario al contributors"
#Method to close issue
closedIssue = (msg, numberIssue) ->
github.patch "#{endPointGitHub}/issues/#{numberIssue}", {state: "closed"}, (issue, error) ->
if error
msg.send "Error, por favor intentalo más tarde"
showDialog(msg)
else
msg.send "Issue número: #{numberIssue} cerrada."
#Method to responde issue to user
respondIssueDialog = (msg, numberIssue) ->
dialog = switchBoard.startDialog(msg);
msg.reply "¿Qué deseas contestar al usuario?"
dialog.addChoice /(.*)/i, (msg1) ->
text = msg1.match[1]
msg1.reply "¿Estás seguro de la respuesta?: #{text}"
dialog.addChoice /si/, (msg2) ->
respondIssue(msg2, numberIssue, text)
dialog.addChoice /no/, (msg3) ->
msg1.reply "¿Deseas salir o volver a escribir?"
dialog.addChoice /volver (.*)/, (msg2) ->
respondIssueDialog(msg2, numberIssue)
dialog.addChoice /salir/, (msg3) ->
showDialog(msg3)
#Method to view all users registered in repo
respondIssue = (msg, numberIssue, text) ->
param = {
body: text
}
msg.send "#{numberIssue}"
github.post "#{endPointGitHub}/issues/#{numberIssue}/comments", param, (issue, error) ->
if error
msg.send "Error, por favor intentalo más tarde"
showDialog(msg)
else
msg.send "Contestada la issue #{numberIssue}"
showDialog(msg)
#Method to view all users registered in repo
viewUsersInSystem = (msg) ->
github.get "#{endPointGitHub}/contents/CONTRIBUTORS.json", {}, (contributors) ->
content = JSON.parse new Buffer(contributors.content, 'base64').toString()
for i in content.contributors
msg.send "Usuario del sistema: #{i.name}"
showDialog(msg)
#Method to view specific issue and user can do actions with specific issue
viewSpecificIssue = (msg, numberIssue) ->
getIssues(numberIssue, {}).then (issue) ->
msg.send "[#{issue.number}] #{issue.title} realizada por #{issue.user.login} con el contenido #{issue.body}\n"
showDialogIssue(msg, numberIssue, issue)
#Method get issues (all or specific issue)
getIssues = (numberIssue, state) ->
return new Promise (resolve, reject) ->
number = if numberIssue then "/#{numberIssue}" else ""
return github.get "#{endPointGitHub}/issues#{number}", state, (issue) ->
resolve issue