-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.js
172 lines (143 loc) · 5.66 KB
/
shared.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
const setHeaders = (res) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers','Accept,Content-Type,Content-Length,Accept-Encoding,X-CSRF-Token,Authorization');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
}
const fetchSecrets = async (local) => {
const secrets = {};
if (local) {
secrets.client_id = process.env.GITHUB_CLIENT_ID_DEV;
secrets.client_secret = process.env.GITHUB_CLIENT_SECRET_DEV;
}
else{
secrets.client_id = process.env.GITHUB_CLIENT_ID;
secrets.client_secret = process.env.GITHUB_CLIENT_SECRET;
}
const client = new SecretManagerServiceClient();
let fetchedSecrets = {};
for (const [key, value] of Object.entries(secrets)) {
const [version] = await client.accessSecretVersion({ name: value });
fetchedSecrets[key] = version.payload.data.toString();
}
return fetchedSecrets;
}
const updateIndexFile = async (octokit, owner, repo, filePath, content) => {
try {
let directoryPath = '';
if (filePath.includes('/')) {
directoryPath = filePath.substring(0, filePath.lastIndexOf('/'));
}
const indexPath = directoryPath ? `${directoryPath}/index.json` : 'index.json';
let indexContent = {};
let indexSha = null;
// Step 1: Fetch the existing index.json (if it exists)
try {
const indexResponse = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner,
repo,
path: indexPath,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
const indexData = Buffer.from(indexResponse.data.content, 'base64').toString('utf-8');
indexContent = JSON.parse(indexData);
indexSha = indexResponse.data.sha;
} catch (error) {
// If index.json doesn't exist, we'll create a new one
if (error.status !== 404) {
throw error;
}
}
// Ensure indexContent is an object
if (!indexContent || typeof indexContent !== 'object') {
indexContent = {};
}
// Step 2: Read the "key" value from the file content
const fileData = Buffer.from(content, 'base64').toString('utf-8');
const fileJson = JSON.parse(fileData);
const keyValue = fileJson.key || '';
// Step 3: Update the indexContent with the new/updated entry
const fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
indexContent[fileName] = keyValue;
// Step 4: Commit the updated index.json
const updatedIndexContent = Buffer.from(JSON.stringify(indexContent, null, 2)).toString('base64');
const commitMessage = `Update index.json for ${filePath}`;
// Prepare the request parameters
const params = {
owner,
repo,
path: indexPath,
message: commitMessage,
content: updatedIndexContent,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
};
// Include 'sha' only if indexSha is defined (not null)
if (indexSha) {
params.sha = indexSha;
}
console.log(indexSha);
console.log(params);
// Make the API call
const response = await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', params);
console.log(response);
} catch (error) {
console.error('Error updating index.json:', error);
throw error;
}
}
const removeFromIndexFile = async (octokit, owner, repo, filePath) => {
try {
let directoryPath = '';
if (filePath.includes('/')) {
directoryPath = filePath.substring(0, filePath.lastIndexOf('/'));
}
const indexPath = directoryPath ? `${directoryPath}/index.json` : 'index.json';
let indexContent = {};
let indexSha = null;
// Step 1: Fetch the existing index.json
const indexResponse = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner,
repo,
path: indexPath,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
const indexData = Buffer.from(indexResponse.data.content, 'base64').toString('utf-8');
indexContent = JSON.parse(indexData);
indexSha = indexResponse.data.sha;
// Ensure indexContent is an object
if (!indexContent || typeof indexContent !== 'object') {
indexContent = {};
}
// Step 2: Remove the file entry from indexContent
delete indexContent[filePath];
// Step 3: Commit the updated index.json
const updatedIndexContent = Buffer.from(JSON.stringify(indexContent, null, 2)).toString('base64');
const commitMessage = `Update index.json after deleting ${filePath}`;
await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', {
owner,
repo,
path: indexPath,
message: commitMessage,
content: updatedIndexContent,
sha: indexSha,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
} catch (error) {
console.error('Error updating index.json:', error);
throw error;
}
}
module.exports = {
setHeaders,
fetchSecrets,
updateIndexFile,
removeFromIndexFile
}