-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleUpGitHubAccounts.gs
124 lines (107 loc) · 2.92 KB
/
BubbleUpGitHubAccounts.gs
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
115
116
117
118
119
120
121
122
123
124
/**
* Watch a whole repository, but only see GitHub notifications created on behalf
* of specific users.
*
* Uses [Google Apps Script]{@link https://developers.google.com/apps-script/}
* to periodically read a Gmail label and move messages to the inbox.
*/
/**
* Holds the GitHub usernames of the people you want to bubble up.
* @type {string[]}
*/
var gitHubAccounts = [];
/**
* The name of the GMail label you want to bubble from.
* @type string
*/
var gmailLabel = '';
/**
* Google Apps Script Gmail API
* @external GmailApp
* @see {@link https://developers.google.com/apps-script/reference/gmail/}
*/
/**
* Google Apps Script Gmail Message Object
* @class GmailMessage
* @memberof external:GmailApp
* @see {@link https://developers.google.com/apps-script/reference/gmail/gmail-message}
*/
/**
* Google Apps Script Gmail Thread Object
* @class GmailThread
* @memberof external:GmailApp
* @see {@link https://developers.google.com/apps-script/reference/gmail/gmail-thread}
*/
/**
* Checks whether the given message should bubble.
*
* A message bubbles if it was sent to you explicitly by GitHub
* (`X-GitHub-Reason: ...`), or if it was sent on behalf of one of the
* whitelisted users from {@link gitHubAccounts}.
*
* @param {external:GmailApp.GmailMessage}
* @return {Boolean}
*/
function messageShouldBubble( message ) {
var raw = message.getRawContent();
if ( ~ raw.indexOf( 'X-GitHub-Reason:' ) ) {
return true;
}
return gitHubAccounts.some( function( gitHubAccount ) {
var sender = raw.indexOf( Utilities.formatString(
'X-GitHub-Sender: %s',
gitHubAccount
) );
var mention = raw.indexOf( Utilities.formatString(
'@%s',
gitHubAccount
) );
return ( ~ sender ) || ( ~ mention );
} );
}
/**
* Checks whether the given thread should bubble.
*
* A thread bubbles if it contains any message which bubbles.
* @see messageShouldBubble
*
* @param {external:GmailApp.GmailThread}
* @return {Boolean}
*/
function threadShouldBubble( thread ) {
var messages = thread.getMessages();
if ( ! messages ) {
thread.markImportant();
return false;
}
return messages.some( messageShouldBubble );
}
/**
* Runs the script.
*/
function main() {
/*
* Google App Scripts are fastest when read and write operations are
* batched. Alternating between read and write is slow, so we do write
* operation on many threads/messages at once rather than looping
* through them and reading from/writing to each in turn.
*/
// READ
var searchResults = GmailApp.search( Utilities.formatString(
'label:%s is:unread is:important -in:inbox',
gmailLabel
) );
if ( ! searchResults.length ) {
return;
}
// WRITE ALL
GmailApp.markThreadsUnimportant( searchResults );
// READ ALL
var moveToInbox = searchResults.filter( threadShouldBubble );
if ( ! moveToInbox.length ) {
return;
}
// WRITE ALL
GmailApp.moveThreadsToInbox( moveToInbox );
GmailApp.markThreadsImportant( moveToInbox );
}