This repository was archived by the owner on Mar 24, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub.ts
More file actions
132 lines (118 loc) · 4.13 KB
/
github.ts
File metadata and controls
132 lines (118 loc) · 4.13 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type { Role, FormData, Question } from '../types';
// Create a function for base64 encoding that is Cloudflare-compatible
function base64Encode(str: string): string {
// Use Cloudflare-compatible approach
const encoder = new TextEncoder();
const data = encoder.encode(str);
return btoa(
Array.from(data)
.map((byte) => String.fromCharCode(byte))
.join('')
);
}
/**
* Stores application data in GitHub repository
*/
export async function storeApplicationDataOnGitHub(
roleData: Role,
formData: FormData,
timestamp: string,
// Add parameters for configuration
githubToken: string,
repoOwner: string,
repoName: string
): Promise<boolean> {
try {
// Use passed-in parameters instead of process.env
// const githubToken = process.env.GITHUB_TOKEN;
if (!githubToken) {
console.log('GitHub token not provided, skipping GitHub storage');
return false;
}
console.log('Attempting to store application data on GitHub...');
// Use passed-in parameters
// const repoOwner =
// process.env.NEXT_PUBLIC_GITHUB_REPO_OWNER || 'allthingslinux';
// const repoName = process.env.NEXT_PUBLIC_GITHUB_REPO_NAME || 'applications';
if (!repoOwner || !repoName) {
console.error(
'GitHub repo owner or name not provided, skipping GitHub storage.'
);
return false;
}
// Create application data object
const applicationData = {
timestamp,
role: {
name: roleData.name,
slug: roleData.slug,
department: roleData.department,
description: roleData.description,
},
applicant: {
discord_username: formData.discord_username,
discord_id: formData.discord_id,
},
// Include all form data organized by questions
generalAnswers: roleData.generalQuestions
.filter((q: Question) => formData[q.name])
.map((q: Question) => ({
question: q.question,
answer: formData[q.name],
name: q.name,
optional: q.optional || false,
})),
roleAnswers: roleData.questions
.filter((q: Question) => formData[q.name])
.map((q: Question) => ({
question: q.question,
answer: formData[q.name],
name: q.name,
optional: q.optional || false,
})),
// Include simplified form data for complete backup
rawFormData: Object.fromEntries(
Object.entries(formData).map(([key, value]) => [key, String(value)])
),
};
// Format timestamp for filename
const safeTimestamp = timestamp.replace(/[:.]/g, '-');
const safeUsername = formData.discord_username.replace(/[^a-z0-9]/gi, '_');
const filename = `applications/${roleData.slug}/${safeUsername}-${safeTimestamp}.json`;
const content = JSON.stringify(applicationData, null, 2);
// Use the separate function for encoding
const contentEncoded = base64Encode(content);
console.log(`Attempting to create file: ${filename}`);
// Create the file via GitHub API - using the safe variables
const response = await fetch(
`https://api.github.com/repos/${repoOwner}/${repoName}/contents/${filename}`,
{
method: 'PUT',
headers: {
Authorization: `token ${githubToken}`,
'Content-Type': 'application/json',
Accept: 'application/vnd.github.v3+json',
'User-Agent': 'Cloudflare-Worker',
},
body: JSON.stringify({
message: `Application submission: ${roleData.name} - ${formData.discord_username}`,
content: contentEncoded,
branch: 'main',
}),
}
);
console.log(`GitHub API response status: ${response.status}`);
if (!response.ok) {
const responseData = await response.json();
console.error('GitHub API error details:', JSON.stringify(responseData));
throw new Error(
`GitHub API error: ${response.status} - ${JSON.stringify(responseData)}`
);
}
console.log(`Successfully stored application in GitHub at ${filename}`);
return true;
} catch (error) {
console.error('Error storing application data on GitHub:', error);
return false;
}
}