github -> gogs

This commit is contained in:
Michele Guerini Rocco 2018-06-08 08:57:39 +02:00
parent 2210914740
commit ec09b5f00b
6 changed files with 53 additions and 37 deletions

View File

@ -9,9 +9,8 @@
"chai": "^2.3.0", "chai": "^2.3.0",
"cheerio": "^0.19.0", "cheerio": "^0.19.0",
"coffee-coverage": "^0.4.6", "coffee-coverage": "^0.4.6",
"coffee-script": "^1.9.2", "coffeescript": "^1.9.2",
"fast-levenshtein": "^1.0.6", "fast-levenshtein": "^1.0.6",
"github": "^0.2.4",
"hubot": "^2.12.0", "hubot": "^2.12.0",
"hubot-bitcoin": "^1.0.3", "hubot-bitcoin": "^1.0.3",
"hubot-diagnostics": "0.0.1", "hubot-diagnostics": "0.0.1",
@ -26,7 +25,7 @@
"hubot-rules": "^0.1.0", "hubot-rules": "^0.1.0",
"hubot-scripts": "^2.5.16", "hubot-scripts": "^2.5.16",
"hubot-shipit": "^0.2.0", "hubot-shipit": "^0.2.0",
"hubot-matrix": "rnhmjoj/hubot-matrix", "hubot-matrix": "git+https://maxwell.ydns.eu/git/rnhmjoj/hubot-matrix.git",
"hubot-youtube": "^0.1.2", "hubot-youtube": "^0.1.2",
"mocha": "^3.2.0", "mocha": "^3.2.0",
"moment": "^2.10.2", "moment": "^2.10.2",

View File

@ -1,9 +1,14 @@
# Description: # Description:
# interazioni tra asjon e github # interazioni tra asjon e git
# #
# Requires: # Dependencies:
# "github": "0.2.4" # None
# #
# Configuration:
# HUBOT_GIT_URL - git server API base url
# HUBOT_GIT_TOKEN - Gogs v1 or GitHub v3 API token
# HUBOT_GIT_REPO - repository name (owner/repo)
#
# Commands: # Commands:
# asjon mostra le issue - mostra le issue aperte su rnhmjoj/asjon # asjon mostra le issue - mostra le issue aperte su rnhmjoj/asjon
# #
@ -11,14 +16,11 @@
# Enrico Fasoli (fazo96) # Enrico Fasoli (fazo96)
# Michele Guerini Rocco (rnhmjoj) # Michele Guerini Rocco (rnhmjoj)
GitHubAPI = require 'github'
github = new GitHubAPI version: '3.0.0'
module.exports = (robot) -> module.exports = (robot) ->
githubhook = (req, res) -> githook = (req, res) ->
res.send 200 res.send 200
dest = name: req.params.name, room: id: req.params.room dest = name: req.params.name, room: id: req.params.room
robot.emit 'githubhook', req.body, req.params robot.emit 'githook', req.body, req.params
s = "branch #{req.body.ref} aggiornato!\n" s = "branch #{req.body.ref} aggiornato!\n"
cm = req.body.commits.map (c) -> cm = req.body.commits.map (c) ->
[c.committer.username, c.message].join ' -> ' [c.committer.username, c.message].join ' -> '
@ -26,13 +28,23 @@ module.exports = (robot) ->
unless process.env.TESTING_ASJON unless process.env.TESTING_ASJON
# Disabilito http route durante i test # Disabilito http route durante i test
robot.router.post '/hubot/githubhook/:room/:name?', githubhook robot.router.post '/hubot/githook/:room/:name?', githook
robot.respond /(?:(?:mostra(?:mi)?|fammi vedere) )?(?:le )?issue(?:s)?/i, (res) -> robot.respond /(?:(?:mostra(?:mi)?|fammi vedere) )?(?:le )?issue(?:s)?/i, (res) ->
msg = state: 'open', user: 'rnhmjoj', repo: 'asjon', sort: 'updated' msg = state: 'open', user: 'rnhmjoj', repo: 'asjon', sort: 'updated'
res.send 'controllo issues...' res.send 'controllo issues...'
github.issues.repoIssues msg, (err,data) -> url = process.env.HUBOT_GIT_URL
repo = process.env.HUBOT_GIT_REPO
token = process.env.HUBOT_GIT_TOKEN
if not token
return res.send 'non ho il token per la repo'
robot.http("#{url}/repos/#{repo}/issues?state=open&sort=updated")
.header('Authorization', 'token '+token)
.get() (err, r, body) ->
if err then return res.send err if err then return res.send err
data = JSON.parse body
if data.length is 0 then return res.send '0 issues' if data.length is 0 then return res.send '0 issues'
r = data.map (i) -> r = data.map (i) ->
labels = i.labels.map((x) -> x.name).join ', ' labels = i.labels.map((x) -> x.name).join ', '
@ -41,13 +53,15 @@ module.exports = (robot) ->
res.send r.join '\n' res.send r.join '\n'
robot.respond /linkami (?:la )?issue (?:(?:n(?:°)?(?: )?)|numero )?(\d+)/i, (res) -> robot.respond /linkami (?:la )?issue (?:(?:n(?:°)?(?: )?)|numero )?(\d+)/i, (res) ->
base = 'http://github.com/rnhmjoj/asjon/issues/' url = process.env.HUBOT_GIT_URL
repo = process.env.HUBOT_GIT_REPO
base = "#{url}/#{repo}/issues/"
res.send base+res.match[1] res.send base+res.match[1]
robot.respond /linkami (?:la )?repo (\w+\/\w+)/i, (res) -> robot.respond /linkami (?:la )?repo (\w+\/\w+)/i, (res) ->
res.send 'https://github.com/'+res.match[1] res.send "#{url}/#{res.match[1]}/"
# rendo l'handler dell'hook di github accessibile # rendo l'handler dell'hook di git accessibile
# in caso serve (nei test) # in caso serve (nei test)
return githubhook return githook

View File

@ -85,7 +85,7 @@ module.exports = (robot) ->
runCmd ssh, res, -> runCmd ssh, res, ->
res.send "connessione chiusa. reverse shell terminata" res.send "connessione chiusa. reverse shell terminata"
robot.on 'githubhook', (data,params) -> robot.on 'githook', (data,params) ->
if data.ref is 'refs/heads/master' and process.env.AUTO_KILL_ON_UPDATE if data.ref is 'refs/heads/master' and process.env.AUTO_KILL_ON_UPDATE
dest = name: params.name, room: params.room dest = name: params.name, room: params.room
res = send: (x) -> robot.send dest, x res = send: (x) -> robot.send dest, x

View File

@ -929,7 +929,7 @@
} }
}, },
{ {
"filename": "scripts/github.coffee", "filename": "scripts/git.coffee",
"coverage": 10.344827586206897, "coverage": 10.344827586206897,
"hits": 3, "hits": 3,
"misses": 26, "misses": 26,
@ -1016,7 +1016,7 @@
"coverage": "" "coverage": ""
}, },
"21": { "21": {
"source": " robot.router.post '/hubot/githubhook/:room/:name?', (req, res) ->", "source": " robot.router.post '/hubot/githook/:room/:name?', (req, res) ->",
"coverage": 0 "coverage": 0
}, },
"22": { "22": {
@ -1028,7 +1028,7 @@
"coverage": 0 "coverage": 0
}, },
"24": { "24": {
"source": " robot.emit 'githubhook', req.body, req.params", "source": " robot.emit 'githook', req.body, req.params",
"coverage": 0 "coverage": 0
}, },
"25": { "25": {
@ -2541,7 +2541,7 @@
"coverage": "" "coverage": ""
}, },
"60": { "60": {
"source": " robot.on 'githubhook', (data,params) ->", "source": " robot.on 'githook', (data,params) ->",
"coverage": 1 "coverage": 1
}, },
"61": { "61": {

View File

@ -3,17 +3,20 @@ expect = require("chai").should()
Asjon = require './asjon-testing.coffee' Asjon = require './asjon-testing.coffee'
asjon = undefined asjon = undefined
githubhook = undefined githook = undefined
describe 'modulo github', -> describe 'modulo git', ->
before (done) -> before (done) ->
# Inizializzo robot # Inizializzo robot
Asjon (assa) -> Asjon (assa) ->
asjon = assa asjon = assa
after asjon.after after asjon.after
afterEach asjon.clear afterEach asjon.clear
githubhook = require('../scripts/github.coffee')(asjon.robot) githook = require('../scripts/git.coffee')(asjon.robot)
done() done()
process.env.HUBOT_GIT_URL = 'https://git.example.com'
process.env.HUBOT_GIT_REPO = 'owner/asjon'
process.env.HUBOT_GIT_TOKEN = 'secret'
it 'dovrebbe rispondere a "mostra le issues"', (done) -> it 'dovrebbe rispondere a "mostra le issues"', (done) ->
questions = [ questions = [
@ -23,8 +26,8 @@ describe 'modulo github', ->
"asjon le issue" "asjon le issue"
"asjon issue" "asjon issue"
] ]
nock('https://api.github.com') nock('https://git.example.com')
.get('/repos/fazo96/asjon/issues?state=open&sort=updated') .get('/repos/owner/asjon/issues?state=open&sort=updated')
.times(questions.length) .times(questions.length)
.reply 200, [] .reply 200, []
acc = 0 acc = 0
@ -34,8 +37,8 @@ describe 'modulo github', ->
questions.map (q) -> asjon.send q questions.map (q) -> asjon.send q
it 'dovrebbe rispondere correttamente in caso di 0 issues', (done) -> it 'dovrebbe rispondere correttamente in caso di 0 issues', (done) ->
nock('https://api.github.com') nock('https://git.example.com')
.get('/repos/rnhmjoj/asjon/issues?state=open&sort=updated') .get('/repos/owner/asjon/issues?state=open&sort=updated')
.reply 200, [] .reply 200, []
acc = 0 acc = 0
asjon.receive (e,l) -> asjon.receive (e,l) ->
@ -62,8 +65,8 @@ describe 'modulo github', ->
login: 'user' login: 'user'
it 'dovrebbe rispondere correttamente in caso di 1 o più issues', (done) -> it 'dovrebbe rispondere correttamente in caso di 1 o più issues', (done) ->
nock('https://api.github.com') nock('https://git.example.com')
.get('/repos/rnhmjoj/asjon/issues?state=open&sort=updated') .get('/repos/owner/asjon/issues?state=open&sort=updated')
.reply 200, [issue1, issue2] .reply 200, [issue1, issue2]
acc = 0 acc = 0
asjon.receive (e,l) -> asjon.receive (e,l) ->
@ -81,16 +84,16 @@ describe 'modulo github', ->
acc = 0 acc = 0
asjon.receive (e,l) -> asjon.receive (e,l) ->
if acc is 0 if acc is 0
l.join().should.equal 'http://github.com/rnhmjoj/asjon/issues/5' l.join().should.equal 'https://git.example.com/owner/asjon/issues/5'
else else
l.join().should.equal 'http://github.com/rnhmjoj/asjon/issues/456' l.join().should.equal 'https://git.example.com/owner/asjon/issues/456'
acc++ acc++
if acc is 2 then done() if acc is 2 then done()
asjon.send 'asjon linkami la issue numero 5' asjon.send 'asjon linkami la issue numero 5'
asjon.send 'asjon linkami issue 456' asjon.send 'asjon linkami issue 456'
it 'dovrebbe rendere disponibile l\'handler del webhook', -> it 'dovrebbe rendere disponibile l\'handler del webhook', ->
githubhook.should.not.be.undefined githook.should.not.be.undefined
it 'dovrebbe informare correttamente riguardo gli aggiornamenti', (done) -> it 'dovrebbe informare correttamente riguardo gli aggiornamenti', (done) ->
acc = 0 acc = 0
@ -110,10 +113,10 @@ describe 'modulo github', ->
l.join().should.equal 'branch '+req.body.ref+' aggiornato!\ntest -> commit' l.join().should.equal 'branch '+req.body.ref+' aggiornato!\ntest -> commit'
acc++ acc++
if acc is 2 then done() if acc is 2 then done()
githubhook req, res githook req, res
req.body.ref = 'refs/heads/master' req.body.ref = 'refs/heads/master'
req.body.commits.push req.body.commits.push
committer: committer:
username: 'test' username: 'test'
message: 'commit' message: 'commit'
githubhook req, res githook req, res

View File

@ -88,4 +88,4 @@ describe 'modulo shell', ->
if acc is 3 if acc is 3
done() done()
process.env.AUTO_KILL_ON_UPDATE = 'true' process.env.AUTO_KILL_ON_UPDATE = 'true'
asjon.robot.emit 'githubhook', data, params asjon.robot.emit 'githook', data, params