migrate JavaScript to TypeScript

This commit is contained in:
tdurieux
2021-08-11 18:18:45 +02:00
parent ee4a20286d
commit caeff49ab0
58 changed files with 6034 additions and 3096 deletions

83
src/source/GitHubBase.ts Normal file
View File

@@ -0,0 +1,83 @@
import AnonymizedFile from "../AnonymizedFile";
import { Branch, Tree } from "../types";
import { GitHubRepository } from "./GitHubRepository";
import config from "../../config";
import { OAuthApp } from "@octokit/oauth-app";
import Repository from "../Repository";
import * as stream from "stream";
import UserModel from "../database/users/users.model";
export default abstract class GitHubBase {
type: "GitHubDownload" | "GitHubStream" | "Zip";
githubRepository: GitHubRepository;
branch: Branch;
accessToken: string;
repository: Repository;
constructor(
data: {
type: "GitHubDownload" | "GitHubStream" | "Zip";
branch?: string;
commit?: string;
repositoryId?: string;
repositoryName?: string;
accessToken?: string;
},
repository: Repository
) {
this.type = data.type;
this.accessToken = data.accessToken;
this.githubRepository = new GitHubRepository({
name: data.repositoryName,
externalId: data.repositoryId,
branches: [{ commit: data.commit, name: data.branch }],
});
this.repository = repository;
this.branch = { commit: data.commit, name: data.branch };
}
async getFileContent(file: AnonymizedFile): Promise<stream.Readable> {
throw new Error("Method not implemented.");
}
getFiles(): Promise<Tree> {
throw new Error("Method not implemented.");
}
async getToken(owner?: string) {
if (owner) {
const user = await UserModel.findOne({ username: owner });
if (user && user.accessToken) {
return user.accessToken as string;
}
}
if (this.accessToken) {
try {
const app = new OAuthApp({
clientType: "github-app",
clientId: config.CLIENT_ID,
clientSecret: config.CLIENT_SECRET,
});
await app.checkToken({
token: this.accessToken,
});
return this.accessToken;
} catch (error) {
// console.debug("Token is invalid.", error);
this.accessToken = config.GITHUB_TOKEN;
}
}
return config.GITHUB_TOKEN;
}
get url() {
return "https://github.com/" + this.githubRepository.fullName;
}
toJSON(): any {
return {
type: this.type,
fullName: this.githubRepository.fullName?.toString(),
branch: this.branch,
};
}
}