From 5d9a498e086937a5d6b125c0c695e9eba759ab1e Mon Sep 17 00:00:00 2001 From: "jeanphi.baconnais" Date: Tue, 8 Nov 2022 13:10:04 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=9A=80=20draft=20:=20add=20hacktoberf?= =?UTF-8?q?est=20function=20but=20it's=20a=20draft=20=F0=9F=94=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hacktoberfest/index.js | 94 ++++++++++++++++++++++++++++++++++++++ hacktoberfest/package.json | 10 ++++ 2 files changed, 104 insertions(+) create mode 100644 hacktoberfest/index.js create mode 100644 hacktoberfest/package.json diff --git a/hacktoberfest/index.js b/hacktoberfest/index.js new file mode 100644 index 0000000..f5785e4 --- /dev/null +++ b/hacktoberfest/index.js @@ -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) +} diff --git a/hacktoberfest/package.json b/hacktoberfest/package.json new file mode 100644 index 0000000..c378f27 --- /dev/null +++ b/hacktoberfest/package.json @@ -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" + } +} From e93e0ebb1498c092e4c131a980ffd5c128ff71c8 Mon Sep 17 00:00:00 2001 From: "jeanphi.baconnais" Date: Thu, 10 Nov 2022 13:44:39 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=8E=89=20add=20recuperation=20of=20MR?= =?UTF-8?q?=20in=20GitLab.=20But=20needs=20to=20change=20oss.zenika.com?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hacktoberfest/README | 7 ++++ hacktoberfest/index.js | 88 ++++++++++++++++++++++++++++++++---------- 2 files changed, 74 insertions(+), 21 deletions(-) create mode 100644 hacktoberfest/README diff --git a/hacktoberfest/README b/hacktoberfest/README new file mode 100644 index 0000000..ce2919e --- /dev/null +++ b/hacktoberfest/README @@ -0,0 +1,7 @@ +# Hacktoberfest + +This directory contains the Cloud function used to get number of contribution for people. + +The list of contributors is in the oss.zenika.com website. + +⚠️ No CI exists on this project, so the cloud function has to be deployed manually (but it's in progress 😅) diff --git a/hacktoberfest/index.js b/hacktoberfest/index.js index f5785e4..dd184ea 100644 --- a/hacktoberfest/index.js +++ b/hacktoberfest/index.js @@ -27,8 +27,9 @@ exports.hacktoberfest = async (req, res) => { const githubId = process.env.GITHUB_ID const githubToken = process.env.GITHUB_OAUTH + const gitlabToken = process.env.GITLAB_TOKEN - const client = new ApolloClient({ + const clientGitHub = new ApolloClient({ uri: `https://api.github.com/graphql?access_token=${githubToken}`, headers: { 'User-Agent': githubId, @@ -37,6 +38,16 @@ exports.hacktoberfest = async (req, res) => { fetch, }) + const clientGitLab = new ApolloClient({ + uri: `https://gitlab.com/api/graphql`, + headers: { + 'User-Agent': githubId, + Authorization: `Bearer ${gitlabToken}`, + 'Content-Type': 'application/json', + }, + fetch, + }) + if (cache.data.length > 0 && cache.ttl > new Date()) { return res.status(200).send(cache.data) } @@ -48,32 +59,67 @@ exports.hacktoberfest = async (req, res) => { console.log(JSON.stringify(handles)) const data = await Promise.all( - Object.keys(handles).map(async handle => { + Object.entries(handles).map(async infosUser => { 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 + if (infosUser[1].github && infosUser[1].github.handle) { + const responseGitHub = await clientGitHub.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: infosUser[1].github.handle, + }, + }) + + // update json + infosUser[1].github.nbContributions = + responseGitHub.data.user.contributionsCollection.pullRequestContributions.totalCount + } + + if (infosUser[1].gitlab && infosUser[1].gitlab.handle) { + const responseGitLab = await clientGitLab.query({ + query: gql` + query getUserMR($login: String!) { + users(usernames: [$login]) { + nodes { + username + authoredMergeRequests( + createdAfter: "2022-10-01T00:00:00+00:00" + createdBefore: "2022-10-31T00:00:00+00:00" + state: merged + ) { + count + } } } } - } - `, - variables: { - login: handle, - }, - }) - return response.data + `, + variables: { + login: infosUser[1].gitlab.handle, + }, + }) + + // update json + infosUser[1].gitlab.nbContributions = + responseGitLab.data.users.nodes[0].authoredMergeRequests.count + } + + return infosUser } catch (e) { - console.log(JSON.stringify(e)) + console.log('error : ' + e + ' | ' + JSON.stringify(e)) return null } }), From e8f01b54162f14cc3b7a0eedabefe59f1c81daf2 Mon Sep 17 00:00:00 2001 From: Jean-Phi Baconnais Date: Mon, 14 Nov 2022 11:35:26 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=9A=80=20maj=20cloud=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hacktoberfest/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hacktoberfest/index.js b/hacktoberfest/index.js index dd184ea..cb98eb9 100644 --- a/hacktoberfest/index.js +++ b/hacktoberfest/index.js @@ -52,7 +52,7 @@ exports.hacktoberfest = async (req, res) => { return res.status(200).send(cache.data) } - const handles = await fetch('https://oss.zenika.com/hacktoberfest.json').then( + const handles = await fetch('https://oss.zenika.com/hacktoberfest-new.json').then( response => response.json(), ) @@ -129,10 +129,10 @@ exports.hacktoberfest = async (req, res) => { const filteredData = data.filter(Boolean) - filteredData.forEach(({ user }) => { - user.location = handles[user.login] + filteredData.forEach((user) => { + user.location = handles[user[1].name] }) - + cache.data = filteredData cache.ttl = new Date().addHours(1)