Skip to content

Instantly share code, notes, and snippets.

@camilleriluke
Created April 16, 2019 15:11
Show Gist options
  • Save camilleriluke/a624820da82950eaa8c07ca7cd150b20 to your computer and use it in GitHub Desktop.
Save camilleriluke/a624820da82950eaa8c07ca7cd150b20 to your computer and use it in GitHub Desktop.
github labels on for gmail (https://script.google.com/u/1/home/my)
var PARENT_LABEL = 'GH';
var GITHUB_NOTIFICATION_EMAIL = '[email protected]';
var ISSUE_STRING = "\"description\": \"View this Issue on GitHub\"";
var PR_STRING = "\"description\": \"View this Pull Request on GitHub\"";
function addGitHubLabels() {
// Let's do some set up here
createLabelIfNeeded(PARENT_LABEL);
// Get the last batch of threads
// Ideally we change this to a batching process
//var threads = GmailApp.getInboxThreads();
var threads = getThreadsSearch();
for (var i = 0; i < Math.min(threads.length, 100); i++) {
var currentThread = threads[i];
// If for some strange reason the thread does not hold any messages we can skip it
if (currentThread.getMessageCount() <= 0) {
continue;
}
var messages = currentThread.getMessages()
// Are the messages from github?
var topMessage = messages[0];
if (!isMessageFromGithub(topMessage)) {
continue;
}
var threadSubject = currentThread.getFirstMessageSubject();
Logger.log(currentThread.getFirstMessageSubject());
var orgRepo = extractRepoLabel(threadSubject);
if (!orgRepo) {
throw 'Could not extract repo name for threadId: ' + currentThread.getId();
}
var [org, repo] = orgRepo.split('/');
createLabelIfNeeded([PARENT_LABEL, org].join('/'));
var repoLabel = createLabelIfNeeded([PARENT_LABEL, org, repo].join('/'));
currentThread.addLabel(repoLabel);
// var messageLines = topMessage.getPlainBody().split('\n');
// Logger.log(messageLines);
}
// Get threads
// Filter firstMessage is from github
// Add label if it does not exist
}
function getThreadsSearch () {
return GmailApp.search('newer_than:1d');
}
function extractRepoLabel(str) {
var REPO_EXTRACT_REGEX = /\[([.\w\/\-]+)\]/;
if (!REPO_EXTRACT_REGEX.test(str)) {
return null;
}
var [, repo] = str.match(REPO_EXTRACT_REGEX);
return repo;
}
function createLabelIfNeeded(labelName) {
var label = getLabel(labelName);
if (Boolean(label)) {
return label;
}
Logger.log('Creating label %s', labelName)
return createLabel(labelName);
}
function isMessageFromGithub(topMessage) {
var messageFrom = topMessage.getFrom();
Logger.log(messageFrom);
return searchInSubject(GITHUB_NOTIFICATION_EMAIL, messageFrom);
}
// Parent label exsist
function getLabel(labelName) {
return GmailApp.getUserLabelByName(labelName)
}
function parentLabelExist() {
return doesLabelExists(PARENT_LABEL)
}
function createLabel(labelName) {
return GmailApp.createLabel(labelName);
}
// Create parent label
function fromFromThread(thread) {
return thread.getMessages()[0].getFrom();
}
function isThreadFrom(thread, from) {
return thread.getMessages()[0].getFrom();
}
function searchInSubject(search, subject) {
return new RegExp(search).test(subject);
}
@camilleriluke
Copy link
Author

⚠️ this is far from completion, I am pasting this here for colleagues to look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment