From d6ba80876fbe8f58bdcf222b1ee04b0ff1ea1151 Mon Sep 17 00:00:00 2001 From: Marian Zeis <13335743+marianfoo@users.noreply.github.com> Date: Sun, 1 May 2022 13:37:14 +0200 Subject: [PATCH] feat(clones): add clones history for generators (#9) --- data/clones.json | 2 ++ src/gh-repos.ts | 34 ++++++++++++++++++++++++++++++++-- src/types.d.ts | 11 +++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 data/clones.json diff --git a/data/clones.json b/data/clones.json new file mode 100644 index 00000000..32960f8c --- /dev/null +++ b/data/clones.json @@ -0,0 +1,2 @@ +[ +] \ No newline at end of file diff --git a/src/gh-repos.ts b/src/gh-repos.ts index 465abe7c..16be0242 100644 --- a/src/gh-repos.ts +++ b/src/gh-repos.ts @@ -4,12 +4,14 @@ import { throttling } from "@octokit/plugin-throttling"; const MyOctokit = Octokit.plugin(throttling); import * as jsdoc2md from "jsdoc-to-markdown"; import * as yaml from "js-yaml"; +import { readFileSync, writeFileSync } from "fs"; -import { IPackage, Jsdoc, JsdocType, Params, Source, SubPackage, UI5Yaml } from "./types"; +import { ClonesJson, IPackage, Jsdoc, JsdocType, Params, Source, SubPackage, UI5Yaml } from "./types"; import Package from "./Package"; export default class GitHubRepositoriesProvider { static source = "github-packages"; + static clonesJson: ClonesJson[] = []; static octokit = new MyOctokit({ auth: process.env.GITHUB_TOKEN, @@ -31,6 +33,8 @@ export default class GitHubRepositoriesProvider { }); static async get(sources: Source[]): Promise { + const json = readFileSync(`${__dirname}/../data/clones.json`, { encoding: "utf8", flag: "r" }); + this.clonesJson = JSON.parse(json) as ClonesJson[]; const packages: IPackage[] = []; for (const source of sources) { @@ -62,7 +66,7 @@ export default class GitHubRepositoriesProvider { packages.push(packageInfo); } } - + writeFileSync(`${__dirname}/../data/clones.json`, JSON.stringify(this.clonesJson)); return packages; } @@ -82,6 +86,11 @@ export default class GitHubRepositoriesProvider { console.log(`Error while fetching last commit date for ${source.path}`); packageObject.updatedAt = repo.data.updated_at; } + try { + await this.updateCloningStats(source); + } catch (error) { + console.log(error); + } } else { packageObject.updatedAt = repo.data.updated_at; } @@ -258,4 +267,25 @@ export default class GitHubRepositoriesProvider { return latestCommit.data.committer.date; } + + static async updateCloningStats(source: Source): Promise { + const clonesRawData = await GitHubRepositoriesProvider.octokit.rest.repos.getClones({ + owner: source.owner, + repo: source.repo, + }); + let clonesGithubData: ClonesJson = clonesRawData.data as ClonesJson; + let cloneHistory = this.clonesJson.filter((clone: any) => clone.name === source.repo); + if (cloneHistory.length === 0) { + clonesGithubData.name = source.repo; + this.clonesJson.push(clonesGithubData); + } else { + for (const cloneDate of clonesGithubData.clones) { + // find objects with same timestamp in clones.data + const newCloneDate = cloneHistory[0].clones.filter((clone: any) => clone.timestamp === cloneDate.timestamp); + if (newCloneDate.length === 0) { + cloneHistory[0].clones.push(cloneDate); + } + } + } + } } diff --git a/src/types.d.ts b/src/types.d.ts index 51f1e148..a356e6fc 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -143,3 +143,14 @@ export interface NPMVersions { date: string; version: string; } + +export interface ClonesJson { + name: string; + count: number; + uniques: number; + clones: { + timestamp: string; + count: number; + uniques: number; + }[]; +}