Luccas Mateus 20ac80a5e8
Example of how to create a data portal in 5 minutes (#769)
* [example][m] - start of a simple-example

* Empty-Commit

* [simple-example][sm] - change from repos.json to datasets.json

* [example][m] - changed styling and added octokit

* [build][sm] - fix build
2023-04-18 13:51:48 -03:00

61 lines
1.7 KiB
TypeScript

import * as crypto from "crypto";
import axios from "axios";
import { Octokit } from "octokit"
export default class Project {
id: string;
name: string;
owner: string;
github_repo: string;
readme: string;
metadata: any;
repo_metadata: any;
constructor(owner: string, name: string) {
this.name = name;
this.owner = owner;
this.github_repo = `https://github.com/${owner}/${name}`;
// TODO: using the GitHub repo to set the id is not a good idea
// since repos can be renamed and then we are going to end up with
// a duplicate
const encodedGHRepo = Buffer.from(this.github_repo, "utf-8").toString();
this.id = crypto.createHash("sha1").update(encodedGHRepo).digest("hex");
}
initFromGitHub = async () => {
const octokit = new Octokit()
// TODO: what if the repo doesn't exist?
await this.getFileContent("README.md")
.then((content) => (this.readme = content))
.catch((e) => (this.readme = null));
await this.getFileContent("datapackage.json")
.then((content) => (this.metadata = content))
.catch((e) => (this.metadata = {}));
const github_metadata = await octokit.rest.repos.get({ owner: this.owner, repo: this.name })
this.repo_metadata = github_metadata.data ? github_metadata.data : null
};
getFileContent = (path, branch = "main") => {
return axios
.get(
`https://raw.githubusercontent.com/${this.owner}/${this.name}/${branch}/${path}`
)
.then((res) => res.data);
};
serialize() {
return JSON.parse(JSON.stringify(this));
}
static async getFromGitHub(owner: string, name: string) {
const project = new Project(owner, name);
await project.initFromGitHub();
return project;
}
}