Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Prima "versione funzionante" #2

Open
wants to merge 14 commits into from

1 participant

Alberto Zaccagni
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 3 changed files with 124 additions and 38 deletions. Show diff stats Hide diff stats

  1. +21 0 README
  2. +15 0 config.js
  3. +88 38 rmr.js
21 README
... ... @@ -0,0 +1,21 @@
  1 +#Redmine Reminder
  2 +#MIT License
  3 +
  4 +#How to learn not to use the latest technology for the first problem at hand (lesson learnt :)
  5 +
  6 +
  7 +#setup your environment
  8 +
  9 +mkdir ~/bin
  10 +#if ~/bin is not in your path you might want to add it now
  11 +
  12 +mkdir ~/node-latest
  13 +cd !$
  14 +curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
  15 +
  16 +#ssl dev libraries are required
  17 +sudo apt-get install libssl-dev
  18 +./configure --prefix=~
  19 +make install
  20 +curl http://npmjs.org/install.sh | sh
  21 +npm install xml2js
15 config.js
... ... @@ -0,0 +1,15 @@
  1 +var config = {
  2 + host: '',
  3 + port: '',
  4 + redmineUserId: '',
  5 + redmineuser: '',
  6 + redminepass: '',
  7 + dbhost: '',
  8 + dbname: '',
  9 + dbuser: '',
  10 + dbpass: ''
  11 +};
  12 +
  13 +exports.getConfig = function() {
  14 + return config;
  15 +};
126 rmr.js
... ... @@ -1,49 +1,99 @@
1   -// Modulo utilizzato per lanciare l'eseguibile di Zenity
2   -var exec = require('child_process').exec;
  1 +//Redmine Reminder
  2 +//MIT License
3 3
4   -// Parser per le risposte xml
  4 +var USER_PRESSED_YES = 0;
  5 +var USER_PRESSED_NO = 1;
  6 +var cfg = require('./config');
  7 +var exec = require('child_process').exec;
5 8 var xml2js = require('xml2js');
6 9 var parser = new xml2js.Parser();
  10 +// to log something: require("sys").puts("error");
7 11
8   -// Client per le richieste al server
9   -var redmine = require('http').createClient(80, 'tracker.nextre.it');
  12 +Date.prototype.getWeek = function() {
  13 + var onejan = new Date(this.getFullYear(),0,1);
  14 + return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
  15 +}
10 16
11   -// Header per le richieste al tracker
  17 +var redmine = require('http').createClient(80, cfg.getConfig().host);
12 18 var requestHeaders = {
13   - 'host': 'tracker.nextre.it',
14   - 'Authorization': 'Basic ' + new Buffer('gennaro.vietri:270583').toString('base64')
  19 + 'host': cfg.getConfig().host,
  20 + 'Authorization': 'Basic ' + new Buffer(cfg.getConfig().redmineuser + ':' + cfg.getConfig().redminepass).toString('base64')
15 21 };
16 22
17   -// Recupero tutte le issue aperte e assegnate all'utente
18   -var reqIssues = redmine.request('GET', '/issues.xml?assigned_to_id=me&status_id=open', requestHeaders);
  23 +//retrieve all user assigned open issues
  24 +var reqIssues = redmine.request('GET', '/issues.xml?assigned_to_id=me', requestHeaders);
  25 +var issuesListTemplate = '/usr/bin/zenity --list --width=600 --height=500 --title="Your issues" --column="#" --column="Project" --column="Title" ';
  26 +reqIssues.on('response', function(response) {
  27 + response.setEncoding('utf8');
  28 + var body = "";
  29 + response.on('data', function(chunk) {
  30 + body += chunk;
  31 + }).on('end', function() {
  32 + parser.on('end', function(result) {
  33 + //build the issue list
  34 + issuesList = issuesListTemplate;
  35 + var issues = result.issue;
  36 + for (var i in issues) {
  37 + //remove double quotes since are use by zenity
  38 + issuesList += issues[i].id + ' "' + issues[i].project['@'].name + '" "' + issues[i].subject.replace(/\"/g, "") + '" ';
  39 + }
  40 + //show the list
  41 + child = exec(issuesList, function(error, stdout, stderr) {
  42 + var issueId = stdout.replace("\n", "");
  43 + if(issueId===''){
  44 + return;
  45 + }
19 46
20   -// Template per generare la lista con Zenity
21   -// @todo identificare più precisamente l'eseguibile
22   -var issuesListTpl = 'zenity --list --width=600 --height=500 --title="Elenco delle issue che ti sono assegnate" --column="#" --column="Progetto" --column="Titolo" ';
  47 + exec('/usr/bin/zenity --question --text "Do you want to log an hour on task #'+issueId+'?"; echo $?', function(error, stdout, stderr){
23 48
24   -reqIssues.on('response', function(response) {
25   -
26   - response.setEncoding('utf8');
27   -
28   - // Recupero tutto l'esito della richiesta,
29   - // al termine processo la risposta
30   - var body = "";
31   - response.on('data', function(chunk) {
32   - body += chunk;
33   - }).on('end', function() {
34   - parser.on('end', function(result) {
35   - // Compongo la lista delle issue
36   - issuesList = issuesListTpl;
37   - var issues = result.issue;
38   - for (var i in issues) {
39   - // Strippo via i doppi apici poiché sono usati da Zenity come enclosure
40   - issuesList += issues[i].id + ' "' + issues[i].project['@'].name + '" "' + issues[i].subject.replace(/\"/g, "") + '" ';
41   - }
42   -
43   - // Mostra la lista
44   - child = exec(issuesList, function(error, stdout, stderr) {
45   -
46   - });
47   - }).parseString(body);
48   - });
  49 + var today = new Date;
  50 + var monthDay = today.getDate().length==1 ? "0"+today.getDate() : today.getDate();
  51 + var year = today.getFullYear();
  52 + var month = today.getMonth()+1;
  53 + var week = today.getWeek();
  54 + var curdate = year+"-"+month+"-"+monthDay+" "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds();
  55 + var description = "";
  56 +
  57 + if(userChoice(stdout)==USER_PRESSED_NO){
  58 + exec('/usr/bin/zenity --question --text "Do you want to log half an hour on task #'+issueId+'?"; echo $?', function(error, stdout, stderr){
  59 + if(userChoice(stdout)==USER_PRESSED_NO){
  60 + return;
  61 + }
  62 +
  63 + exec('zenity --entry --text "Insert your description" --entry-text "generated from rmr"; echo $?', function(error, stdout, stderr){
  64 + description = stdout.replace("\n0", "");
  65 + exec('mysql -u'+cfg.getConfig().dbuser+' -p'+cfg.getConfig().dbpass+' -h '+cfg.getConfig().dbhost+' '+cfg.getConfig().dbname+
  66 + ' -e "SELECT project_id FROM issues WHERE id = '+issueId+'"',function(error, stdout, stderr){
  67 + var projectId = stdout.replace("\n", "").replace("project_id", "");
  68 + var query = 'mysql -u'+cfg.getConfig().dbuser+' -p'+cfg.getConfig().dbpass+' -h '+cfg.getConfig().dbhost+' '+cfg.getConfig().dbname+
  69 + ' -e "INSERT INTO time_entries(project_id, user_id, issue_id, hours, comments, activity_id, spent_on, tyear, tmonth, tweek, created_on, updated_on) '+
  70 + 'VALUES('+projectId+', '+cfg.getConfig().redmineUserId+', '+issueId+', 0.5, \''+description+'\', 9, \''+curdate+'\', '+year+', '+month+', '+week+', \''+curdate+'\', \''+curdate+'\')"';
  71 + exec(query, function(error, stdout, stderr){});
  72 + });
  73 + });
  74 +
  75 + });
  76 + }
  77 + if(userChoice(stdout)==USER_PRESSED_YES){
  78 + exec('zenity --entry --text "Insert your description" --entry-text "generated from rmr"; echo $?', function(error, stdout, stderr){
  79 + description = stdout.replace("\n0", "");
  80 + exec('mysql -u'+cfg.getConfig().dbuser+' -p'+cfg.getConfig().dbpass+' -h '+cfg.getConfig().dbhost+' '+cfg.getConfig().dbname+
  81 + ' -e "SELECT project_id FROM issues WHERE id = '+issueId+'"',function(error, stdout, stderr){
  82 + var projectId = stdout.replace("\n", "").replace("project_id", "");
  83 + var query = 'mysql -u'+cfg.getConfig().dbuser+' -p'+cfg.getConfig().dbpass+' -h '+cfg.getConfig().dbhost+' '+cfg.getConfig().dbname+
  84 + ' -e "INSERT INTO time_entries(project_id, user_id, issue_id, hours, comments, activity_id, spent_on, tyear, tmonth, tweek, created_on, updated_on) '+
  85 + 'VALUES('+projectId+', '+cfg.getConfig().redmineUserId+', '+issueId+', 1, \''+description+'\',9, \''+curdate+'\', '+year+', '+month+', '+week+', \''+curdate+'\', \''+curdate+'\')"';
  86 + exec(query, function(error, stdout, stderr){});
  87 + });
  88 + });
  89 + }
  90 + });
  91 +
  92 + });
  93 + }).parseString(body);
  94 + });
49 95 }).end();
  96 +
  97 +function userChoice(stdout){
  98 + return stdout.replace("\n", "");
  99 +}

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.