/*! Kernel CI Dashboard | Licensed under the GNU GPL v3 (or later) */
define([
    'sprintf',
    'utils/git-rules',
    'URI'
], function(p, gitRules, URI) {
    'use strict';
    var urls;

    urls = {};

    // Translate a URL into a URI object.
    // Return a 2-elements list:
    //  0. The URI object
    //  1. The URI path
    // In case the server URL is not valid, it returns a 2-nulls list.
    urls.translateServerURL = function(serverUrl, serverPath, data) {
        var serverUri,
            translatedUrl,
            validPath;

        function addPath(value) {
            validPath = validPath + value + '/';
        }

        if (serverUrl) {
            if (serverPath) {
                validPath = serverPath;
            } else {
                validPath = '';
                if (data) {
                    data.forEach(addPath);
                }
            }

            serverUri = new URI(serverUrl);
            translatedUrl = [serverUri, serverUri.path() + '/' + validPath];
        } else {
            translatedUrl = [null, null];
        }

        return translatedUrl;
    };

    /*
        Return a list with:
        0. The base git URL
        1. The git commit URL
    */
    urls.translateCommit = function(url, sha) {
        var bURL,
            cURL,
            hostName,
            knownGit,
            parser,
            urlPath;

        function replaceRule(value) {
            urlPath = urlPath.replace(value[0], value[1]);
        }

        bURL = null;
        cURL = null;
        if (url && sha) {
            parser = new URI(url);
            hostName = parser.hostname();
            urlPath = parser.path();

            // Perform translation only if we know the host.
            if (gitRules.hasOwnProperty(hostName)) {
                knownGit = gitRules[hostName];

                knownGit[3].forEach(replaceRule);

                bURL = new URI({
                    protocol: knownGit[0],
                    hostname: hostName,
                    path: p.sprintf(knownGit[1], urlPath)
                });
                cURL = new URI({
                    protocol: knownGit[0],
                    hostname: hostName,
                    path: p.sprintf(knownGit[2], urlPath) + sha
                });

                bURL = bURL.href();
                cURL = cURL.href();
            }
        }
        return [bURL, cURL];
    };

    return urls;
});

@@ -197,24 +197,2014 @@
 urls = %7B%7D;%0A%0A
+    /**%0A     * Concatenate the path and extra path to create a full URL.%0A     * @param  %7BURI%7D    base      The base URI%0A     * @param  %7BString%7D path      The path string%0A     * @param  %7BString%7D extraPath The extra path to append%0A     * @return %7BString%7D           The normalized href%0A     */%0A    urls.getHref = function(base, path, extraPath) %7B%0A        return base.path(URI.joinPaths(path, extraPath)).normalizePath().href();%0A    %7D;%0A%0A    /**%0A     * Create the file server URL and its path.%0A     * @param  %7BString%7D serverURL The file server URL%0A     * @param  %7BObject%7D data      The object from which to take the data%0A     * @return %7BArray%7D            A 2-values array: the file server URI,%0A     * and the path%0A     */%0A    urls.createFileServerURL = function(serverURL, data) %7B%0A        var translatedURL;%0A        var serverURI;%0A%0A        translatedURL = %5Bnull, null%5D;%0A        if (serverURL) %7B%0A            serverURI = new URI(serverURL);%0A%0A            translatedURL%5B0%5D = serverURI;%0A%0A            if (data.file_server_resource) %7B%0A                translatedURL%5B1%5D = URI%0A                    .joinPaths(serverURI.path(), data.file_server_resource)%0A                    .path();%0A            %7D else %7B%0A                if (data.version === '1.0') %7B%0A                    translatedURL%5B1%5D = URI%0A                        .joinPaths(%0A                            data.job,%0A                            data.kernel,%0A                            data.arch + '-' +%0A                            (data.defconfig_full %7C%7C data.defconfig))%0A                        .path();%0A                %7D else %7B%0A                    translatedURL%5B1%5D = URI%0A                        .joinPaths(%0A                            data.job,%0A                            data.git_branch,%0A                            data.kernel,%0A                            data.arch,%0A                            (data.defconfig_full %7C%7C data.defconfig))%0A                        .path();%0A                %7D%0A            %7D%0A        %7D%0A%0A        return translatedURL;%0A    %7D;%0A%0A
     // Trans

Add new function to urls module