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

fix: Remove direct GitHub API calls from frontend #40

Merged
merged 4 commits into from
Dec 10, 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
32 changes: 6 additions & 26 deletions src/services/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,14 @@ describe('GitHub Service', () => {
activities: [{
id: 'issue-1-2',
type: 'issue',
status: 'success',
status: 'pr_open',
timestamp: '2023-11-28T00:01:00Z',
url: 'https://github.com/All-Hands-AI/OpenHands/issues/1#comment-2',
prUrl: 'https://api.github.com/repos/All-Hands-AI/OpenHands/pulls/2',
description: 'A potential fix has been generated and a draft PR #2 has been created. Please review the changes.'
}],
lastUpdated: '2023-11-28T00:01:00Z'
});
} else if (url === 'https://api.github.com/repos/All-Hands-AI/OpenHands/pulls/2') {
return createMockResponse({
state: 'open',
merged: false
});
}
throw new Error(`Unexpected URL: ${url}`);
});
Expand All @@ -78,7 +73,7 @@ describe('GitHub Service', () => {
activities: [{
id: 'issue-1-2',
type: 'issue',
status: 'failure',
status: 'no_pr',
timestamp: '2023-11-28T00:01:00Z',
url: 'https://github.com/All-Hands-AI/OpenHands/issues/1#comment-2',
description: 'The workflow to fix this issue encountered an error. Openhands failed to create any code changes.'
Expand Down Expand Up @@ -143,7 +138,7 @@ describe('GitHub Service', () => {
activities: [{
id: 'issue-1-2',
type: 'issue',
status: 'success',
status: 'no_pr',
timestamp: '2023-11-28T00:01:00Z',
url: 'https://github.com/All-Hands-AI/OpenHands/issues/1#comment-2',
description: 'Working on the issue...'
Expand Down Expand Up @@ -173,19 +168,14 @@ describe('GitHub Service', () => {
activities: [{
id: 'issue-1-2',
type: 'issue',
status: 'success',
status: 'pr_open',
timestamp: '2023-11-28T00:01:00Z',
url: 'https://github.com/All-Hands-AI/OpenHands/issues/1#comment-2',
prUrl: 'https://api.github.com/repos/All-Hands-AI/OpenHands/pulls/2',
description: 'Created PR #2'
}],
lastUpdated: '2023-11-28T00:01:00Z'
});
} else if (url === 'https://api.github.com/repos/All-Hands-AI/OpenHands/pulls/2') {
return createMockResponse({
state: 'open',
merged: false
});
}
throw new Error(`Unexpected URL: ${url}`);
});
Expand All @@ -209,19 +199,14 @@ describe('GitHub Service', () => {
activities: [{
id: 'issue-1-2',
type: 'issue',
status: 'success',
status: 'pr_merged',
timestamp: '2023-11-28T00:01:00Z',
url: 'https://github.com/All-Hands-AI/OpenHands/issues/1#comment-2',
prUrl: 'https://api.github.com/repos/All-Hands-AI/OpenHands/pulls/2',
description: 'Created PR #2'
}],
lastUpdated: '2023-11-28T00:01:00Z'
});
} else if (url === 'https://api.github.com/repos/All-Hands-AI/OpenHands/pulls/2') {
return createMockResponse({
state: 'closed',
merged: true
});
}
throw new Error(`Unexpected URL: ${url}`);
});
Expand All @@ -245,19 +230,14 @@ describe('GitHub Service', () => {
activities: [{
id: 'issue-1-2',
type: 'issue',
status: 'success',
status: 'pr_closed',
timestamp: '2023-11-28T00:01:00Z',
url: 'https://github.com/All-Hands-AI/OpenHands/issues/1#comment-2',
prUrl: 'https://api.github.com/repos/All-Hands-AI/OpenHands/pulls/2',
description: 'Created PR #2'
}],
lastUpdated: '2023-11-28T00:01:00Z'
});
} else if (url === 'https://api.github.com/repos/All-Hands-AI/OpenHands/pulls/2') {
return createMockResponse({
state: 'closed',
merged: false
});
}
throw new Error(`Unexpected URL: ${url}`);
});
Expand Down
37 changes: 4 additions & 33 deletions src/services/github.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
import { BotActivity, IssueActivityStatus } from '../types';
import { BotActivity } from '../types';

async function checkPRStatus(prUrl: string): Promise<IssueActivityStatus> {
try {
const response = await fetch(prUrl);
if (!response.ok) {
return 'no_pr';
}
const pr = await response.json();

if (pr.merged) {
return 'pr_merged';
} else if (pr.state === 'closed') {
return 'pr_closed';
} else {
return 'pr_open';
}
} catch (error) {
console.error('Error checking PR status:', error);
return 'no_pr';
}
}
// PR status is now included in the cached data, no need to fetch it

export async function fetchBotActivities(since?: string): Promise<BotActivity[]> {
try {
Expand All @@ -39,18 +20,8 @@ export async function fetchBotActivities(since?: string): Promise<BotActivity[]>
);
}

// Process issue activities to determine PR status
const processedActivities = await Promise.all(activities.map(async activity => {
if (activity.type === 'issue') {
if (activity.prUrl) {
const prStatus = await checkPRStatus(activity.prUrl);
return { ...activity, status: prStatus };
} else {
return { ...activity, status: 'no_pr' as IssueActivityStatus };
}
}
return activity;
}));
// PR status is already included in the cached data
const processedActivities = activities;

// Sort by timestamp in descending order
return processedActivities.sort((a, b) =>
Expand Down
Loading