Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include all commits from the year the user joined #223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default function cardStyle(data: GetData, uiConfig: UiConfig): string {
${gradientStops}
</linearGradient>
</defs>
<rect x="0.5" y="0.5" rx="${uiConfig.borderRadius}" height="99.6%" width="99.8%" fill="url(#${gradientId})" ${hideBorder}/>`;
<rect x="0.5" y="0.5" rx="${uiConfig.borderRadius}" height="99.4%" width="99.8%" fill="url(#${gradientId})" ${hideBorder}/>`;
}

// Generating SVG code for the background based on background color or gradient
Expand All @@ -158,7 +158,7 @@ export default function cardStyle(data: GetData, uiConfig: UiConfig): string {
backgroundSVG = generateGradient(gradientColors);
} else {
backgroundSVG = `
<rect x="0.5" y="0.5" rx="${uiConfig.borderRadius}" height="99.6%" width="99.8%" fill="#${uiConfig.bgColor}" ${hideBorder}/>`;
<rect x="0.5" y="0.5" rx="${uiConfig.borderRadius}" height="99.4%" width="99.8%" fill="#${uiConfig.bgColor}" ${hideBorder}/>`;
}
}
}
Expand Down
118 changes: 111 additions & 7 deletions src/fetcher/apiFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
repositories: Repositories;
followers: Followers;
following: Following;
contributionsCollection: ContributionsCollection;
openedIssues: OpenedIssues;
closedIssues: ClosedIssues;
pullRequests: PullRequests;
mergedPullRequests: MergedPullRequests;
discussionStarted: DiscussionStarted;
discussionAnswered: DiscussionAnswered;
repositoriesContributedTo: RepositoriesContributedTo;
totalCommitContributions: number;
restrictedContributionsCount: number;
totalPullRequestReviewContributions: number;
}

// Represents the total count of repositories
Expand Down Expand Up @@ -77,13 +79,101 @@
totalCount: number;
}

async function getUserJoinYear(username: string): Promise<number> {
const data = await axios({

Check warning on line 83 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L83

Added line #L83 was not covered by tests
method: "post",
url: "https://api.github.com/graphql",
headers: {
"User-Agent": "FajarKim/github-readme-profile",
Authorization: getRandomToken(true),
},
data: {
query: `query userInfo($username: String!) {
user(login: $username) {
createdAt
}
}`,
variables: {
username,
},
},
});

if (data.data.errors?.length > 0) {
throw new Error(data.data.errors[0].message);

Check warning on line 103 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L103

Added line #L103 was not covered by tests
}

const user = data.data.data.user;

Check warning on line 106 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L106

Added line #L106 was not covered by tests
if (!user || !user.createdAt) {
throw new Error("User data is missing.");

Check warning on line 108 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L108

Added line #L108 was not covered by tests
}

const joinDate = new Date(user.createdAt);
return joinDate.getFullYear();

Check warning on line 112 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L111-L112

Added lines #L111 - L112 were not covered by tests
}

async function fetchContributions(username: string, year: number): Promise<ContributionsCollection> {
const from = `${year}-01-01T00:00:00Z`;
const to = `${year}-12-31T23:59:59Z`;

Check warning on line 117 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L116-L117

Added lines #L116 - L117 were not covered by tests

const data = await axios({

Check warning on line 119 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L119

Added line #L119 was not covered by tests
method: "post",
url: "https://api.github.com/graphql",
headers: {
"User-Agent": "FajarKim/github-readme-profile",
Authorization: getRandomToken(true),
},
data: {
query: `query userInfo($username: String!, $from: DateTime!, $to: DateTime!) {
user(login: $username) {
contributionsCollection(from: $from, to: $to) {
totalCommitContributions
restrictedContributionsCount
totalPullRequestReviewContributions
}
}
}`,
variables: {
username,
from,
to,
},
},
});

if (data.data.errors?.length > 0) {
throw new Error(data.data.errors[0].message);

Check warning on line 145 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L145

Added line #L145 was not covered by tests
}

const contributions = data.data.data.user.contributionsCollection;
return {

Check warning on line 149 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L148-L149

Added lines #L148 - L149 were not covered by tests
totalCommitContributions: contributions.totalCommitContributions,
restrictedContributionsCount: contributions.restrictedContributionsCount,
totalPullRequestReviewContributions: contributions.totalPullRequestReviewContributions,
};
}

/**
* Fetches user data from the GitHub GraphQL API.
*
* @param {string} username GitHub username.
* @returns {Promise<User>} Promise representing the user data obtained from the GitHub Graphql API.
*/
export default async function apiFetch(username: string): Promise<User> {
const startYear = await getUserJoinYear(username);
const endYear = new Date().getFullYear();

Check warning on line 164 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L163-L164

Added lines #L163 - L164 were not covered by tests

let TotalCommitContributions = 0;
let RestrictedContributionsCount = 0;
let TotalPullRequestReviewContributions = 0;

Check warning on line 168 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L166-L168

Added lines #L166 - L168 were not covered by tests

for (let year = startYear; year <= endYear; year++) {
const contributions = await fetchContributions(username, year);
TotalCommitContributions += contributions.totalCommitContributions;
RestrictedContributionsCount += contributions.restrictedContributionsCount;
TotalPullRequestReviewContributions += contributions.totalPullRequestReviewContributions;

Check warning on line 174 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L170-L174

Added lines #L170 - L174 were not covered by tests
}

const data = await axios({
method: "post",
url: "https://api.github.com/graphql",
Expand All @@ -106,11 +196,6 @@
following {
totalCount
}
contributionsCollection {
totalCommitContributions
restrictedContributionsCount
totalPullRequestReviewContributions
}
openedIssues: issues(states: OPEN) {
totalCount
}
Expand Down Expand Up @@ -143,5 +228,24 @@
if (data.data.errors?.length > 0)
throw new Error(data.data.errors[0].message);

return data.data.data.user;
const user = data.data.data.user;

Check warning on line 231 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L231

Added line #L231 was not covered by tests

return {

Check warning on line 233 in src/fetcher/apiFetch.ts

View check run for this annotation

Codecov / codecov/patch

src/fetcher/apiFetch.ts#L233

Added line #L233 was not covered by tests
name: user.name,
login: user.login,
avatarUrl: user.avatarUrl,
repositories: user.repositories,
followers: user.followers,
following: user.following,
openedIssues: user.openedIssues,
closedIssues: user.closedIssues,
pullRequests: user.pullRequests,
mergedPullRequests: user.mergedPullRequests,
discussionStarted: user.discussionStarted,
discussionAnswered: user.discussionAnswered,
repositoriesContributedTo: user.repositoriesContributedTo,
totalCommitContributions: TotalCommitContributions,
restrictedContributionsCount: RestrictedContributionsCount,
totalPullRequestReviewContributions: TotalPullRequestReviewContributions,
};
}
6 changes: 3 additions & 3 deletions src/getData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ async function getData(username: string): Promise<GetData> {
total_prs: millify(user.pullRequests.totalCount),
total_prs_merged: millify(user.mergedPullRequests.totalCount),
total_commits: millify(
user.contributionsCollection.restrictedContributionsCount +
user.contributionsCollection.totalCommitContributions
user.restrictedContributionsCount +
user.totalCommitContributions
),
total_review: millify(user.contributionsCollection.totalPullRequestReviewContributions),
total_review: millify(user.totalPullRequestReviewContributions),
total_discussion_answered: millify(user.discussionAnswered.totalCount),
total_discussion_started: millify(user.discussionStarted.totalCount),
total_contributed_to: millify(user.repositoriesContributedTo.totalCount),
Expand Down