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: Add GitHub profile fetching option #123

Merged
merged 1 commit into from
Jun 19, 2024
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: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ PROFILES_DIRECTORY=
REDIS_URL=redis://localhost:6379

LINKEDIN_PROFILE_FETCHER_API_KEY=
LINKEDIN_PROFILE_FETCHER_HOST=\
LINKEDIN_PROFILE_FETCHER_HOST=

GITHUB_PROFILE_FETCHER_API_KEY=

SESSION_SECRET=
31 changes: 31 additions & 0 deletions src/user/profile-fetcher/github.profile-fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { InternalServerErrorException } from '@nestjs/common';
import { ProfileDetails, ProfileFetcher } from './base.profile-fetcher';
import { SocialAccountType } from '@prisma/client';

export class GitHubProfileFetcher extends ProfileFetcher {
async getProfileDetails(): Promise<ProfileDetails> {
const apiKey = process.env.GITHUB_PROFILE_FETCHER_API_KEY;

const username = this.profileUrl.split('https://github.com/')[1];

const response = await fetch(`https://api.github.com/users/${username}`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
});

if (response.ok) {
const data = await response.json();
return {
name: data.name,
socialAccountType: SocialAccountType.GITHUB,
profileUrl: data.html_url,
headline: data.bio,
socialId: data.login,
profilePictureUrl: data.avatar_url,
};
} else {
throw new InternalServerErrorException('Failed to fetch GitHub profile');
}
}
}
3 changes: 3 additions & 0 deletions src/user/profile-fetcher/profile-fetcher.factory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { BadRequestException } from '@nestjs/common';
import { LinkedInProfileFetcher } from './linkedin.profile-fetcher';
import { GitHubProfileFetcher } from './github.profile-fetcher';

export class ProfileFetcherFactory {
generateProfileFetcher(profileUrl: string) {
if (profileUrl.startsWith('https://www.linkedin.com')) {
return new LinkedInProfileFetcher(profileUrl);
} else if (profileUrl.startsWith('https://github.com')) {
return new GitHubProfileFetcher(profileUrl);
}
throw new BadRequestException('Unsupported profile URL');
}
Expand Down
Loading