aboutsummaryrefslogtreecommitdiff
path: root/api/github.js
blob: 318d084b79f5b3322527f459b9fdf04c7d9d6d2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const { Octokit: OctokitRest } = require("@octokit/rest");
const fetch = (...args) =>
  import("node-fetch").then(({ default: fetch }) => fetch(...args));

require("dotenv").config();
class Github {
  octokit = null;
  username = null;
  constructor(username) {
    this.octokitRest = new OctokitRest({
      auth: process.env.GITHUB_TOKEN,
    });
    this.username = username;
  }
  async getRepos(page) {
    const { data } = await this.octokitRest.repos.listForUser({
      username: this.username,
      type: "all",
      sort: "updated",
      per_page: 10,
      page: page,
    });
    return data;
  }

  async getUserDetails() {
    const { data } = await this.octokitRest.users.getByUsername({
      username: this.username,
    });
    return data;
  }

  async getOneYearUserContributions() {
    const body = {
      query: `query {
                user(login: "${this.username}") {
                name
                contributionsCollection {
                    contributionCalendar {
                    colors
                    totalContributions
                    weeks {
                        contributionDays {
                        color
                        contributionCount
                        date
                        weekday
                        }
                        firstDay
                    }
                    }
                }
                }
            }`,
    };
    const headers = {
      Authorization: `bearer ${process.env.GITHUB_TOKEN}`,
    };
    const response = await fetch("https://api.github.com/graphql", {
      method: "POST",
      body: JSON.stringify(body),
      headers: headers,
    });
    const data = await response.json();
    const contributionData = [];
    data.data.user.contributionsCollection.contributionCalendar.weeks.forEach(
      (week) => {
        let weeklyContributionCount = 0;

        week.contributionDays.forEach((day) => {
          weeklyContributionCount += day.contributionCount;
        });
        contributionData.push(weeklyContributionCount);
      }
    );
    return contributionData;
  }
}

exports.Github = Github;