Last active
January 4, 2023 15:33
-
-
Save azu/d2c6b280453dcaf118479948ca8ab03c to your computer and use it in GitHub Desktop.
Google Apps Script: auto reply `@dependabot merge`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT LICENSE ©️ azu | |
// Usage: | |
// 1. Create `MERGE_REPLY_LABEL` | |
// 2. Upload GAS | |
// 3. Update CONFIG | |
// 4. Execute `createTimeTrigger` function on GAS | |
// === CONFIG ==== | |
// Interval hours | |
const FETCH_INTERVAL_HOURS = 12; | |
const YOUR_MAIL_ADDRESS = "{yourmail}@gmail.com" | |
const MERGE_REPLY_LABEL = "dependabot-merge"; | |
function fetchPublishedMails() { | |
const now = Math.floor(new Date().getTime() / 1000); | |
const timeTerm = now - (60 * 60 * FETCH_INTERVAL_HOURS); | |
// -label does not apply thread | |
// https://webapps.stackexchange.com/questions/62881/exclude-label-from-a-gmail-search | |
// -cc:comment@noreply.github.com | |
// ignore comment = thread | |
const strTerms = `after:${timeTerm} from:dependabot[bot] <notifications@github.com> deliveredto:${YOUR_MAIL_ADDRESS} -l:${MERGE_REPLY_LABEL} -cc:comment@noreply.github.com`; | |
console.log("Search: ", strTerms); | |
return GmailApp.search(strTerms); | |
} | |
/** | |
* Creates time triggers. | |
*/ | |
function createTimeTrigger() { | |
ScriptApp.newTrigger('main') | |
.timeBased() | |
.everyHours(FETCH_INTERVAL_HOURS) | |
.create(); | |
} | |
function main() { | |
const threadList = fetchPublishedMails() | |
if (threadList.length === 0) { | |
console.log("No new mail"); | |
return; | |
} | |
console.log("New mail threads: " + threadList.length); | |
if (threadList.length > 100) { | |
throw new Error("Too many replyTo"); | |
} | |
threadList.forEach((mainThreads, index) => { | |
const message = mainThreads.getMessages()?.[0]; | |
if (!message) { | |
return; | |
} | |
const replyTo = message.getReplyTo(); | |
console.log("replyTo", replyTo); | |
// need to embed replayTo into body for recognizing by GitHub | |
// dependabot[bot] ***@***.*** | |
message.reply(`@dependabot merge | |
${new Date().toLocaleString()} ${replyTo} | |
`); | |
mainThreads.addLabel(GmailApp.getUserLabelByName(MERGE_REPLY_LABEL)); | |
Utilities.sleep(1000); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment