Automatically create links in the issue description and comments.

Link to issues but also commits.
This commit is contained in:
Loic d'Anterroches 2008-07-28 20:31:23 +02:00
parent 3dbae6272a
commit 3fb47562ce
4 changed files with 126 additions and 4 deletions

View File

@ -30,9 +30,11 @@
class IDF_Project extends Pluf_Model class IDF_Project extends Pluf_Model
{ {
public $_model = __CLASS__; public $_model = __CLASS__;
public $_extra_cache = array();
function init() function init()
{ {
$this->_extra_cache = array();
$this->_a['table'] = 'idf_projects'; $this->_a['table'] = 'idf_projects';
$this->_a['model'] = __CLASS__; $this->_a['model'] = __CLASS__;
$this->_a['cols'] = array( $this->_a['cols'] = array(
@ -147,11 +149,18 @@ class IDF_Project extends Pluf_Model
* Get the open/closed tag ids as they are often used when doing * Get the open/closed tag ids as they are often used when doing
* listings. * listings.
* *
* As this can be often used, the info are cached.
*
* @param string Status ('open') or 'closed' * @param string Status ('open') or 'closed'
* @param bool Force cache refresh (false)
* @return array Ids of the open/closed tags * @return array Ids of the open/closed tags
*/ */
public function getTagIdsByStatus($status='open') public function getTagIdsByStatus($status='open', $cache_refresh=false)
{ {
if (!$cache_refresh
and isset($this->_extra_cache['getTagIdsByStatus-'.$status])) {
return $this->_extra_cache['getTagIdsByStatus-'.$status];
}
switch ($status) { switch ($status) {
case 'open': case 'open':
$key = 'labels_issue_open'; $key = 'labels_issue_open';
@ -167,6 +176,7 @@ class IDF_Project extends Pluf_Model
foreach ($this->getTagsFromConfig($key, $default, 'Status') as $tag) { foreach ($this->getTagsFromConfig($key, $default, 'Status') as $tag) {
$tags[] = (int) $tag->id; $tags[] = (int) $tag->id;
} }
$this->_extra_cache['getTagIdsByStatus-'.$status] = $tags;
return $tags; return $tags;
} }

View File

@ -0,0 +1,107 @@
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ***** END LICENSE BLOCK ***** */
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
/**
* Make the links to issues and commits.
*/
class IDF_Template_IssueComment extends Pluf_Template_Tag
{
private $project = null;
private $git = null;
function start($text, $project)
{
$this->project = $project;
$this->git = new IDF_Git(Pluf::f('git_repository'));
$text = wordwrap($text, 80, "\n", true);
$text = Pluf_esc($text);
$text = ereg_replace('[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]',
'<a href="\\0" rel="nofollow">\\0</a>',
$text);
$text = preg_replace_callback('#(issues?|bugs?|tickets?)\s+(\d+)((\s+and|\s+or|,)\s+(\d+)){0,}#im',
array($this, 'callbackIssues'), $text);
$text = preg_replace_callback('#(commit\s+)([0-9a-f]{5,40})#im',
array($this, 'callbackCommit'), $text);
echo $text;
}
/**
* General call back for the issues.
*/
function callbackIssues($m)
{
if (count($m) == 3) {
$issue = new IDF_Issue($m[2]);
if ($issue->id > 0 and $issue->project == $this->project->id) {
return $this->linkIssue($issue, $m[1].' '.$m[2]);
} else {
return $m[0]; // not existing issue.
}
} else {
return preg_replace_callback('/(\d+)/',
array($this, 'callbackIssue'),
$m[0]);
}
}
/**
* Call back for the case of multiple issues like 'issues 1, 2 and 3'.
*
* Called from callbackIssues, it is linking only the number of
* the issues.
*/
function callbackIssue($m)
{
$issue = new IDF_Issue($m[1]);
if ($issue->id > 0 and $issue->project == $this->project->id) {
return $this->linkIssue($issue, $m[1]);
} else {
return $m[0]; // not existing issue.
}
}
function callbackCommit($m)
{
if ($this->git->testHash($m[2]) != 'commit') {
return $m[0];
}
$co = $this->git->getCommit($m[2]);
return '<a href="'.Pluf_HTTP_URL_urlForView('IDF_Views_Source::treeBase', array($this->project->shortname, $co->commit)).'">'.$m[1].$m[2].'</a>';
}
/**
* Generate the link to an issue.
*
* @param IDF_Issue Issue.
* @param string Name of the link.
* @return string Linked issue.
*/
public function linkIssue($issue, $title)
{
$ic = (in_array($issue->status, $this->project->getTagIdsByStatus('closed'))) ? 'issue-c' : 'issue-o';
return '<a href="'.Pluf_HTTP_URL_urlForView('IDF_Views_Issue::view',
array($this->project->shortname, $issue->id)).'" class="'.$ic.'" title="'.Pluf_esc($issue->summary).'">'.Pluf_esc($title).'</a>';
}
}

View File

@ -13,7 +13,7 @@
<p>{blocktrans}Comment <a href="{$url}">{$i}</a> by {$who}, {$c.creation_dtime|date}{/blocktrans}</p> <p>{blocktrans}Comment <a href="{$url}">{$i}</a> by {$who}, {$c.creation_dtime|date}{/blocktrans}</p>
{/if} {/if}
<pre class="issue-comment-text">{if strlen($c.content) > 0}{$c.content|issuetext}{else}<i>{trans '(No comments were given for this change.)'}</i>{/if}</pre> <pre class="issue-comment-text">{if strlen($c.content) > 0}{issuetext $c.content, $project}{else}<i>{trans '(No comments were given for this change.)'}</i>{/if}</pre>
{if $i> 0 and $c.changedIssue()} {if $i> 0 and $c.changedIssue()}
<div class="issue-changes"> <div class="issue-changes">

View File

@ -123,8 +123,13 @@ span.px-header-title {
/** /**
* Issue * Issue
*/ */
p.issue-comment-text { a.issue-c {
text-decoration: line-through;
}
pre.issue-comment-text {
font-family: monospace; font-family: monospace;
line-height: 1.2; /* to be nice also with links */
} }
div.issue-comment { div.issue-comment {