public
Description: A little JS badge for showing off your latest Github activity on your site.
Homepage: http://log.lachstock.com.au/past/2008/10/29/pimping-your-github-commits-js/
Clone URL: git://github.com/lachlanhardy/github-activity-badge.git
github-activity-badge / inc / js / github-activity.js
100644 114 lines (94 sloc) 4.078 kb
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
112
113
114
function githubActivity() {
 
    var username = "lachlanhardy";
    var url = "";
 
 $.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20atom%20where%20url%3D%22http%3A%2F%2Fgithub.com%2F" + username + ".atom%22&format=json&callback=?",
    function(feed){
      
      var item = feed.query.results.entry;
      $(item).each(function(i){
        var v = item[i].id;
        var idValue = v.match(/([^\/]*):([^\/]*)\/([^\/]*)$/);
        var eventType = idValue[2];
        
        // only for CommitEvents right now - need to bust out other events as options
        switch(eventType) {
          case 'CommitEvent':
            if (url == "") {
              url = item[i].link.href;
              titleText = " committed to ";
              return url;
              return titleText;
            }
            break;
          case 'FollowEvent':
            //
            break;
          case 'GistEvent':
            //
            break;
          case 'WikiEvent':
            //
            break;
        }
      });
      
      // grabbing details from URL
      var v = url.match(/http:\/\/github.com\/([^\/]*)\/([^\/]*)\/commit\/([^\/]*)$/);
      var user = v[1];
      var repo = v[2];
      var id = v[3];
      var repoName = user + "/" + repo;
      
      $.getJSON("http://github.com/api/v1/json/" + repoName + "/commit/" + id + "?callback=?",
        function(data){
          
          // creating initial elements
          var dl = $(document.createElement("dl"));
          var dt = $(document.createElement("dt"));
          var dtStrong = $(document.createElement("strong"));
        
          // linking username with REL microformat
          var dtUser = $(document.createElement("a"));
          dtUser.attr("href", "http://github.com/" + username).attr("rel", "me").text(username);
          
          // Adding username and eventText to dtStrong
          dtStrong.append(dtUser).append(titleText);
          
          // Linking repo name
          var repoAnchor = $(document.createElement("a"));
          repoAnchor.attr("href", "http://github.com/" + repoName).text(repoName);
          dtStrong.append(repoAnchor);
          
          // Building date for prettifying
          var dateSpan = $(document.createElement("span"));
          var dateTime = parseDate(data.commit.committed_date); // converts date to format Pretty Date recognises
          dateSpan.addClass("date").text(dateTime).attr("title", dateTime);
          dateSpan.prettyDate();
          setInterval(function(){ dateSpan.prettyDate(); }, 5000);
          
          // add dtStrong and dateSpan to DT
          dt.append(dtStrong).append(dateSpan);
          
          // Linking message to commit
          var ddMessage = $(document.createElement("dd"));
          messageAnchor = $(document.createElement("a"));
          messageAnchor.attr("href", url).text(data.commit.message);
          ddMessage.append(messageAnchor);
          
          // Listing all changed files
          var ddFilenames = $(document.createElement("dd"));
          ddFilenames.addClass("filenames");
          var filenamesUl = $(document.createElement("ul"));
          $(data.commit.modified).each(function(i){
            filenamesUl.append($(document.createElement("li")).text(data.commit.modified[i].filename));
          });
          ddFilenames.append(filenamesUl);
          
          // Adding content to DL
          dl.append(dt).append(ddMessage).append(ddFilenames);
          
          // Replacing static HTML with new hottness
          $("#github p").replaceWith(dl);
          
        }
      );
    }
  );
}
 
function parseDate(dateTime) {
  var timeZone = 10; // or "-3" as appropriate
  
  // TODO: need to add date changing functionality too
  dateTime = dateTime.substring(0,19) + "Z";
  var theirTime = dateTime.substring(11,13);
  var ourTime = parseInt(theirTime) + 7 + timeZone;
  if (ourTime > 24) {
    ourTime = ourTime - 24;
  };
  dateTime = dateTime.replace("T" + theirTime, "T" + ourTime);
  return dateTime;
};