v0.4 Release – Better regexps for Creating bugLinks

This is concerning bug 459727. As one of my goals for this release I wanted to create better regexps in order to match all possible bugLink patterns i.e. bug 123455, b=134566, 345777 or BUG 293455

To do this I came up with 3 different regexps which match the above patterns and nothing else. The following is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function createBuglink(str) {
  //Create buglink
  var matchFound = new Boolean(false);
  var bugInDesc = 0;
  var pattern = -1;
  var bugLink = "";
 
  re = new RegExp("[bB][uU][gG]\s\d{6}");
  re2 = new RegExp("b=\d{6}");
  re3 = new RegExp("\d{6}\W");
 
  bugInDesc = str.search(re);
  if(bugInDesc != -1) {
    matchFound = true;
	pattern = 1;
  } else {
      bugInDesc = str.search(re2);
	  if(bugInDesc != -1) {
	    matchFound = true;
	    pattern = 2;
	  } else {
	      bugInDesc = str.search(re3);
	      if(bugInDesc != -1) {
	        matchFound = true;
		    pattern = 3;
		  }
	  }
  }
 
  if(matchFound == true) {
    if(pattern == 1) {
	  bugLink = bugLinkify(bugInDesc, str, 4, 10);
	} else if(pattern == 2) {
	  bugLink = bugLinkify(bugInDesc, str, 2, 8);
    } else if(pattern == 3) {
	  bugLink = bugLinkify(bugInDesc, str, 0, 6);
    }	
  } else { //No bug provided
      bugLink = str;
    }
 
  return bugLink;
}

As you can see once a match is found (bugInDesc is not -1) the pattern being used in the string is identified and then bugLinkify() is called which is responsible for actually creating the link.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function bugLinkify(bugInDesc, str, start, end) {
  var bugLinkName = str.substring(bugInDesc, bugInDesc + end);
  var bugNumber = bugLinkName.substring(start, end);
  var bugLink = 
    str.substring(0, bugInDesc) + 
    '<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=' + 
    bugNumber + 
    '">' + 
    bugLinkName + 
    '</a>' + 
    str.substring(bugInDesc + end, str.length);
 
  return bugLink;
}

Using this method the 3 different patterns are converted into bug links properly. See results below

This entry was posted in DPS911, Mercurial Project, Open Source and tagged , , , , . Bookmark the permalink.

3 Responses to v0.4 Release – Better regexps for Creating bugLinks

  1. Gijs says:

    Hi Sid,

    Just a short note: believe it or not, but there are bug #s which do not have 6 digits and have not yet been closed, so people will refer to them from time to time, so I believe your regexps should be adapted to also cope with those. :-)

    Best regards,
    Gijs

  2. Gijs says:

    Ah, also, for doing case insensitivity, there is the “i” flag for regexps – then you don’t need to do [bB][uU][gG], but can just use “bug” as-is. Hope that helps!

  3. Sid says:

    Good point Gijs, I didn’t know that there were bugs which don’t have 6 digits. I guess I will have to adjust my regexps.

    Thanks for the info

Leave a Reply to Sid Cancel reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>