Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord GFI webhook #12436

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ BUILD_LOCALES=

# If resource constraints are being hit during builds, change LIMIT_CPUS to a
# fixed number of CPUs (e.g. 2) to limit the demand during build time
LIMIT_CPUS=
LIMIT_CPUS=

# Discord environment (ID and token required for Discord webhook call)
# DISCORD_ID=
# DISCORD_TOKEN=

# Github token used for fetching good first issues
Comment on lines +35 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Discord environment (ID and token required for Discord webhook call)
# DISCORD_ID=
# DISCORD_TOKEN=
# Github token used for fetching good first issues
# ISSUES_GITHUB_TOKEN_READ_ONLY

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# ISSUES_GITHUB_TOKEN_READ_ONLY
22 changes: 22 additions & 0 deletions .github/workflows/gfi-discord-webhook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: GFI Discord webhook

on:
schedule:
- cron: "0 * * * *"
workflow_dispatch:

jobs:
build:
name: GFI Discord webhook
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
pettinarip marked this conversation as resolved.
Show resolved Hide resolved
- uses: actions/setup-node@v1
with:
node-version: 18
pettinarip marked this conversation as resolved.
Show resolved Hide resolved
- run: yarn install
- run: yarn discord-issues
env:
DISCORD_ID: ${{ secrets.DISCORD_ID }}
DISCORD_TOKEN: ${{ secrets.DISCORD_TOKEN }}
ISSUES_GITHUB_TOKEN: ${{ secrets.ISSUES_GITHUB_TOKEN_READ_ONLY }}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"chromatic": "chromatic --project-token fee8e66c9916",
"crowdin-clean": "rm -rf .crowdin && mkdir .crowdin",
"crowdin-import": "ts-node src/scripts/crowdin-import.ts",
"markdown-checker": "ts-node -O '{ \"module\": \"commonjs\" }' src/scripts/markdownChecker.ts"
"markdown-checker": "ts-node -O '{ \"module\": \"commonjs\" }' src/scripts/markdownChecker.ts",
"discord-issues": "ts-node -O '{ \"module\": \"commonjs\" }' src/scripts/gfi-discord-webhook"
},
"dependencies": {
"@chakra-ui/react": "^2.8.0",
Expand Down
40 changes: 40 additions & 0 deletions src/lib/api/fetchGFIs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const owner = "ethereum"
const repo = "ethereum-org-website"
const label = "good first issue"

type GHIssue = {
title: string
html_url: string
created_at: string
user: {
login: string
html_url: string
avatar_url: string
}
}

export const fetchGFIs = async (since: string) => {
const url = `https://api.github.com/repos/${owner}/${repo}/issues?labels=${encodeURIComponent(
label
)}&since=${since}&state=open&sort=created&direction=desc`

try {
const response = await fetch(url, {
headers: {
Authorization: `token ${process.env.ISSUES_GITHUB_TOKEN_READ_ONLY}`,
Accept: "application/vnd.github.v3+json",
},
})

if (!response.ok) {
throw new Error(
`GitHub API responded with ${response.status}: ${response.statusText}`
)
}

return (await response.json()) as GHIssue[]
} catch (error) {
console.error(error)
return []
}
}
63 changes: 63 additions & 0 deletions src/scripts/gfi-discord-webhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as dotenv from "dotenv"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we won't need this

Suggested change
import * as dotenv from "dotenv"


Comment on lines +1 to +2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the getTranslationProgress.ts script we have import "dotenv/config", which points to node_modules/dotenv/config.d.ts containing export {};... This may be all we need to get the default env vars from the workflow

Suggested change
import * as dotenv from "dotenv"

import { fetchGFIs } from "../lib/api/fetchGFIs"

dotenv.config({ path: `.env.local` })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dotenv.config({ path: `.env.local` })
dotenv.config()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dotenv.config({ path: `.env.local` })
import "dotenv/config"


const run = async () => {
// Calculate the start of the last hour
const now = new Date()
const sinceDate = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate(),
now.getHours() - 1
).toISOString()

const issues = await fetchGFIs(sinceDate)

if (!issues.length) {
console.log("No new good first issues found.")
return
}

const embeds = issues.map((issue) => ({
title: issue.title,
url: issue.html_url,
timestamp: issue.created_at,
color: 10181046,
footer: {
text: "Good First Issue",
},
author: {
name: issue.user.login,
url: issue.user.html_url,
icon_url: issue.user.avatar_url,
},
}))
const message = {
content:
issues.length > 1
? `## (${issues.length}) New good first issues! 🎉`
: "## New good first issue! 🎉",
embeds,
}

const webhookUrl = `https://discord.com/api/webhooks/${process.env.DISCORD_ID}/${process.env.DISCORD_TOKEN}`

const res = await fetch(webhookUrl, {
method: "post",
body: JSON.stringify(message),
headers: { "Content-Type": "application/json" },
})

if (!res.ok) {
const error = await res.json()
console.log(error, res)
throw new Error(`Error: ${res.status} ${res.statusText}`)
}

console.log("Message sent successfully!")
}

run()
Loading