Skip to content

Commit

Permalink
Merge pull request #222 from jaredwray/adding-in-github
Browse files Browse the repository at this point in the history
adding in github
  • Loading branch information
jaredwray authored Dec 10, 2023
2 parents 0ac68ca + 31494a5 commit 5e2cf83
Show file tree
Hide file tree
Showing 5 changed files with 1,513 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"writr": "./bin/writr.js"
},
"dependencies": {
"axios": "^1.6.2",
"ecto": "^2.2.4",
"feed": "^4.2.2",
"fs-extra": "^11.2.0",
Expand Down
73 changes: 73 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import axios from 'axios';

export type GithubOptions = {
api: string | undefined;
author: string;
repo: string;
};

export class Github {
options = {
api: 'https://api.github.com',
author: '',
repo: '',
};

constructor(options: GithubOptions) {
this.parseOptions(options);
}

async getData(): Promise<Record<string, unknown>> {
const data = {
releases: {},
contributors: {},
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
data.releases = await this.getReleases();
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
data.contributors = await this.getContributors();

return data;
}

async getReleases(): Promise<any> {
const url = `${this.options.api}/repos/${this.options.author}/${this.options.repo}/releases`;
try {
const result = await axios.get(url);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result.data;
} catch (error: unknown) {
const typedError = error as {response: {status: number}};
if (typedError.response?.status === 404) {
throw new Error(`Repository ${this.options.author}/${this.options.repo} not found.`);
}

throw error;
}
}

async getContributors(): Promise<any> {
const url = `${this.options.api}/repos/${this.options.author}/${this.options.repo}/contributors`;
try {
const result = await axios.get(url);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result.data;
} catch (error: unknown) {
const typedError = error as {response: {status: number}};
if (typedError.response?.status === 404) {
throw new Error(`Repository ${this.options.author}/${this.options.repo} not found.`);
}

throw error;
}
}

public parseOptions(options: GithubOptions) {
if (options.api) {
this.options.api = options.api;
}

this.options.author = options.author;
this.options.repo = options.repo;
}
}
65 changes: 65 additions & 0 deletions test/fixtures/data-mocks/github-contributors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[
{
"login": "jaredwray",
"id": 1205481,
"node_id": "MDQ6VXNlcjEyMDU0ODE=",
"avatar_url": "https://avatars.githubusercontent.com/u/1205481?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/jaredwray",
"html_url": "https://github.com/jaredwray",
"followers_url": "https://api.github.com/users/jaredwray/followers",
"following_url": "https://api.github.com/users/jaredwray/following{/other_user}",
"gists_url": "https://api.github.com/users/jaredwray/gists{/gist_id}",
"starred_url": "https://api.github.com/users/jaredwray/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/jaredwray/subscriptions",
"organizations_url": "https://api.github.com/users/jaredwray/orgs",
"repos_url": "https://api.github.com/users/jaredwray/repos",
"events_url": "https://api.github.com/users/jaredwray/events{/privacy}",
"received_events_url": "https://api.github.com/users/jaredwray/received_events",
"type": "User",
"site_admin": false,
"contributions": 788
},
{
"login": "christianllv",
"id": 30430879,
"node_id": "MDQ6VXNlcjMwNDMwODc5",
"avatar_url": "https://avatars.githubusercontent.com/u/30430879?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/christianllv",
"html_url": "https://github.com/christianllv",
"followers_url": "https://api.github.com/users/christianllv/followers",
"following_url": "https://api.github.com/users/christianllv/following{/other_user}",
"gists_url": "https://api.github.com/users/christianllv/gists{/gist_id}",
"starred_url": "https://api.github.com/users/christianllv/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/christianllv/subscriptions",
"organizations_url": "https://api.github.com/users/christianllv/orgs",
"repos_url": "https://api.github.com/users/christianllv/repos",
"events_url": "https://api.github.com/users/christianllv/events{/privacy}",
"received_events_url": "https://api.github.com/users/christianllv/received_events",
"type": "User",
"site_admin": false,
"contributions": 79
},
{
"login": "dwatklnsweb",
"id": 94058928,
"node_id": "U_kgDOBZs5sA",
"avatar_url": "https://avatars.githubusercontent.com/u/94058928?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/dwatklnsweb",
"html_url": "https://github.com/dwatklnsweb",
"followers_url": "https://api.github.com/users/dwatklnsweb/followers",
"following_url": "https://api.github.com/users/dwatklnsweb/following{/other_user}",
"gists_url": "https://api.github.com/users/dwatklnsweb/gists{/gist_id}",
"starred_url": "https://api.github.com/users/dwatklnsweb/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/dwatklnsweb/subscriptions",
"organizations_url": "https://api.github.com/users/dwatklnsweb/orgs",
"repos_url": "https://api.github.com/users/dwatklnsweb/repos",
"events_url": "https://api.github.com/users/dwatklnsweb/events{/privacy}",
"received_events_url": "https://api.github.com/users/dwatklnsweb/received_events",
"type": "User",
"site_admin": false,
"contributions": 3
}
]
Loading

0 comments on commit 5e2cf83

Please sign in to comment.