Skip to content

Commit

Permalink
add site webhook (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycouet committed May 17, 2023
1 parent b649784 commit f5af871
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
7 changes: 6 additions & 1 deletion site/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
VITE_SESSION_SECRET=MY_SUPER_VALUE
VITE_SESSION_SECRET=MY_SUPER_VALUE

# Requires the `repo` scope.
GITHUB_TOKEN_TRIGGER_SPONSORS=MY_SUPER_VALUE

WEBHOOK_PATH="/webhook/something"
32 changes: 32 additions & 0 deletions site/src/handles/webhookHandle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { GITHUB_TOKEN_TRIGGER_SPONSORS, WEBHOOK_PATH } from '$env/static/private'
import { json } from '@sveltejs/kit'

/** @type {import('@sveltejs/kit').Handle} */
export const webhookHandle = async ({ event, resolve }) => {
// if WEBHOOK_PATH is defined and the request match, let's go
if (WEBHOOK_PATH && event.url.pathname === WEBHOOK_PATH) {
// trigger the CI
const result = await event.fetch(
`https://api.github.com/repos/HoudiniGraphql/sponsors/actions/workflows/generate.yml/dispatches`,
{
method: 'POST',
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${GITHUB_TOKEN_TRIGGER_SPONSORS}`,
'X-GitHub-Api-Version': '2022-11-28'
},
body: JSON.stringify({ ref: 'main' })
}
)

let data = null
try {
data = await result.json()
} catch (error) {}

return json({ status: result.status, statusText: result.statusText, data })
}

const response = await resolve(event)
return response
}
4 changes: 4 additions & 0 deletions site/src/hooks.server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { redirect } from '@sveltejs/kit'
import { sequence } from '@sveltejs/kit/hooks'
import { webhookHandle } from './handles/webhookHandle'

export const handle = sequence(webhookHandle)

/** @type {import('@sveltejs/kit').HandleServerError} */
export function handleError({ error, event }) {
Expand Down
6 changes: 6 additions & 0 deletions site/src/routes/_sponsors/+page.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const load = async (event) => {
const result = await event.fetch(
'https://raw.githubusercontent.com/HoudiniGraphql/sponsors/main/generated/sponsors.svg'
)
return { svg: await result.text() }
}
34 changes: 34 additions & 0 deletions site/src/routes/_sponsors/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script>
import { browser } from '$app/environment'
import Logo from '~/components/Logo.svelte'
export let data
$: ui_theme = browser
? parseInt(document.cookie.match('(^|;)\\s*' + 'ui_theme' + '\\s*=\\s*([^;]+)')?.pop() || '0')
: data?.ui_theme
</script>
<a href="/">
<div style="margin: 14px">
<div style="display: flex; align-items: center; gap: 7px">
<Logo size={30} color={ui_theme === 0 ? 'white' : 'black'} />
<span class="logo-text">Houdini</span>
</div>
</div>
</a>
{@html data.svg}
<style>
.logo-text {
color: white;
font-size: 22px;
font-family: 'Hind', sans-serif;
font-weight: 600;
}
a {
text-decoration: none;
}
</style>
8 changes: 7 additions & 1 deletion site/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@
"~/*": ["src/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"]
"include": [
"src/**/*.d.ts",
"src/**/*.js",
"src/**/*.ts",
"src/**/*.svelte",
".svelte-kit/ambient.d.ts"
]
}

0 comments on commit f5af871

Please sign in to comment.