summaryrefslogtreecommitdiffstats
path: root/root/static/github_activity.js
blob: 5c4cc5580493ba1992ed74e6907383e068a80f4e (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * Github activity feed.
 *
 * Graeme Sutherland, July 2012.
 *
 * Uses .json activity from github to show public commits.
 * Requires jQuery and underscore.js
 *
 */

var GithubActivity = (function($, _) {

    var
        self = {},
        gh = 'http://github.com/',
        default_template = '<li class="action"> \
            <% if (type == "PushEvent") { %> \
                <a href="<%= url %>"><b>pushed</b></a> \
                to <a href="<%= repository.url %><% if (payload.ref != "refs/heads/master") { print("/tree/"); print(payload.ref.replace("refs/heads/", "")); } %>"> \
                <%= repository.name %>/<% print(payload.ref.replace("refs/heads/", "")); %></a> \
                <dl><% _.each(payload.shas, function(sha) { %> \
                    <dt> \
                    <a href="<%= repository.url %>/commit/<%= sha[0] %>"> \
                    <%= sha[0].substring(0,6) %></a></dt> \
                    <dd><%= (sha[2].split("\\n"))[0] %></dd><% }); %></dl>\
            <% } else if (type == "GistEvent") { %> \
                <b><%= payload.action %>d gist</b>: \
                <a href="<%= payload.url %>"><%= payload.desc %></a> \
            <% } else if (type == "CreateEvent") { %> \
                <b>created <%= payload.ref_type %></b> <a href="<%= url %>"> \
                <%= payload.ref %></a> in <a href="<%= repository.url %>"> \
                <%= repository.name %></a> \
            <% } else if (type == "PullRequestEvent") { %> \
                <b><%= payload.action %> pull request</b> \
                <a href="<%= payload.pull_request.html_url %>"> \
                #<%= payload.number %>: <%= payload.pull_request.title %></a> \
            <% } else if (type == "IssueCommentEvent") { %> \
                <b>commented on issue</b> <a href="<%= url %>"> \
                #<% print(url.replace(/.*\\/issues\\/(\\d+)#.*/, "$1")); %> \
                </a> in \
                <a href="<%= repository.url%>"><%= repository.name %></a> \
            <% } else { %> \
                Unknown event type <% type %> \
            <% } %>\
            </li>';               

    /**
     * Fill in activity into selector from public events for username,
     * with optional template selector tmpl_selector.
     */
    self.show_activity = function (username, selector, tmpl_selector) {
        var
            url = 'https://github.com/' + username + '.json?callback=?',
            template = $(tmpl_selector).html() || default_template,
            compiled = _.template(template),
            current_date = '',
            $current_list;

        $.getJSON(url, {}, function (data) {
            $.each(data.slice(0, 6), function(index, commit) {
                var date = commit.created_at.substring(0, 10);
                if (date != current_date) {
                    if (current_date) {
                        var $new_item = $('<li class="date"></li>');
                        $new_item.append('<h5>' + current_date + '</h5>');
                        var $div = $('<div class="actions"></div>');
                        $div.append($current_list);
                        $new_item.append($div);
                        $(selector).append($new_item);
                    }

                    $current_list = $('<ul></ul>');
                    current_date = date;
                }
                var $action = $(compiled(commit));
                if (index % 2) {
                    $action.addClass('odd')
                }
                $current_list.append($action);
            });
            var $new_item = $('<li class="date"></li>');
            $new_item.append('<h5>' + current_date + '</h5>');
            var $div = $('<div class="actions"></div>');
            $div.append($current_list);
            $new_item.append($div);
            $(selector).append($new_item);
        });
    };

    return self;

}(jQuery, _));