Skip to content

Commit

Permalink
feat(clones): add clones history for generators (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
marianfoo authored May 1, 2022
1 parent 182e34c commit d6ba808
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions data/clones.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[
]
34 changes: 32 additions & 2 deletions src/gh-repos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,6 +33,8 @@ export default class GitHubRepositoriesProvider {
});

static async get(sources: Source[]): Promise<IPackage[]> {
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) {
Expand Down Expand Up @@ -62,7 +66,7 @@ export default class GitHubRepositoriesProvider {
packages.push(packageInfo);
}
}

writeFileSync(`${__dirname}/../data/clones.json`, JSON.stringify(this.clonesJson));
return packages;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -258,4 +267,25 @@ export default class GitHubRepositoriesProvider {

return latestCommit.data.committer.date;
}

static async updateCloningStats(source: Source): Promise<void> {
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);
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}[];
}

0 comments on commit d6ba808

Please sign in to comment.