-
Notifications
You must be signed in to change notification settings - Fork 5
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
jeanphi-baconnais
wants to merge
3
commits into
Zenika:main
Choose a base branch
from
jeanphi-baconnais:feat-add-function-hacktoberfest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 😅) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
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 gitlabToken = process.env.GITLAB_TOKEN | ||
|
||
const clientGitHub = new ApolloClient({ | ||
uri: `https://api.github.com/graphql?access_token=${githubToken}`, | ||
headers: { | ||
'User-Agent': githubId, | ||
Authorization: `token ${githubToken}`, | ||
}, | ||
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) | ||
} | ||
|
||
const handles = await fetch('https://oss.zenika.com/hacktoberfest-new.json').then( | ||
response => response.json(), | ||
) | ||
|
||
console.log(JSON.stringify(handles)) | ||
|
||
const data = await Promise.all( | ||
Object.entries(handles).map(async infosUser => { | ||
await sleep(25) | ||
try { | ||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same hardcoded comment |
||
state: merged | ||
) { | ||
count | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
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('error : ' + e + ' | ' + JSON.stringify(e)) | ||
return null | ||
} | ||
}), | ||
) | ||
|
||
console.log(JSON.stringify(data)) | ||
|
||
const filteredData = data.filter(Boolean) | ||
|
||
filteredData.forEach((user) => { | ||
user.location = handles[user[1].name] | ||
}) | ||
|
||
cache.data = filteredData | ||
cache.ttl = new Date().addHours(1) | ||
|
||
res.status(200).send(filteredData) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this can be set to current year instead of having 2022 hardcoded in it ?