add polling git for issue changes

This commit is contained in:
Johannes Millan 2017-02-12 19:34:05 +01:00
parent 7e9c942b5a
commit 8d2cfab6cb
7 changed files with 107 additions and 14 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View file

@ -39,6 +39,7 @@
.config(fixUnhandledRejectionError)
.run(initGlobalModels)
.run(initPollJiraTaskUpdates)
.run(initPollGitTaskUpdates)
.run(initPollForSimpleTimeTracking)
.run(initMousewheelZoomForElectron)
.run(initGlobalShortcuts)
@ -149,19 +150,32 @@
/* @ngInject */
function initPollJiraTaskUpdates($localStorage, Jira, IS_ELECTRON, $interval, JIRA_UPDATE_POLL_INTERVAL) {
// handle updates that need to be made locally
if (IS_ELECTRON) {
// one initial
Jira.checkForUpdates($localStorage.tasks);
// one initial call
if (Jira.isSufficientJiraSettings()) {
Jira.checkForUpdates($localStorage.tasks);
}
$interval(() => {
if ($localStorage.currentTask) {
if (Jira.isSufficientJiraSettings()) {
Jira.checkForUpdates($localStorage.tasks);
}
}, JIRA_UPDATE_POLL_INTERVAL);
}
}
/* @ngInject */
function initPollGitTaskUpdates($localStorage, Git, $interval, GIT_UPDATE_POLL_INTERVAL) {
// one initial
Git.checkAndUpdateTasks($localStorage.tasks);
$interval(() => {
if ($localStorage.git.projectDir && $localStorage.git.repo) {
Git.checkAndUpdateTasks($localStorage.tasks);
}
}, GIT_UPDATE_POLL_INTERVAL);
}
/* @ngInject */
function showWelcomeDialog($localStorage, Dialogs) {
if ($localStorage.isShowWelcomeDialog) {

View file

@ -125,6 +125,7 @@
.constant('REQUEST_TIMEOUT', 15000)
.constant('WORKLOG_DATE_STR_FORMAT', 'YYYY-MM-DD')
.constant('JIRA_UPDATE_POLL_INTERVAL', 60 * 1000 * 5)
.constant('GIT_UPDATE_POLL_INTERVAL', 60 * 1000 * 0.25)
.constant('SIMPLE_TIME_TRACKING_INTERVAL', 1000 * 5)
.constant('IS_ELECTRON', (typeof window.ipcRenderer !== 'undefined'))
.constant('THEMES', [

View file

@ -14,14 +14,20 @@
.service('Git', Git);
/* @ngInject */
function Git($http, $localStorage) {
function Git($http, $localStorage, TasksUtil, $injector, $q, Notifier, SimpleToast) {
const TYPE = 'GITHUB';
const BASE_URL = 'https://api.github.com/';
const settings = $localStorage.git;
function mapIssue(issue) {
// PRIVATE HELPER FUNCTIONS
// ------------------------
function transformIssue(issue) {
let title;
if (typeof issue === 'string') {
issue = angular.fromJson(issue);
}
if (issue.pull_request) {
title = `${settings.prPrefix} #${issue.number}: ${issue.title}`;
} else {
@ -45,17 +51,11 @@
}
const newIssues = [];
issues.forEach((issue) => {
newIssues.push(mapIssue(issue));
newIssues.push(transformIssue(issue));
});
return newIssues;
}
this.getIssueList = () => {
return $http.get(BASE_URL + 'repos/' + settings.repo + '/issues', {
transformResponse: [transformIssueList]
});
};
function transformComments(comments) {
if (typeof comments === 'string') {
comments = angular.fromJson(comments);
@ -71,12 +71,81 @@
return newComments;
}
function taskIsUpdatedHandler(updatedTask) {
if (updatedTask) {
Notifier({
title: 'Git Update',
message: '"' + updatedTask.title + '" => has been updated as it was updated on Git.',
sound: true,
wait: true
});
SimpleToast('"' + updatedTask.title + '" => has been updated as it was updated on Git.');
}
}
// HELPER METHODS
// --------------
this.isGitTask = (task) => {
return task && task.originalId && task.originalType === TYPE;
};
// API METHODS
// -----------
this.getIssueList = () => {
return $http.get(BASE_URL + 'repos/' + settings.repo + '/issues', {
transformResponse: [transformIssueList]
});
};
this.getCommentListForIssue = (issueNumber) => {
return $http.get(BASE_URL + 'repos/' + settings.repo + '/issues/' + issueNumber + '/comments', {
transformResponse: [transformComments]
});
};
this.getIssueById = (issueNumber) => {
return $http.get(BASE_URL + 'repos/' + settings.repo + '/issues/' + issueNumber, {
transformResponse: [transformIssue]
});
};
// COMPLEXER WRAPPER METHODS
// -------------------------
this.checkAndUpdateTasks = (tasks) => {
const TasksUtil = $injector.get('TasksUtil');
const defer = $q.defer();
const tasksToPoll = TasksUtil.flattenTasks(tasks, this.isGitTask, this.isGitTask);
// execute requests sequentially to have a little more time
const pollPromise = tasksToPoll.reduce((promise, task) =>
promise.then(() =>
this.getIssueById(task.originalId)
.then((res) => {
const issue = res.data;
const lastUpdate = moment(task.originalUpdated);
if (lastUpdate && moment(issue.originalUpdated).isAfter(lastUpdate)) {
// extend task with new values
angular.extend(task, issue);
taskIsUpdatedHandler(issue, task);
// also update comment list in the next step
this.getCommentListForIssue(task.originalId)
.then((res) => {
task.originalComments = res.data;
});
}
}, defer.reject)
), Promise.resolve()
);
pollPromise
.then(() => {
defer.resolve();
});
return defer.promise;
};
}
})();

View file

@ -17,10 +17,19 @@
function Notifier(IS_ELECTRON) {
const IPC_NOTIFIER_EV = 'NOTIFY';
if (!IS_ELECTRON && Notification && Notification.permission !== 'granted') {
Notification.requestPermission();
}
return function (notification) {
if (IS_ELECTRON) {
// send to electron
window.ipcRenderer.send(IPC_NOTIFIER_EV, notification);
} else if (Notification && Notification.permission === 'granted') {
new Notification(notification.title, {
icon: notification.icon || 'img/icon_128x128-with-pad.png',
body: notification.message
});
}
};

View file

@ -145,7 +145,7 @@
</section>
<section>
<div class="md-caption">Notes</div>
<div class="md-caption">{{ task.originalId ? 'Description': 'Notes' }}</div>
<div md-whiteframe="4">
<inline-markdown ng-model="task.notes"
on-changed="$ctrl.onTaskNotesChanged(task)"></inline-markdown>