-2

I want to use Greasemonkey to link Redmine issue numbers which are displayed in cgit commit messages to their issues or projects.

The HTML source of the cgit Commit messages is like this.

<a href='/editingmodule/commit/?id=49e4a33e0f8b306ded5'>refs #459 - create separate directory and repo for editingModule, lots of dif...</a>

What I want to do is to make the #459 a separate href to the Redmine issue, but leave retain the cgit links on both sides unchanged. So the URL above is transformed into something like this:

<a href='/editingmodule/commit/?id=49e4a33e0f8b306ded5'>refs</a>
<a href='http://redmine.project.com/redmine/issues/459'>#459</a>
<a href='/editingmodule/commit/?id=49e4a33e0f8b306ded5'> - create separate directory and repo for editingModule, lots of dif...</a>

It may be hard to read but the #459 in the link above is linked to the Redmine project.

The link to the Redmine issue can also be appended to the cgit link for clarity.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
vfclists
  • 19,193
  • 21
  • 73
  • 92
  • 1
    [I think it is appropriate to ask what is your question?](http://whathaveyoutried.com) – Alexander Dec 08 '12 at 08:21
  • @Alexander Thanks I have edited it. I copied my original notes about the question into the stackoverflow edit box and forgot to edit them. – vfclists Dec 08 '12 at 08:33
  • Alexander,Juhana, Explosion Pills, Francois Wahl et al. Did you notice that someone understood the question well enough to give a perfect working solution to the question in programming code before you closed it about 7 hrs later? – vfclists Dec 09 '12 at 18:09
  • My vote was casted more than 20 minutes before the answer. There wasn't any real question at that time and I still consider this haven't changed. I couldn't see what you tried and why you failed to accomplish this task and it's still unknown. Code request are also usual and you can find answered code requests, I just think that you were lucky this time – Alexander Dec 09 '12 at 19:12
  • @Alexander You may have voted 20 mins before the answer but clearly those who voted after you could see that someone understood the question well enough to give a perfect non-trivial answer to it, and it is the best answer I have had to a question on SO. It is hard to frame a question using the right terms in an area one is unfamiliar with and that's no reason to close a question. The person who answered had the insight, the expertise and the diligence in the subject matter to understand it, and it seems that it was the absence of that on your part and the other that led it to be voted down. – vfclists Dec 09 '12 at 19:50
  • I can't argue on everyone else's judgement – Alexander Dec 09 '12 at 20:01
  • In your (Alexander) case I can understand because I even replied and fixed it, but the last 2 or 3 who voted it down are plain wrong if they voted after it was answered. Did the followup voters observe the depth and thoroughness of the answer in relation to the question? Have you and them considered whether the stock reason given for closing displayed below is a complement to your moderation skills and attitude in light of the answer? I suggest you all read the question and the answer and reconsider. It is also unfair to the guy who answered it. I think he deserves more kudos for his answer. – vfclists Dec 09 '12 at 20:06

1 Answers1

3

Use jQuery to find the links, and regex to extract the key text parts.

Here is a complete working script; see the inline comments for more info:

// ==UserScript==
// @name     _Split Commit links and add Redmine links
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

//-- Fing matching links
$("a[href*='/editingmodule/commit/']").each ( function () {
    /*-- Parse each link for the expected format:
        refs #{number} {description text}
    */
    var jThis       = $(this);
    var parseText   = jThis.text ().match (/\s*(refs)\s+\#(\d+)\s+(.+)/i);
    if (parseText  &&  parseText.length >= 4) {
        //-- Truncate original link text.
        jThis.text (parseText[1]);

        //-- Add tailing link.
        jThis.after (
            ' <a href="' + jThis.attr ("href") + '">' + parseText[3] + '</a>'
        );

        //-- Add Redmine link. Note it is inserted just after the original link.
        var issueNumber = parseInt (parseText[2], 10);
        jThis.after (
            ' <a href="http://redmine.project.com/redmine/issues/'
            + issueNumber + '">#' + issueNumber + '</a>'
        );

        //-- Style the Redmine link, if desired.
        jThis.next ().css ("background", "yellow")
    }
} );
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thanks for the quick reply, much obliged. Neither Javascript nor regex are my forte. I sometimes have the commit messages reference multiple issues, eg refs #456, #457 etc. The 'refs' also don't need to be matched as Redmine allows other issue referencing words. It is only the #XXX strings that need to be matched. Could you help me with that as well? – vfclists Dec 08 '12 at 10:42
  • That's a bit more than the original question. Please open a new question for that. Give examples of every type of ref you want to match. A link to a http://pastebin.com/ of the whole HTML would be a good idea too. ... Also, to avoid people downvoting and voting to close (which i didn't), show what code you have attempted. – Brock Adams Dec 08 '12 at 12:58
  • I think it got downvoted because I copied my original note about the question into the edit box and forgot to edit them out, which I corrected. The addon question is more of a Javascript regex question, and how the matches are placed in an array. I'll do that when I'm back from my errands – vfclists Dec 08 '12 at 13:16
  • I have added another question - http://stackoverflow.com/questions/13782177/how-to-match-regular-expressions-only-if-they-are-surrounded-with-proper-punctua. The idea is that if the proper regex is derived, I use it to split the original URL and combine them with links to the issue numbers as in you original answer – vfclists Dec 08 '12 at 21:41