Skip to content

Instantly share code, notes, and snippets.

@joshareed
Last active March 2, 2023 21:26
Show Gist options
  • Select an option

  • Save joshareed/5706061 to your computer and use it in GitHub Desktop.

Select an option

Save joshareed/5706061 to your computer and use it in GitHub Desktop.

Revisions

  1. Josh Reed revised this gist Jun 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GithubClient.groovy
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/usr/bin/env groovy
    //#!/usr/bin/env groovy

    import groovy.json.JsonSlurper
    import java.text.SimpleDateFormat
  2. Josh Reed created this gist Jun 4, 2013.
    45 changes: 45 additions & 0 deletions GithubClient.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/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()