From ec09b5f00b9eaeeeb7e9aa6bb2e72aa361fddc78 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Fri, 8 Jun 2018 08:57:39 +0200 Subject: [PATCH] github -> gogs --- package.json | 5 +-- scripts/{github.coffee => git.coffee} | 42 +++++++++++++------- scripts/shell.coffee | 2 +- test/coverage.json | 8 ++-- test/{github-test.coffee => git-test.coffee} | 31 ++++++++------- test/shell-test.coffee | 2 +- 6 files changed, 53 insertions(+), 37 deletions(-) rename scripts/{github.coffee => git.coffee} (57%) rename test/{github-test.coffee => git-test.coffee} (77%) diff --git a/package.json b/package.json index 7193b6b..3f00116 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,8 @@ "chai": "^2.3.0", "cheerio": "^0.19.0", "coffee-coverage": "^0.4.6", - "coffee-script": "^1.9.2", + "coffeescript": "^1.9.2", "fast-levenshtein": "^1.0.6", - "github": "^0.2.4", "hubot": "^2.12.0", "hubot-bitcoin": "^1.0.3", "hubot-diagnostics": "0.0.1", @@ -26,7 +25,7 @@ "hubot-rules": "^0.1.0", "hubot-scripts": "^2.5.16", "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", "mocha": "^3.2.0", "moment": "^2.10.2", diff --git a/scripts/github.coffee b/scripts/git.coffee similarity index 57% rename from scripts/github.coffee rename to scripts/git.coffee index a602300..9ea5fc6 100644 --- a/scripts/github.coffee +++ b/scripts/git.coffee @@ -1,9 +1,14 @@ # Description: -# interazioni tra asjon e github +# interazioni tra asjon e git # -# Requires: -# "github": "0.2.4" +# Dependencies: +# 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: # asjon mostra le issue - mostra le issue aperte su rnhmjoj/asjon # @@ -11,14 +16,11 @@ # Enrico Fasoli (fazo96) # Michele Guerini Rocco (rnhmjoj) -GitHubAPI = require 'github' -github = new GitHubAPI version: '3.0.0' - module.exports = (robot) -> - githubhook = (req, res) -> + githook = (req, res) -> res.send 200 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" cm = req.body.commits.map (c) -> [c.committer.username, c.message].join ' -> ' @@ -26,13 +28,23 @@ module.exports = (robot) -> unless process.env.TESTING_ASJON # 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) -> msg = state: 'open', user: 'rnhmjoj', repo: 'asjon', sort: 'updated' 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 + data = JSON.parse body if data.length is 0 then return res.send '0 issues' r = data.map (i) -> labels = i.labels.map((x) -> x.name).join ', ' @@ -41,13 +53,15 @@ module.exports = (robot) -> res.send r.join '\n' 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] 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) - return githubhook + return githook diff --git a/scripts/shell.coffee b/scripts/shell.coffee index a7a86f8..4060017 100644 --- a/scripts/shell.coffee +++ b/scripts/shell.coffee @@ -85,7 +85,7 @@ module.exports = (robot) -> runCmd ssh, res, -> 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 dest = name: params.name, room: params.room res = send: (x) -> robot.send dest, x diff --git a/test/coverage.json b/test/coverage.json index 4f543ae..1e34472 100644 --- a/test/coverage.json +++ b/test/coverage.json @@ -929,7 +929,7 @@ } }, { - "filename": "scripts/github.coffee", + "filename": "scripts/git.coffee", "coverage": 10.344827586206897, "hits": 3, "misses": 26, @@ -1016,7 +1016,7 @@ "coverage": "" }, "21": { - "source": " robot.router.post '/hubot/githubhook/:room/:name?', (req, res) ->", + "source": " robot.router.post '/hubot/githook/:room/:name?', (req, res) ->", "coverage": 0 }, "22": { @@ -1028,7 +1028,7 @@ "coverage": 0 }, "24": { - "source": " robot.emit 'githubhook', req.body, req.params", + "source": " robot.emit 'githook', req.body, req.params", "coverage": 0 }, "25": { @@ -2541,7 +2541,7 @@ "coverage": "" }, "60": { - "source": " robot.on 'githubhook', (data,params) ->", + "source": " robot.on 'githook', (data,params) ->", "coverage": 1 }, "61": { diff --git a/test/github-test.coffee b/test/git-test.coffee similarity index 77% rename from test/github-test.coffee rename to test/git-test.coffee index 0c64007..e30a92d 100644 --- a/test/github-test.coffee +++ b/test/git-test.coffee @@ -3,17 +3,20 @@ expect = require("chai").should() Asjon = require './asjon-testing.coffee' asjon = undefined -githubhook = undefined +githook = undefined -describe 'modulo github', -> +describe 'modulo git', -> before (done) -> # Inizializzo robot Asjon (assa) -> asjon = assa after asjon.after afterEach asjon.clear - githubhook = require('../scripts/github.coffee')(asjon.robot) + githook = require('../scripts/git.coffee')(asjon.robot) 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) -> questions = [ @@ -23,8 +26,8 @@ describe 'modulo github', -> "asjon le issue" "asjon issue" ] - nock('https://api.github.com') - .get('/repos/fazo96/asjon/issues?state=open&sort=updated') + nock('https://git.example.com') + .get('/repos/owner/asjon/issues?state=open&sort=updated') .times(questions.length) .reply 200, [] acc = 0 @@ -34,8 +37,8 @@ describe 'modulo github', -> questions.map (q) -> asjon.send q it 'dovrebbe rispondere correttamente in caso di 0 issues', (done) -> - nock('https://api.github.com') - .get('/repos/rnhmjoj/asjon/issues?state=open&sort=updated') + nock('https://git.example.com') + .get('/repos/owner/asjon/issues?state=open&sort=updated') .reply 200, [] acc = 0 asjon.receive (e,l) -> @@ -62,8 +65,8 @@ describe 'modulo github', -> login: 'user' it 'dovrebbe rispondere correttamente in caso di 1 o più issues', (done) -> - nock('https://api.github.com') - .get('/repos/rnhmjoj/asjon/issues?state=open&sort=updated') + nock('https://git.example.com') + .get('/repos/owner/asjon/issues?state=open&sort=updated') .reply 200, [issue1, issue2] acc = 0 asjon.receive (e,l) -> @@ -81,16 +84,16 @@ describe 'modulo github', -> acc = 0 asjon.receive (e,l) -> 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 - l.join().should.equal 'http://github.com/rnhmjoj/asjon/issues/456' + l.join().should.equal 'https://git.example.com/owner/asjon/issues/456' acc++ if acc is 2 then done() asjon.send 'asjon linkami la issue numero 5' asjon.send 'asjon linkami issue 456' 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) -> acc = 0 @@ -110,10 +113,10 @@ describe 'modulo github', -> l.join().should.equal 'branch '+req.body.ref+' aggiornato!\ntest -> commit' acc++ if acc is 2 then done() - githubhook req, res + githook req, res req.body.ref = 'refs/heads/master' req.body.commits.push committer: username: 'test' message: 'commit' - githubhook req, res + githook req, res diff --git a/test/shell-test.coffee b/test/shell-test.coffee index 7a6f082..ec0efea 100644 --- a/test/shell-test.coffee +++ b/test/shell-test.coffee @@ -88,4 +88,4 @@ describe 'modulo shell', -> if acc is 3 done() process.env.AUTO_KILL_ON_UPDATE = 'true' - asjon.robot.emit 'githubhook', data, params + asjon.robot.emit 'githook', data, params