1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// This approach fetches GitHub activity for two users specified in the URL,
// sends it to OpenAI for analysis, and returns collaboration suggestions.
// It uses the GitHub API (which doesn't require authentication for public data)
// and the OpenAI API (which does require an API key).
// Tradeoff: We're using an inline API key for simplicity, which isn't ideal for security.
// Note: This might hit rate limits for the GitHub API due to fetching a year of data.
import { OpenAI } from "https://esm.town/v/std/openai";
const OPENAI_API_KEY = "your_openai_api_key"; // Replace with your actual OpenAI API key
export default async function main(req: Request): Promise<Response> {
const url = new URL(req.url);
const user1 = url.searchParams.get('user1') || 'ejfox';
const user2 = url.searchParams.get('user2') || 'octocat';
async function fetchUserActivity(username: string) {
const oneYearAgo = new Date(Date.now() - 365 * 24 * 60 * 60 * 1000).toISOString();
const response = await fetch(`https://api.github.com/users/${username}/events?per_page=100&since=${oneYearAgo}`);
const data = await response.json();
if (!Array.isArray(data)) {
throw new Error(`Unexpected GitHub API response for user ${username}`);
}
return data;
}
try {
const [user1Data, user2Data] = await Promise.all([
fetchUserActivity(user1),
fetchUserActivity(user2)
]);
function summarizeActivity(data: any[]) {
const repoSet = new Set();
const languageSet = new Set();
data.forEach((event: any) => {
repoSet.add(event.repo.name);
if (event.payload.commits) {
event.payload.commits.forEach((commit: any) => {
if (commit.url) {
// This is a simplification. Ideally, we'd fetch each commit to get language info
languageSet.add("Unknown");
}
});
}
});
return {
repos: Array.from(repoSet),
languages: Array.from(languageSet)
};
}
const user1Summary = summarizeActivity(user1Data);
const user2Summary = summarizeActivity(user2Data);
const openai = new OpenAI(OPENAI_API_KEY);
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are a helpful assistant that suggests collaboration opportunities based on GitHub activity." },
{ role: "user", content: `Analyze the GitHub activity of two users and suggest collaboration opportunities:
User 1 (${user1}):
Repositories: ${user1Summary.repos.join(", ")}
Languages: ${user1Summary.languages.join(", ")}
User 2 (${user2}):
Repositories: ${user2Summary.repos.join(", ")}
Languages: ${user2Summary.languages.join(", ")}
Based on this information, suggest:
1. Potential collaborative projects they could work on together
2. Technologies they could explore or learn from each other
3. Ways they could complement each other's skills
Provide a detailed analysis and specific suggestions.` }
],
max_tokens: 500
});
const suggestions = completion.choices[0].message.content;
return new Response(suggestions, {
headers: { "Content-Type": "text/plain" }
});
} catch (error) {
return new Response(`Error: ${error.message}`, { status: 500 });
}
}