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

✨ Initiate project for getting statistics for all the year #38

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GITHUB_ID=<github_id>
GITHUB_OAUTH=<oauth to access the API>
GITHUB_ORGA=<name of the organization>
GITHUB_WEBSITE=<repository name of the website>
GITHUB_WEBSITE_ORGA=<name of the website orga>
GITLAB_TOKEN=<GitLab token>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ typings/

# dotenv environment variables file
.env
hacktoberfest/.env

# next.js build output
.next
Expand Down
7 changes: 7 additions & 0 deletions hacktoberfest/README
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 😅)
136 changes: 136 additions & 0 deletions hacktoberfest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
require('dotenv').config();

const fetch = require('node-fetch')
const { default: ApolloClient, gql } = require('apollo-boost')

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,
})

hacktoberfest(2023)

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(),
}

async function hacktoberfest (year) {

console.log("🤖 Start getting data for " + year + " ... ");

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.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: "2023-01-01T00:00:00Z"
to: "2023-12-31T23:59:59Z"
) {
pullRequestContributions(first: 1) {
totalCount
}
}
}
}
`,
variables: {
login: infosUser[1].github.handle,
year: year
},
})

// 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: "2023-01-01T00:00:00+00:00"
createdBefore: "2023-12-31T00:00:00+00:00"
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("\n" + 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)

console.log("🤖 End ...");
}
Loading