summaryrefslogtreecommitdiffstats
path: root/root/static/github_activity.js
blob: 1cd5b48dc5c4229d5d3a36024d5de589f23fdf3f (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * 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/", "")); } %>"> \
                <% print(payload.ref.replace("refs/heads/", "")); %></a> \
                at <a href="<%= repository.url %>"> \
                <%= repository.owner %>/<%= repository.name %></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 %></a> for \
                <a href="<%= repository.url %>"> \
                <%= repository.owner %>/<%= repository.name %></a>: \
                <%= 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.owner %>/<%= repository.name %></a> \
            <% } else if (type == "ForkEvent") { %> \
                <b>forked</b> <a href="<%= repository.url %>"> \
                <%= repository.owner %>/<%= repository.name %></a> \
                to <a href="<%= url %>"> \
                <%= actor %>/<%= repository.name %></a> \
            <% } else if (type == "GollumEvent") { %> \
                <b><%= payload.pages[0].action %></b> the \
                <a href="<%= url %>">"<%= payload.pages[0].page_name %>" \
                wiki page</a> \
                in <a href="<%= repository.url%>"> \
                <%= repository.owner %>/<%= repository.name %></a> \
            <% } else if (type == "FollowEvent") { %> \
                <b>started following</b> \
                <a href="<%= url %>"><%= payload.target.login %></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, _));