Skip to content

Commit

Permalink
feat: add scheduled:rfcs task
Browse files Browse the repository at this point in the history
Outputs a Slack-formatted message including open RFC issues in the Artsy GitHub org.

**Example**

```
$ artsy scheduled:rfcs
{"text":"There is one open RFC:","attachments":[{"fallback":"Open RFCs","color":"#36a64f","author_name":"jonallured","author_link":"https://github.com/jonallured","title":"RFC: Slight Rebranding for Artsy Engineering","title_link":"https://github.com/artsy/README/issues/364"}],"unfurl_links":false}
```
  • Loading branch information
dblandin committed Feb 8, 2021
1 parent bcf68ad commit bc4dd5d
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/commands/scheduled/rfcs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Command } from "@oclif/command"
import { GitHub } from "../../utils/github"

export default class RFCs extends Command {
static description = "lists open RFCs"

async run() {
require("dotenv").config()

const github = new GitHub()
const issues = await github.openRFCs()

if (issues.length === 0) {
const payload = JSON.stringify({
text: `No open RFCs this week.`,
})
this.log(payload)
return
}

const attachments = issues.map(issue => ({
fallback: "Open RFCs",
color: "#36a64f",
author_name: issue.author?.login,
author_link: issue.author?.url,
author_icon: issue.author?.avatarURL,
title: issue.title,
title_link: issue.url,
}))

const text =
issues.length === 1
? `There is one open RFC:`
: `There are ${issues.length} open RFCs:`

const payload = JSON.stringify({
text,
attachments,
unfurl_links: false,
})

this.log(payload)
}
}
62 changes: 62 additions & 0 deletions src/utils/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { graphql } from "@octokit/graphql"

interface Issue {
title: string
url: string
author: {
url: string
login: string
avatarURL: string
}
}

interface OpenRFCsGraphQLSearchResponse {
edges: Array<{ node: Issue }>
}

export class GitHub {
apiKey: string

constructor() {
if (!process.env.GITHUB_TOKEN) {
throw new Error("GITHUB_TOKEN env var required")
}

this.apiKey = process.env.GITHUB_TOKEN
}

async openRFCs(): Promise<Issue[]> {
const { search }: { search: OpenRFCsGraphQLSearchResponse } = await graphql(
`
{
search(
query: "org:Artsy label:RFC state:open"
type: ISSUE
first: 100
) {
edges {
node {
... on Issue {
title
url
author {
login
url
avatarUrl
}
}
}
}
}
}
`,
{
headers: {
authorization: `token ${this.apiKey}`,
},
}
)

return search.edges.map(edge => edge.node)
}
}
45 changes: 45 additions & 0 deletions test/commands/scheduled/rfcs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect, test } from "@oclif/test"

describe("scheduled:rfcs", () => {
beforeEach(() => {
process.env.GITHUB_TOKEN = "test"
})

afterEach(() => {
delete process.env.GITHUB_TOKEN
})

test
.nock("https://api.github.com", api =>
api.post("/graphql").reply(200, {
data: {
search: {
edges: [
{
node: {
title: "Test",
},
},
],
},
},
})
)
.stdout()
.command(["scheduled:rfcs"])
.it("returns Slack-formatted open RFCs message", ctx => {
expect(ctx.stdout.trim()).to.eq(
JSON.stringify({
text: "There is one open RFC:",
attachments: [
{
fallback: "Open RFCs",
color: "#36a64f",
title: "Test",
},
],
unfurl_links: false,
})
)
})
})

0 comments on commit bc4dd5d

Please sign in to comment.