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

🚀 Add hacktoberfest function on this repo #33

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
🚀 draft : add hacktoberfest function but it's a draft 🔥
jeanphi-baconnais committed Nov 8, 2022

Verified

This commit was signed with the committer’s verified signature.
Moritzoni Moritz
commit 5d9a498e086937a5d6b125c0c695e9eba759ab1e
94 changes: 94 additions & 0 deletions hacktoberfest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const fetch = require('node-fetch')
const { default: ApolloClient, gql } = require('apollo-boost')

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}

Date.prototype.addHours = function(h) {
this.setHours(this.getHours() + h)
return this
}

const cache = {
data: [],
ttl: new Date(),
}

exports.hacktoberfest = async (req, res) => {
res.set('Access-Control-Allow-Origin', '*')

if (req.method === 'OPTIONS') {
res.set('Access-Control-Allow-Methods', 'GET')
res.set('Access-Control-Allow-Headers', 'Content-Type')
res.set('Access-Control-Max-Age', '3600')
return res.status(204).send('')
}

const githubId = process.env.GITHUB_ID
const githubToken = process.env.GITHUB_OAUTH

const client = new ApolloClient({
uri: `https://api.github.com/graphql?access_token=${githubToken}`,
headers: {
'User-Agent': githubId,
Authorization: `token ${githubToken}`,
},
fetch,
})

if (cache.data.length > 0 && cache.ttl > new Date()) {
return res.status(200).send(cache.data)
}

const handles = await fetch('https://oss.zenika.com/hacktoberfest.json').then(
response => response.json(),
)

console.log(JSON.stringify(handles))

const data = await Promise.all(
Object.keys(handles).map(async handle => {
await sleep(25)
try {
const response = await client.query({
query: gql`
query getUserPullRequest($login: String!) {
user(login: $login) {
login
contributionsCollection(
from: "2022-10-01T00:00:00Z"
to: "2022-10-31T23:59:59Z"
) {
pullRequestContributions(first: 1) {
totalCount
}
}
}
}
`,
variables: {
login: handle,
},
})
return response.data
} catch (e) {
console.log(JSON.stringify(e))
return null
}
}),
)

console.log(JSON.stringify(data))

const filteredData = data.filter(Boolean)

filteredData.forEach(({ user }) => {
user.location = handles[user.login]
})

cache.data = filteredData
cache.ttl = new Date().addHours(1)

res.status(200).send(filteredData)
}
10 changes: 10 additions & 0 deletions hacktoberfest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"apollo-boost": "^0.4.4",
"graphql": "^14.5.6",
"graphql-tag": "^2.10.1",
"node-fetch": "^2.6.0"
}
}