Skip to content

Commit dbc2949

Browse files
committed
adds firebase function to fetch github projects (#58)
1 parent 452783d commit dbc2949

File tree

13 files changed

+6163
-617
lines changed

13 files changed

+6163
-617
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/bazel-*
2-
/node_modules
2+
**/node_modules
33
/user.bazelrc
44
/profile.json
55
/.firebase
6+
**-debug.log
67

78
# Generated reference XML used during development
89
/src/assets/_devref

firebase.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"$schema": "./node_modules/firebase-tools/schema/firebase-config.json",
3+
"functions": {
4+
"runtime": "nodejs16"
5+
},
36
"hosting": {
7+
"site": "ecsact-dev",
48
"public": "public",
59
"ignore": [
610
"firebase.json",

functions/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const https = require('https');
2+
const functions = require('firebase-functions');
3+
const {defineSecret} = require('firebase-functions/params');
4+
const SEAUBOT_GITHUB_TOKEN = defineSecret('SEAUBOT_GITHUB_TOKEN');
5+
6+
const fetchQuery = `query {
7+
organization(login:"ecsact-dev") {
8+
projectsV2(orderBy: {field:TITLE, direction:ASC}, first:10, query:"is:open") {
9+
nodes {
10+
number
11+
title
12+
shortDescription
13+
readme
14+
}
15+
}
16+
}
17+
}`;
18+
19+
exports.fetchProjects = functions
20+
.runWith({secrets: [SEAUBOT_GITHUB_TOKEN]})
21+
.https.onRequest((req, res) => {
22+
res.set('Access-Control-Allow-Origin', '*');
23+
res.set('Access-Control-Allow-Methods', 'GET');
24+
if (req.method === 'OPTIONS') {
25+
res.status(204).end('');
26+
return;
27+
}
28+
29+
const ghReq = https.request({
30+
host: 'api.github.com',
31+
path: '/graphql',
32+
method: 'POST',
33+
headers: {
34+
'User-Agent': 'Ecsact Dev Cloud Function',
35+
'Content-Type': 'application/json',
36+
Authorization: `bearer ${SEAUBOT_GITHUB_TOKEN.value()}`,
37+
},
38+
});
39+
40+
ghReq.on('response', ghRes => {
41+
let msg = '';
42+
ghRes.setEncoding('utf8');
43+
ghRes.on('error', () => {
44+
res.status(500).end(`Error GitHub Response: ${err.message}`);
45+
});
46+
47+
ghRes.on('data', chunk => {
48+
msg += chunk;
49+
});
50+
ghRes.on('end', () => {
51+
res.status(ghRes.statusCode);
52+
res.set('Content-Type', 'application/json');
53+
if (ghRes.statusCode === 200) {
54+
res.set('Cache-Control', 'public, max-age=86400');
55+
}
56+
res.end(msg);
57+
});
58+
});
59+
60+
ghReq.on('error', err => {
61+
res.status(500).end(`Error GitHub Request: ${err.message}`);
62+
});
63+
64+
ghReq.end(JSON.stringify({query: fetchQuery}));
65+
});

0 commit comments

Comments
 (0)