Skip to content

Instantly share code, notes, and snippets.

@tron1point0
Created March 31, 2012 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tron1point0/2265848 to your computer and use it in GitHub Desktop.
Save tron1point0/2265848 to your computer and use it in GitHub Desktop.
Redmine TODO list
#!/usr/bin/env perl
use v5.14;
use warnings;
use LWP::UserAgent;
use List::MoreUtils qw(part);
use YAML qw(LoadFile);
use JSON::XS qw(decode_json);
use Text::FormatTable;
use Term::ReadKey;
die join "",<DATA> if @ARGV && $ARGV[0] =~ /-h/;
my @confs = reverse grep {defined && -r} (
shift @ARGV,
qw(.todo.yml .todo),
"$ENV{XDG_CONFIG_HOME}/todo/global.yml"
);
die "Couldn't find suitable configuration." unless @confs;
my %config = map {%{LoadFile $_}} @confs;
my $ua = LWP::UserAgent->new;
$ua->default_header(
content_type => 'application/json',
':X-Redmine-API-Key' => $config{key},
);
@config{qw(base project)} = ($1,$2) if
$config{project} =~ m{(http://[^/]+)/projects/([^/]+)};
my $res = $ua->get("$config{base}/issues.json?project_id=$config{project}");
die join "\n",
$res->status_line,
$res->{_request}->headers->as_string,
$res->headers->as_string,
$res->content, "" unless $res->is_success;
my @fields = @{$config{fields}};
my @issues =
map {[
map {{map {ref $_ ? $_->{name} : $_} %$_}}
sort {$a->{$config{sort}}{id} <=> $b->{$config{sort}}{id}}
@$_]}
grep {defined}
part {$_->{$config{group}}{id}}
@{decode_json($res->content)->{issues}};
my $table = Text::FormatTable->new(join ' ',('l')x@fields);
$table->head(map {ucfirst} @fields);
$table->row(@$_) for map {
map {my %i=%$_; [map {$i{$_}} @fields]} @$_
} @issues;
print $table->render((GetTerminalSize)[0]);
__DATA__
todo: fetch the current project's issue list from Redmine
USAGE: todo [config_file]
config_file: If not passed, assumed to be ./.todo.yml, ./.todo or
$XDG_CONFIG_HOME/todo/global.yml. See CONFIG FILE section for configuration
options.
CONFIG FILE:
Between all the config files, you must specify the following:
key:
Your Redmine API key, which you can find on the right side of
/my/account on your Redmine instance, under "API access key"
fields:
A list of the fields you want to see in the results
group:
The field by which to group the results
sort:
The field by which to sort the results
project:
The project from which to fetch the issue list. You can specify the
full URL and forego specifying base, or you can specify base and use
the human-readable project identifier or the project's ID
You may also need to specify:
base:
The base URL of your Redmine instance. Optional if you use the full URL
in project.
I put key, base, fields, group, and sort in $XDG_CONFIG_HOME/todo/global.yml
and project in ./.todo.yml, but that's just me. Settings in config_file
override settings in ./.todo.yml, which override settings in ./.todo, which
override settings in the global file.
@tron1point0
Copy link
Author

There was no reason for this to have been a private gist.

@tron1point0
Copy link
Author

This should also try to get the project's name based on the directory you're in. I'm thinking it should find the nearest parent with a .git child and try to use it as the project name (but only if no project name is given).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment