-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_activity.module
More file actions
68 lines (57 loc) · 1.76 KB
/
github_activity.module
File metadata and controls
68 lines (57 loc) · 1.76 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
<?php
use Drupal\wconsumer\Wconsumer;
use Drupal\wconsumer\Service\Exception;
function github_activity_wconsumer_define_required_scopes() {
return array('gist');
}
/**
* Implements hook_block_info().
*/
function github_activity_block_info() {
$blocks = array();
$blocks['github_activity'] = array(
'info' => t('GitHub Activity'),
);
return $blocks;
}
function github_activity_block_view($block_name = '') {
if ($block_name != 'github_activity') {
return null;
}
$block = function($contents) {
return array(
'subject' => 'GitHub Activity',
'content' => array(
'#markup' => $contents,
),
);
};
$error = function($message) use ($block) {
return $block('<div class="messages error">'.htmlspecialchars($message).'</div>');
};
$api = null;
try {
$api = Wconsumer::$github->api(NULL, array('gist'));
}
catch (Exception\NotLoggedInUser $e) {
return $error("Please sign up or log in to see your GitHub activity");
}
catch (Exception\ServiceInactive $e) {
return $error("The GitHub service integration is currently deactivated by the website administrator");
}
catch (Exception\NoUserCredentials $e) {
return $error("Before you can see your GitHub activity you need to connect with GitHub in your profile");
}
catch (Exception\AdditionalScopesRequired $e) {
return $error("Please re-connect to GitHub in your profile to see your github activity");
}
$events = null;
try {
$user = $api->get('/user')->send()->json();
$events = $api->get("/users/{$user['login']}/events")->send()->json();
}
catch (\Exception $e) {
return $error("Error while requesting GitHub. Please try again later.");
}
return $block('<pre>'.htmlspecialchars(var_export($events, true)).'</pre>');
}