-
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.
Merge pull request #8 from vladaviedov/dev
0.4.0
- Loading branch information
Showing
9 changed files
with
129 additions
and
45 deletions.
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 |
---|---|---|
|
@@ -13,7 +13,10 @@ | |
"rules": { | ||
"indent": [ | ||
"error", | ||
"tab" | ||
"tab", | ||
{ | ||
"SwitchCase": 1 | ||
} | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
|
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 |
---|---|---|
@@ -1,17 +1,18 @@ | ||
# gh-lang-stats | ||
|
||
Calculate Language Stats for your GitHub Account. | ||
Calculate language usage statistics for your GitHub Account. | ||
|
||
## Running | ||
*TODO: Github Actions template* | ||
Github Actions | ||
- See [template repo](https://github.com/vladaviedov/ghls-template) | ||
|
||
Install Package | ||
* Install the package from GitHub repo | ||
* Copy a template and rename it `template.svg` | ||
* Generate a [personal access token](https://github.com/settings/tokens) | ||
* Run `npx gh-lang-stats` with the env `ACCESS_KEY` set to your PAT | ||
- Install the package from GitHub repo | ||
- Copy a template and rename it `template.svg` | ||
- Generate a [personal access token](https://github.com/settings/tokens) | ||
- Run `npx gh-lang-stats` with the env `ACCESS_KEY` set to your PAT | ||
|
||
## TODO | ||
* GitHub Actions Template | ||
* Rate Limits | ||
* Documentation | ||
* More config | ||
* Caching |
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
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,6 @@ | ||
export const config = { | ||
token: process.env.TOKEN, | ||
maxConcur: process.env.MAX_CONCUR ?? 5, | ||
inputFile: process.env.INPUT_FILE ?? "template.svg", | ||
outputFile: process.env.OUTPUT_FILE ?? "generated.svg" | ||
}; |
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
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
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,60 @@ | ||
import { restCommitInfo } from "./github-api.js"; | ||
import { config } from "./config.js"; | ||
|
||
const queue = []; | ||
|
||
export const loadCommits = async (client, repos) => { | ||
const promises = repos.map(async r => { | ||
const commits = await repoCommits(client, r); | ||
return { | ||
languages: r.languages.nodes, | ||
commits: commits | ||
}; | ||
}); | ||
|
||
processQueue(); | ||
|
||
const result = await Promise.all(promises); | ||
return result; | ||
}; | ||
|
||
const repoCommits = async (client, repo) => { | ||
const owner = repo.owner.login; | ||
const name = repo.name; | ||
const commitHashes = repo.defaultBranchRef.target.history.nodes; | ||
|
||
const promises = commitHashes.map(hash => enqueueHash(client, owner, name, hash.oid)); | ||
|
||
const commits = await Promise.all(promises); | ||
return commits; | ||
}; | ||
|
||
const enqueueHash = (client, owner, name, hash) => { | ||
return new Promise(resolve => { | ||
queue.push({ | ||
func: () => restCommitInfo(client, owner, name, hash), | ||
callback: resolve | ||
}); | ||
}); | ||
}; | ||
|
||
const processQueue = async () => { | ||
const workers = []; | ||
|
||
// Start initial workers | ||
for (let i = 0; i < config.maxConcur; i++) { | ||
workers.push(worker(queue.pop(), i)); | ||
} | ||
|
||
while (queue.length > 0) { | ||
// Wait for free slot | ||
const res = await Promise.any(workers); | ||
workers[res] = worker(queue.pop(), res); | ||
} | ||
}; | ||
|
||
const worker = async (request, index) => { | ||
const response = await request.func(); | ||
request.callback(response); | ||
return index; | ||
}; |
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 |
---|---|---|
@@ -1,33 +1,29 @@ | ||
#!/usr/bin/env node | ||
import { Octokit } from "octokit"; | ||
import { throttling } from "@octokit/plugin-throttling"; | ||
import { analyzeData } from "./analyze.js"; | ||
import { fillTemplate } from "./fill-template.js"; | ||
import { qlUserId, qlFullList, restCommitInfo } from "./github-api.js"; | ||
import { qlUserId, qlFullList } from "./github-api.js"; | ||
import { loadCommits } from "./load-commits.js"; | ||
import { config } from "./config.js"; | ||
|
||
const octokit = new Octokit({ | ||
auth: process.env.ACCESS_KEY | ||
const OctokitPlug = Octokit.plugin(throttling); | ||
const octokit = new OctokitPlug({ | ||
auth: config.token, | ||
throttle: { | ||
onRateLimit: retryAfter => { | ||
console.error(`Ratelimit hit. Waiting ${retryAfter} seconds`); | ||
return true; | ||
}, | ||
onSecondaryRateLimit: retryAfter => { | ||
console.error(`Secondary ratelimit hit. Waiting ${retryAfter} seconds`); | ||
return true; | ||
} | ||
} | ||
}); | ||
|
||
qlUserId(octokit).then(id => { | ||
return qlFullList(octokit, id); | ||
}).then(response => { | ||
const repos = response.nodes; | ||
return Promise.all(repos.map(async r => { | ||
return { | ||
languages: r.languages.nodes, | ||
commits: await repoCommits(r) | ||
}; | ||
})); | ||
}).then(analyzeData).then(fillTemplate); | ||
|
||
const repoCommits = repo => { | ||
const owner = repo.owner.login; | ||
const name = repo.name; | ||
const commitHashes = repo.defaultBranchRef.target.history.nodes; | ||
|
||
const promises = []; | ||
commitHashes.forEach(hash => { | ||
promises.push(restCommitInfo(octokit, owner, name, hash.oid)); | ||
}); | ||
return Promise.all(promises); | ||
}; | ||
qlUserId(octokit) | ||
.then(id => qlFullList(octokit, id)) | ||
.then(list => loadCommits(octokit, list)) | ||
.then(analyzeData) | ||
.then(fillTemplate); |
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