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

feat(clones): add clones history for generators #9

Merged
merged 1 commit into from
May 1, 2022
Merged
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
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;
}[];
}