From 32577bee43db4870503ae71cc1bf00cb3f1056fb Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Wed, 26 Oct 2016 14:18:03 +0200
Subject: [PATCH] Implement poller for GitHub

---
 LICENSE                                       |  2 +-
 config/default.yaml                           |  2 +-
 package.json                                  | 32 ++++----
 .../tracker/sources/github/githubSource.js    | 74 +++++++++++++++++++
 plugins/tracker/sources/sourcesCollection.js  |  1 +
 5 files changed, 96 insertions(+), 15 deletions(-)
 create mode 100644 plugins/tracker/sources/github/githubSource.js

diff --git a/LICENSE b/LICENSE
index b914c99..c6396e9 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2012 Francois Zaninotto
+Copyright (c) 2016 Jürgen Haas
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
diff --git a/config/default.yaml b/config/default.yaml
index 6f779b8..1fe6093 100644
--- a/config/default.yaml
+++ b/config/default.yaml
@@ -12,7 +12,7 @@ monitor:
   apiUrl:                 'http://localhost:8082/api' # must be accessible without a proxy
   pollingInterval:        10000      # ten seconds
   timeout:                5000       # five seconds
-  userAgent:              NodeUptime/3.0 (https://github.com/fzaninotto/uptime)
+  userAgent:              NodeUptime/3.0 (https://gitlab.paragon-es.de/tools/uptime)
 
 ssl:
   enabled:                false
diff --git a/package.json b/package.json
index 8de53d8..cd6d7a0 100644
--- a/package.json
+++ b/package.json
@@ -1,22 +1,22 @@
 {
-  "name":    "node-uptime",
+  "name": "node-uptime",
   "description": "Remote monitoring for HTTP applications",
   "version": "3.2.0",
-  "author": "Francois Zaninotto",
+  "author": "Jürgen Haas",
   "dependencies": {
-    "mongoose":  "3.6.7",
+    "mongoose": "3.6.7",
     "mongoose-lifecycle": "1.0.0",
-    "express":   "3.2.0",
+    "express": "3.2.0",
     "express-partials": "0.1.1",
     "connect-flash": "0.1.0",
-    "ejs":       "0.8.3",
-    "config":    "0.4.32",
-    "async":     "0.1.22",
+    "ejs": "0.8.3",
+    "config": "0.4.32",
+    "async": "0.1.22",
     "socket.io": "0.9.14",
-    "semver":    "1.1.0",
-    "moment":    "2.1.0",
+    "semver": "1.1.0",
+    "moment": "2.1.0",
     "nodemailer": "0.3.35",
-    "net-ping":  "1.1.7",
+    "net-ping": "1.1.7",
     "js-yaml": "2.1.0",
     "webpagetest": "0.2.0",
     "pushover-notifications": "0.1.5",
@@ -24,14 +24,20 @@
     "jira-connector": "^1.9.0",
     "request": "^2.65.0",
     "feedparser": "^1.1.4",
-    "node-mattermost": "^0.0.1"
+    "node-mattermost": "^0.0.1",
+    "github": "^5.3.3"
   },
   "devDependencies": {
     "mocha": "1.7.x",
     "should": "1.1.0"
   },
-  "keywords": ["uptime", "monitoring", "api", "check"],
-  "repository": "https://github.com/fzaninotto/uptime",
+  "keywords": [
+    "uptime",
+    "monitoring",
+    "api",
+    "check"
+  ],
+  "repository": "https://gitlab.paragon-es.de/tools/uptime",
   "license": "MIT",
   "engines": {
     "node": "0.10.x",
diff --git a/plugins/tracker/sources/github/githubSource.js b/plugins/tracker/sources/github/githubSource.js
new file mode 100644
index 0000000..f252c66
--- /dev/null
+++ b/plugins/tracker/sources/github/githubSource.js
@@ -0,0 +1,74 @@
+/**
+ * Module dependencies.
+ */
+var GitHubApi= require('github');
+var util = require('util');
+var BaseTrackerSource = require('../baseSource');
+var github;
+
+/**
+ * GitHub TrackerSource, to receive latest notifications and report to targets
+ *
+ * @param {String} id
+ * @param {Integer} lastActivity
+ * @param {String} url
+ * @param {Array} params
+ * @api   public
+ */
+function GitHubTrackerSource(id, lastActivity, url, params) {
+  GitHubTrackerSource.super_.call(this, id, lastActivity, url, params);
+}
+
+util.inherits(GitHubTrackerSource, BaseTrackerSource);
+
+GitHubTrackerSource.type = 'GitHub';
+
+GitHubTrackerSource.prototype.poll = function() {
+  var source = this;
+  github = new GitHubApi( {
+    host: source.url.substr(source.url.search('://') + 3)
+  });
+  github.authenticate({
+    type: "token",
+    token: source.password
+  });
+  github.activity.getNotifications({}, function(err, res) {
+    if (error) {
+      source.debug(error);
+      return;
+    }
+    if (res === undefined) {
+      return;
+    }
+    source.readItems(res, false);
+  });
+};
+
+GitHubTrackerSource.prototype.notifyItem = function(item) {
+  github.activity.markNotificationThreadAsRead({
+    id: item.id
+  });
+  return true;
+};
+
+GitHubTrackerSource.prototype.renderItem = function(item, linkCB, imageCB) {
+  return linkCB(item.subject.type, item.subject.url.replace('//api.', '')) + ' ' + item.subject.title;
+};
+
+GitHubTrackerSource.prototype.getItemDate = function(item) {
+  return item.updated_at;
+};
+
+GitHubTrackerSource.prototype.compareDate = function(a, b) {
+  return (Date.parse(this.getItemDate(a)) - Date.parse(this.getItemDate(b)));
+};
+
+GitHubTrackerSource.prototype.isItemNew = function(item) {
+  return true;
+};
+
+GitHubTrackerSource.prototype.formatDate = function(timestamp) {
+  return timestamp;
+};
+
+module.exports = GitHubTrackerSource;
diff --git a/plugins/tracker/sources/sourcesCollection.js b/plugins/tracker/sources/sourcesCollection.js
index e455648..2297a8d 100644
--- a/plugins/tracker/sources/sourcesCollection.js
+++ b/plugins/tracker/sources/sourcesCollection.js
@@ -4,6 +4,7 @@ var TrackerSourceCollection = function(sources) {
 };
 
 TrackerSourceCollection.prototype.addDefaultSources = function() {
+  this.add(require('./github/githubSource.js'));
   this.add(require('./jira/jiraSource.js'));
   this.add(require('./redmine/redmineSource.js'));
   this.add(require('./rss/rssSource.js'));
-- 
GitLab