From 5a9cbef00b40c9208ee862e6e23c4ba21a1330e5 Mon Sep 17 00:00:00 2001 From: ryo-ma Date: Sun, 23 Aug 2020 01:22:20 +0900 Subject: [PATCH] first commit --- .gitignore | 2 ++ api/index.ts | 8 +++++ deps.ts | 1 + src/github_api_client.ts | 77 ++++++++++++++++++++++++++++++++++++++++ src/icons.ts | 0 src/utils.ts | 0 6 files changed, 88 insertions(+) create mode 100644 .gitignore create mode 100644 api/index.ts create mode 100644 deps.ts create mode 100644 src/github_api_client.ts create mode 100644 src/icons.ts create mode 100644 src/utils.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a5c8936 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +.env \ No newline at end of file diff --git a/api/index.ts b/api/index.ts new file mode 100644 index 0000000..ffe9293 --- /dev/null +++ b/api/index.ts @@ -0,0 +1,8 @@ +import { serve } from "./../deps.ts"; +import { GithubAPIClient } from './../src/github_api_client.ts'; +const client = new GithubAPIClient(); +await client.requestUserData("ryo-ma"); +const server = serve({ port: 8080 }); +for await (const req of server) { + req.respond({ body: "

TEST

"}); +} \ No newline at end of file diff --git a/deps.ts b/deps.ts new file mode 100644 index 0000000..cb40975 --- /dev/null +++ b/deps.ts @@ -0,0 +1 @@ +export { serve } from "https://deno.land/std@0.65.0/http/server.ts"; \ No newline at end of file diff --git a/src/github_api_client.ts b/src/github_api_client.ts new file mode 100644 index 0000000..9c4f267 --- /dev/null +++ b/src/github_api_client.ts @@ -0,0 +1,77 @@ +import ky from "https://deno.land/x/ky/index.js"; +import "https://deno.land/x/dotenv@v0.5.0/load.ts"; + +class UserInfo { + constructor( + public totalCommits: Number, + public totalFollowers: Number, + public totalIssues: Number, + public totalPullRequests: Number, + public totalStargazers: Number, + public totalRepositories: Number, + public totalContributed: Number, + ) { + } +} + +export class GithubAPIClient { + constructor() { + } + async requestUserData(username: string): Promise { + const token = Deno.env.get('GITHUB_TOKEN'); + const variables = { username: username }; + const query = ` + query userInfo($username: String!) { + user(login: $username) { + name + login + contributionsCollection { + totalCommitContributions + restrictedContributionsCount + } + repositoriesContributedTo(first: 1, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) { + totalCount + } + pullRequests(first: 1) { + totalCount + } + issues(first: 1) { + totalCount + } + followers { + totalCount + } + repositories(first: 100, ownerAffiliations: OWNER, isFork: false, orderBy: {direction: DESC, field: STARGAZERS}) { + totalCount + nodes { + stargazers { + totalCount + } + } + } + } + } + `; + const response = await ky.post( + "https://api.github.com/graphql", + { + json: { query: query, variables }, + headers: { Authorization: `bearer ${token}` }, + }, + ).json(); + const totalCommits = response.data.user.contributionsCollection.restrictedContributionsCount + response.data.user.contributionsCollection.totalCommitContributions; + const totalStargazers = response.data.user.repositories.nodes.reduce((prev: Number, node: any) => { + return prev + node.stargazers.totalCount; + }, 0); + const userInfo = new UserInfo( + totalCommits, + response.data.user.followers.totalCount, + response.data.user.issues.totalCount, + response.data.user.pullRequests.totalCount, + totalStargazers, + response.data.user.repositories.totalCount, + response.data.user.repositoriesContributedTo.totalCount, + ) + return userInfo; + } +} \ No newline at end of file diff --git a/src/icons.ts b/src/icons.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..e69de29