forked from vn7n24fzkq/github-profile-summary-cards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cdd595
commit e3bf5c0
Showing
3 changed files
with
103 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -10,5 +10,8 @@ | |
"github" | ||
], | ||
"author": "vn7 <[email protected]>", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"dependencies": { | ||
"axios": "^0.19.2" | ||
} | ||
} |
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,87 @@ | ||
const request = require("../../utils/request"); | ||
|
||
const fetcher = (token, variables) => { | ||
//contain private repo need token permission | ||
return request( | ||
{ | ||
Authorization: `bearer ${token}`, | ||
}, | ||
{ | ||
query: ` | ||
query userInfo($login: String!,$endCursor: String) { | ||
user(login: $login) { | ||
repositories(isFork: false, first: 100, after: $endCursor,ownerAffiliations: OWNER) { | ||
nodes { | ||
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) { | ||
edges { | ||
size | ||
node { | ||
color | ||
name | ||
} | ||
} | ||
} | ||
} | ||
pageInfo{ | ||
endCursor | ||
hasNextPage | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
variables, | ||
} | ||
); | ||
}; | ||
|
||
//repos per language | ||
async function getRepoLanguage(username,token) { | ||
let hasNextPage = true; | ||
let cursor = null; | ||
let languageMap = new Map(); | ||
let nodes=[]; | ||
|
||
|
||
try { | ||
while (hasNextPage) { | ||
let res = await fetcher(token, { | ||
login: username, | ||
endCursor: cursor, | ||
}); | ||
|
||
if (res.data.errors) { | ||
throw Error(res.data.errors[0].message || "Could not fetch user"); | ||
} | ||
cursor = res.data.data.user.repositories.pageInfo.endCursor; | ||
hasNextPage = res.data.data.user.repositories.pageInfo.hasNextPage; | ||
nodes.push(...res.data.data.user.repositories.nodes); | ||
} | ||
|
||
nodes.forEach(node=>{ | ||
node.languages.edges.forEach(edge=>{ | ||
let langName = edge.node.name; | ||
console.log(languageMap.has(langName)); | ||
if(languageMap.has(langName)){ | ||
let lang = languageMap.get(langName); | ||
lang.count+=1; | ||
languageMap.set(langName, lang); | ||
}else{ | ||
languageMap.set(langName, {count:1,color:edge.node.color}); | ||
} | ||
}); | ||
}); | ||
|
||
} catch (e) { | ||
console.log("error"); | ||
if(e.response){ | ||
console.log(e.response.data); | ||
}else{ | ||
console.log(e); | ||
} | ||
} | ||
|
||
return languageMap; | ||
} | ||
|
||
module.exports = getRepoLanguage; |
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,12 @@ | ||
const axios = require("axios"); | ||
|
||
function request(header,data) { | ||
return axios({ | ||
url: "https://api.github.com/graphql", | ||
method: "post", | ||
headers: header, | ||
data: data, | ||
}); | ||
} | ||
|
||
module.exports = request; |