Skip to content

Instantly share code, notes, and snippets.

@joshareed
Last active March 2, 2023 21:26
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joshareed/5706061 to your computer and use it in GitHub Desktop.
Save joshareed/5706061 to your computer and use it in GitHub Desktop.
Fetch list of issues from Github and draft an email
//#!/usr/bin/env groovy
import groovy.json.JsonSlurper
import java.text.SimpleDateFormat
// fetches a Github API URL and parses the result as JSON
def fetch(addr, params = [:]) {
def auth = "<personal api token>"
def json = new JsonSlurper()
return json.parse(addr.toURL().newReader(requestProperties: ["Authorization": "token ${auth}".toString(), "Accept": "application/json"]))
}
def format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'")
def repos = ['repo1', 'repo2', 'repo3']
// the last week
def now = new Date()
def since = format.format(now - 7)
// start building the email body
def subject = "IT Summary for ${(now - 7).format('MM/dd')} - ${now.format('MM/dd')}"
def body = new StringBuilder()
body.append("\n")
// fetch the list of completed issues in the last week
body.append("Completed\n")
fetch("https://api.github.com/issues?filter=all&state=closed&since=${since}").findAll { it?.repository?.name in repos }.each { issue ->
body.append("* ${issue.title} [${issue.repository.name}]\n")
}
body.append("\n")
// fetch the list of current priorities (labeled 'next')
body.append("Current Priorities\n")
fetch("https://api.github.com/issues?filter=all&state=open&labels=next").findAll { it?.repository?.name in repos }.each { issue ->
body.append("* ${issue.title} [${issue.repository.name}]\n")
}
// create a new draft email in Mail.app
def applescript = """
tell application "Mail"
make new outgoing message with properties {visible:true, subject:"${subject.replace('"', '\\"')}", content:"${body.toString().replace('"', '\\"')}"}
end tell
"""
["osascript", "-e", applescript].execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment