Skip to content

Commit 47ffd8f

Browse files
committed
feat: sync profile bio & location
1 parent a369933 commit 47ffd8f

20 files changed

Lines changed: 190 additions & 170 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- AlterTable
2+
ALTER TABLE "GitProfile" ADD COLUMN "bio" TEXT,
3+
ADD COLUMN "location" TEXT;

apps/api/prisma/schema.prisma

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ model GitProfile {
113113
handle String
114114
name String
115115
avatar String?
116+
bio String?
117+
location String?
116118
workspaceMemberships WorkspaceMembership[]
117119
118120
user User? @relation(fields: [userId], references: [id])

apps/api/src/app/auth/services/auth.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ const getLoginState = (redirectTo: string = "/") => {
7878
export const createOrSyncProfile = async (
7979
githubUser: Pick<
8080
GithubUser,
81-
"node_id" | "login" | "name" | "email" | "avatar_url"
81+
"node_id" | "login" | "name" | "email" | "avatar_url" | "bio" | "location"
8282
>
8383
): Promise<
8484
GitProfile & {
8585
user: User;
8686
workspaceMemberships: WorkspaceMembership[];
8787
}
8888
> => {
89-
const { login, name, email, avatar_url, node_id } = githubUser;
89+
const { login, name, email, avatar_url, node_id, bio, location } = githubUser;
9090

9191
const handle = login;
9292

@@ -101,6 +101,8 @@ export const createOrSyncProfile = async (
101101
handle,
102102
name: name || handle,
103103
avatar: avatar_url,
104+
bio: bio || null,
105+
location: location || null,
104106
user: {
105107
upsert: {
106108
update: {
@@ -120,6 +122,8 @@ export const createOrSyncProfile = async (
120122
name: name || handle,
121123
gitUserId: node_id,
122124
handle,
125+
bio: bio || null,
126+
location: location || null,
123127
user: {
124128
create: {
125129
slug: createUserSlug(GitProvider.GITHUB, handle),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { GitProfile, Prisma } from "@prisma/client";
2+
import { getPrisma } from "../../../prisma";
3+
4+
export const upsertGitProfile = async (
5+
args: Prisma.GitProfileCreateInput
6+
): Promise<GitProfile> => {
7+
const { gitProvider, gitUserId, handle, name, avatar, bio, location } = args;
8+
9+
const data = {
10+
gitProvider,
11+
gitUserId,
12+
handle,
13+
name,
14+
avatar: avatar ?? null,
15+
bio: bio ?? null,
16+
location: location ?? null,
17+
};
18+
19+
return getPrisma().gitProfile.upsert({
20+
where: {
21+
gitProvider_gitUserId: { gitUserId, gitProvider },
22+
},
23+
create: data,
24+
update: data,
25+
});
26+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { GitProvider } from "@prisma/client";
2+
3+
export interface UpsertGitProfileArgs {
4+
gitProvider: GitProvider;
5+
gitUserId: string;
6+
handle: string;
7+
name: string;
8+
avatar?: string | null;
9+
bio?: string | null;
10+
location?: string | null;
11+
}

apps/api/src/app/github/services/github-code-review.service.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ import { ResourceNotFoundException } from "../../errors/exceptions/resource-not-
2424
import { SweetQueue, addJob } from "../../../bull-mq/queues";
2525
import { findWorkspaceByGitInstallationId } from "../../workspaces/services/workspace.service";
2626
import { parseISO } from "date-fns";
27-
28-
interface Author {
29-
id: string;
30-
login: string;
31-
name: string;
32-
avatarUrl: string;
33-
}
27+
import { upsertGitProfile } from "../../gitProfile/services/git-profile.service";
28+
import { GitHubUser } from "./github-user.types";
29+
import { gitHubUserToGitProfileData } from "./github-user.service";
3430

3531
interface ReviewData {
3632
gitProfileId: number;
@@ -46,7 +42,7 @@ type ReviewDataWithId = ReviewData & {
4642
interface ReviewRequestData {
4743
createdAt: Date;
4844
deletedAt: Date | null;
49-
author: Author;
45+
author: GitHubUser;
5046
}
5147

5248
export const syncCodeReviews = async (
@@ -131,13 +127,16 @@ const fetchPullRequestReviews = async (
131127
const reviewEvents: ReviewDataWithId[] = [];
132128
const gitProfiles = new Map<string, GitProfile>();
133129

134-
const getGitProfileId = async (author: Author) => {
135-
if (gitProfiles.has(author.id)) {
136-
return gitProfiles.get(author.id)!.id;
130+
const getGitProfileId = async (author: GitHubUser) => {
131+
if (gitProfiles.has(author.id.toString())) {
132+
return gitProfiles.get(author.id.toString())!.id;
137133
}
138134

139-
const gitProfile = await upsertGitProfile(author);
140-
gitProfiles.set(author.id, gitProfile);
135+
const gitProfile = await upsertGitProfile(
136+
gitHubUserToGitProfileData(author)
137+
);
138+
139+
gitProfiles.set(author.id.toString(), gitProfile);
141140

142141
return gitProfile.id;
143142
};
@@ -154,6 +153,8 @@ const fetchPullRequestReviews = async (
154153
login
155154
name
156155
avatarUrl
156+
bio
157+
location
157158
}
158159
}
159160
}
@@ -166,6 +167,8 @@ const fetchPullRequestReviews = async (
166167
login
167168
name
168169
avatarUrl
170+
bio
171+
location
169172
}
170173
}
171174
}
@@ -201,6 +204,8 @@ const fetchPullRequestReviews = async (
201204
login
202205
name
203206
avatarUrl
207+
bio
208+
location
204209
}
205210
}
206211
comments {
@@ -340,6 +345,8 @@ const getReviewRequests = (nodes: any[]): ReviewRequestData[] => {
340345
login: node.requestedReviewer.login,
341346
name: node.requestedReviewer.name,
342347
avatarUrl: node.requestedReviewer.avatarUrl,
348+
bio: node.requestedReviewer.bio,
349+
location: node.requestedReviewer.location,
343350
},
344351
});
345352
}
@@ -360,6 +367,8 @@ const getReviewRequests = (nodes: any[]): ReviewRequestData[] => {
360367
login: node.requestedReviewer.login,
361368
name: node.requestedReviewer.name,
362369
avatarUrl: node.requestedReviewer.avatarUrl,
370+
bio: node.requestedReviewer.bio,
371+
location: node.requestedReviewer.location,
363372
};
364373
}
365374
}
@@ -376,7 +385,9 @@ const upsertCodeReviewRequests = async (
376385
logger.debug("upsertCodeReviewRequests", { pullRequest, reviewRequests });
377386

378387
return parallel(10, reviewRequests, async (reviewRequest) => {
379-
const gitProfile = await upsertGitProfile(reviewRequest.author);
388+
const gitProfile = await upsertGitProfile(
389+
gitHubUserToGitProfileData(reviewRequest.author)
390+
);
380391

381392
const data = {
382393
workspaceId: pullRequest.workspaceId,
@@ -481,25 +492,6 @@ const updatePullRequestTracking = async (
481492
});
482493
};
483494

484-
const upsertGitProfile = async (author: Author) => {
485-
return getPrisma().gitProfile.upsert({
486-
where: {
487-
gitProvider_gitUserId: {
488-
gitProvider: GitProvider.GITHUB,
489-
gitUserId: author.id,
490-
},
491-
},
492-
update: {},
493-
create: {
494-
gitProvider: GitProvider.GITHUB,
495-
gitUserId: author.id,
496-
handle: author.login,
497-
name: author.name ? author.name : author.login,
498-
avatar: author.avatarUrl,
499-
},
500-
});
501-
};
502-
503495
const findPullRequest = async (
504496
workspaceId: number,
505497
gitPullRequestId: string

apps/api/src/app/github/services/github-installation.service.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { logger } from "../../../lib/logger";
1616
import { getInstallationOctoKit, octokit } from "../../../lib/octokit";
1717
import { addDays, endOfDay } from "date-fns";
1818
import { ResourceNotFoundException } from "../../errors/exceptions/resource-not-found.exception";
19+
import { upsertGitProfile } from "../../gitProfile/services/git-profile.service";
20+
import { gitHubUserToGitProfileData } from "./github-user.service";
1921

2022
export const syncGitHubInstallation = async (
2123
gitInstallation: GitHubInstallation,
@@ -26,7 +28,9 @@ export const syncGitHubInstallation = async (
2628
gitUser,
2729
});
2830

29-
const gitProfile = await upsertGitProfile(gitUser);
31+
const gitProfile = await upsertGitProfile(
32+
gitHubUserToGitProfileData({ avatarUrl: gitUser.avatar_url, ...gitUser })
33+
);
3034
const workspace = await upsertWorkspace(gitInstallation, gitProfile);
3135
const installation = await upsertInstallation(gitInstallation, workspace.id);
3236

@@ -155,28 +159,6 @@ const upsertWorkspace = async (
155159
});
156160
};
157161

158-
const upsertGitProfile = async (gitUser: GitHubUser): Promise<GitProfile> => {
159-
const gitUserId = gitUser.node_id.toString();
160-
161-
const data = {
162-
gitUserId,
163-
gitProvider: GitProvider.GITHUB,
164-
handle: gitUser.login,
165-
name: gitUser.name || gitUser.login,
166-
avatar: gitUser.avatar_url,
167-
};
168-
169-
const gitProfile = await getPrisma().gitProfile.upsert({
170-
where: {
171-
gitProvider_gitUserId: { gitUserId, gitProvider: GitProvider.GITHUB },
172-
},
173-
create: data,
174-
update: data,
175-
});
176-
177-
return gitProfile;
178-
};
179-
180162
const connectUserToWorkspace = async (
181163
gitProfile: GitProfile,
182164
workspace: Workspace

apps/api/src/app/github/services/github-member.service.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
import { GitProfile, GitProvider } from "@prisma/client";
1+
import { GitProfile } from "@prisma/client";
22
import {
33
GITHUB_MAX_PAGE_LIMIT,
44
getInstallationGraphQLOctoKit,
55
} from "../../../lib/octokit";
66
import { getPrisma } from "../../../prisma";
77
import { parallel } from "radash";
88
import { ResourceNotFoundException } from "../../errors/exceptions/resource-not-found.exception";
9+
import { upsertGitProfile } from "../../gitProfile/services/git-profile.service";
910
import { logger } from "../../../lib/logger";
1011
import { findWorkspaceByGitInstallationId } from "../../workspaces/services/workspace.service";
12+
import { gitHubUserToGitProfileData } from "./github-user.service";
13+
import { GitHubUser } from "./github-user.types";
1114

1215
type GitOrganizationMember = {
13-
id: string;
16+
id: number;
1417
login: string;
1518
role: string;
1619
name?: string;
1720
avatarUrl?: string;
21+
bio?: string;
22+
location?: string;
1823
};
1924

20-
type MemberData = Omit<
21-
GitProfile,
22-
"id" | "userId" | "createdAt" | "updatedAt"
23-
> & {
25+
type MemberData = GitHubUser & {
2426
role: string;
2527
};
2628

@@ -41,11 +43,7 @@ export const syncOrganizationMembers = async (
4143
);
4244

4345
const membersData: MemberData[] = gitHubMembers.map((member) => ({
44-
gitProvider: GitProvider.GITHUB,
45-
gitUserId: member.id,
46-
handle: member.login,
47-
name: member.name || member.login,
48-
avatar: member.avatarUrl || null,
46+
...member,
4947
role: member.role,
5048
}));
5149

@@ -101,6 +99,8 @@ const fetchGitHubOrganizationMembers = async (
10199
login
102100
name
103101
avatarUrl
102+
bio
103+
location
104104
}
105105
edges {
106106
role
@@ -150,16 +150,9 @@ const upsertGitProfiles = async (
150150
return parallel(10, membersData, async (memberData) => {
151151
const { role, ...member } = memberData;
152152

153-
const gitProfile = await getPrisma().gitProfile.upsert({
154-
where: {
155-
gitProvider_gitUserId: {
156-
gitProvider: GitProvider.GITHUB,
157-
gitUserId: member.gitUserId,
158-
},
159-
},
160-
create: member,
161-
update: member,
162-
});
153+
const gitProfile = await upsertGitProfile(
154+
gitHubUserToGitProfileData(member)
155+
);
163156

164157
await getPrisma(workspaceId).workspaceMembership.upsert({
165158
where: {

0 commit comments

Comments
 (0)