Skip to content

Commit 8d39553

Browse files
committed
Fixes DCO signoff issue
Auth session keeps information from the user profile, so if the email address is not made visible on the profile or alias is set as an email address, UI was using that email address for signoff. In some cases, this was failing because that email address is not users primary email address. For DCO to pass, user needs to signoff the commits with it's primary email address set in his account. Signed-off-by: Anil Vishnoi <[email protected]>
1 parent d502279 commit 8d39553

File tree

4 files changed

+64
-28
lines changed

4 files changed

+64
-28
lines changed

src/components/Contribute/Knowledge/index.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import './knowledge.css';
55
import { Alert, AlertActionCloseButton } from '@patternfly/react-core/dist/dynamic/components/Alert';
66
import { ActionGroup } from '@patternfly/react-core/dist/dynamic/components/Form';
77
import { Form } from '@patternfly/react-core/dist/dynamic/components/Form';
8-
import { getGitHubUsername } from '../../../utils/github';
8+
import { getGitHubUserInfo } from '../../../utils/github';
99
import { useSession } from 'next-auth/react';
1010
import AuthorInformation from '../AuthorInformation';
1111
import { FormType } from '../AuthorInformation';
@@ -178,15 +178,8 @@ export const KnowledgeForm: React.FunctionComponent<KnowledgeFormProps> = ({ kno
178178
getEnvVariables();
179179
}, []);
180180

181-
useEffect(() => {
182-
if (session?.user?.name && session?.user?.email) {
183-
setName(session?.user?.name);
184-
setEmail(session?.user?.email);
185-
}
186-
}, [session?.user]);
187-
188181
useMemo(() => {
189-
const fetchUsername = async () => {
182+
const fetchUserInfo = async () => {
190183
if (session?.accessToken) {
191184
try {
192185
const headers = {
@@ -196,15 +189,17 @@ export const KnowledgeForm: React.FunctionComponent<KnowledgeFormProps> = ({ kno
196189
'X-GitHub-Api-Version': '2022-11-28'
197190
};
198191

199-
const fetchedUsername = await getGitHubUsername(headers);
200-
setGithubUsername(fetchedUsername);
192+
const fetchedUsername = await getGitHubUserInfo(headers);
193+
setGithubUsername(fetchedUsername.login);
194+
setName(fetchedUsername.name);
195+
setEmail(fetchedUsername.email);
201196
} catch (error) {
202-
console.error('Failed to fetch GitHub username:', error);
197+
console.error('Failed to fetch GitHub user info:', error);
203198
}
204199
}
205200
};
206201

207-
fetchUsername();
202+
fetchUserInfo();
208203
}, [session?.accessToken]);
209204

210205
useEffect(() => {

src/components/Contribute/Knowledge/knowledge.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
border-color: #45a049;
2424
}
2525

26-
.spinner-container{
26+
.spinner-container {
2727
display: flex;
2828
justify-content: center;
2929
align-items: center;

src/components/Contribute/Skill/index.tsx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import './skills.css';
55
import { Alert, AlertActionCloseButton } from '@patternfly/react-core/dist/dynamic/components/Alert';
66
import { ActionGroup } from '@patternfly/react-core/dist/dynamic/components/Form';
77
import { Form } from '@patternfly/react-core/dist/dynamic/components/Form';
8-
import { getGitHubUsername } from '../../../utils/github';
8+
import { getGitHubUserInfo } from '../../../utils/github';
99
import { useSession } from 'next-auth/react';
1010
import AuthorInformation from '../AuthorInformation';
1111
import { FormType } from '../AuthorInformation';
@@ -139,32 +139,28 @@ export const SkillForm: React.FunctionComponent<SkillFormProps> = ({ skillEditFo
139139
getEnvVariables();
140140
}, []);
141141

142-
useEffect(() => {
143-
if (session?.user?.name && session?.user?.email) {
144-
setName(session?.user?.name);
145-
setEmail(session?.user?.email);
146-
}
147-
}, [session?.user]);
148-
149142
useMemo(() => {
150-
const fetchUsername = async () => {
143+
const fetchUserInfo = async () => {
151144
if (session?.accessToken) {
152145
try {
153-
const header = {
146+
const headers = {
154147
'Content-Type': 'application/json',
155148
Authorization: `Bearer ${session.accessToken}`,
156149
Accept: 'application/vnd.github+json',
157150
'X-GitHub-Api-Version': '2022-11-28'
158151
};
159-
const fetchedUsername = await getGitHubUsername(header);
160-
setGithubUsername(fetchedUsername);
152+
153+
const fetchedUsername = await getGitHubUserInfo(headers);
154+
setGithubUsername(fetchedUsername.login);
155+
setName(fetchedUsername.name);
156+
setEmail(fetchedUsername.email);
161157
} catch (error) {
162-
console.error('Failed to fetch GitHub username:', error);
158+
console.error('Failed to fetch GitHub user info:', error);
163159
}
164160
}
165161
};
166162

167-
fetchUsername();
163+
fetchUserInfo();
168164
}, [session?.accessToken]);
169165

170166
useEffect(() => {

src/utils/github.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import axios from 'axios';
33
import { PullRequestUpdateData } from '@/types';
44
import { BASE_BRANCH, FORK_CLONE_CHECK_RETRY_COUNT, FORK_CLONE_CHECK_RETRY_TIMEOUT, GITHUB_API_URL } from '@/types/const';
55

6+
type GithubUserInfo = {
7+
login: string;
8+
name: string;
9+
email: string;
10+
};
11+
612
export async function fetchPullRequests(token: string) {
713
try {
814
console.log('Refreshing PR Listing');
@@ -298,6 +304,29 @@ export const amendCommit = async (
298304
}
299305
};
300306

307+
export async function getGitHubUserInfo(headers: HeadersInit): Promise<GithubUserInfo> {
308+
const response = await fetch(`${GITHUB_API_URL}/user`, {
309+
headers
310+
});
311+
312+
if (!response.ok) {
313+
const errorText = await response.text();
314+
console.error('Failed to fetch GitHub user info:', response.status, errorText);
315+
throw new Error('Failed to fetch GitHub user info');
316+
}
317+
318+
const data = await response.json();
319+
320+
const userInfo: GithubUserInfo = {
321+
name: data.name,
322+
login: data.login,
323+
email: ''
324+
};
325+
326+
userInfo.email = await getGitHubUserPrimaryEmail(headers);
327+
return userInfo;
328+
}
329+
301330
export async function getGitHubUsername(headers: HeadersInit): Promise<string> {
302331
const response = await fetch(`${GITHUB_API_URL}/user`, {
303332
headers
@@ -313,6 +342,22 @@ export async function getGitHubUsername(headers: HeadersInit): Promise<string> {
313342
return data.login;
314343
}
315344

345+
export async function getGitHubUserPrimaryEmail(headers: HeadersInit): Promise<string> {
346+
const response = await fetch(`${GITHUB_API_URL}/user/public_emails`, {
347+
headers
348+
});
349+
350+
if (!response.ok) {
351+
const errorText = await response.text();
352+
console.error('Failed to fetch GitHub email address:', response.status, errorText);
353+
throw new Error('Failed to fetch GitHub email address');
354+
}
355+
356+
const data = await response.json();
357+
const emailInfo = data.find((emailObj: { primary: boolean }) => emailObj.primary === true);
358+
return emailInfo.email;
359+
}
360+
316361
export async function createFork(headers: HeadersInit, upstreamRepoOwner: string, upstreamRepoName: string, username: string) {
317362
const response = await fetch(`${GITHUB_API_URL}/repos/${upstreamRepoOwner}/${upstreamRepoName}/forks`, {
318363
method: 'POST',

0 commit comments

Comments
 (0)