-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
206 lines (175 loc) · 5.67 KB
/
index.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
const [, , owner, repo, githubToken] = process.argv;
if (!githubToken || !owner || !repo) {
console.error(
"Please provide a GitHub access token, owner, and repo as command-line arguments."
);
process.exit(1);
}
async function getIssueDetails(octokit, owner, repo, issueNumber) {
const { data: issue } = await octokit.rest.issues.get({
owner,
repo,
issue_number: issueNumber,
});
console.log(`📝 Issue #${issueNumber}: ${issue.title}`);
const sourceSandboxMatch = issue.body.match(
/### Pick a source sandbox to refresh from\n+(.*)$/m
);
const sourceSandbox = sourceSandboxMatch ? sourceSandboxMatch[1].trim() : "";
const daysToKeepMatch = issue.body.match(
/### How long should the sandbox be kept\?\n+(.*)$/m
);
const daysToKeep = daysToKeepMatch ? daysToKeepMatch[1].trim() : "15"; // Set default to "15"
const userEmailMatch = issue.body.match(
/### Email of the user to which this sandbox should be assigned\n+(.*)$/m
);
const userEmail = userEmailMatch ? userEmailMatch[1].trim() : "";
return { sourceSandbox, daysToKeep, userEmail };
}
async function deleteRepositoryVariable(octokit, repoOwner, repoName, name) {
await octokit.request('DELETE /repos/{owner}/{repo}/actions/variables/{name}', {
owner: repoOwner,
repo: repoName,
name: name,
headers: { 'X-GitHub-Api-Version': '2022-11-28' },
});
}
async function getRepoVariables(octokit, owner, repo) {
const variables = await octokit.paginate(
"GET /repos/{owner}/{repo}/actions/variables",
{
owner: owner,
repo: repo,
per_page: 30,
headers: { "X-GitHub-Api-Version": "2022-11-28" },
}
);
return variables;
}
async function updateIssueWithSpecialString(octokit, owner, repo, issueNumber, sourceSandbox, daysToKeep, userEmail) {
const { data: issue } = await octokit.rest.issues.get({
owner,
repo,
issue_number: issueNumber,
});
const specialString = `<!-- {"id":"request-dev-sandbox","sourceSB":"${sourceSandbox}","daysToKeep":"${daysToKeep}","email":"${userEmail}"} -->`;
const updatedBody = `${issue.body}\n\n${specialString}`;
await octokit.rest.issues.update({
owner,
repo,
issue_number: issueNumber,
body: updatedBody,
});
console.log(`✏️ Updated issue #${issueNumber} with special string`);
}
function createNewVariable(
owner,
repo,
oldVariable,
sourceSandbox,
daysToKeep,
userEmail
) {
const createdAt = oldVariable.createdAt;
const expiry = parseInt(oldVariable.expiry, 10);
const jobToBeExecutedAfter = Math.floor(
(createdAt + expiry * 24 * 60 * 60 * 1000 - Date.now()) / (60 * 1000)
);
const newVariable = {
status: "Awaiting",
payload: {
id: `request-dev-sandbox`,
sourceSB: sourceSandbox,
daysToKeep: daysToKeep,
email: userEmail,
issueNumber: oldVariable.issueNumber,
repoOwner: owner,
repoName: repo,
issueCreator: oldVariable.requester,
valid_issue: "true",
env: "devhub",
status: oldVariable.status,
sandboxName: oldVariable.name,
devHubAuthRequired: true,
jobId: "dev-sandbox-expiry",
username: `${userEmail}.${oldVariable.name}`,
jobToBeExecutedAfter: jobToBeExecutedAfter,
},
};
return newVariable;
}
async function upgradeVariables() {
const { Octokit } = await import("octokit");
const octokit = new Octokit({
auth: githubToken,
});
console.log(`🚀 Starting SFOPS Migration for ${owner}/${repo}`);
console.log(`---------------------------------------------------`);
const variables = await getRepoVariables(octokit, owner, repo);
for (const variable of variables) {
if (variable.name.endsWith("_DEVSBX")) {
console.log();
console.log(`⚙️ Upgrading variable: ${variable.name}`);
const oldVariable = JSON.parse(variable.value);
const { sourceSandbox, daysToKeep, userEmail } = await getIssueDetails(
octokit,
owner,
repo,
oldVariable.issueNumber
);
console.log(`✅ Fetched issue details for issue #${oldVariable.issueNumber}`);
// Update the issue with the special string
await updateIssueWithSpecialString(octokit, owner, repo, oldVariable.issueNumber, sourceSandbox, daysToKeep, userEmail);
const newVariable = createNewVariable(
owner,
repo,
oldVariable,
sourceSandbox,
daysToKeep,
userEmail
);
const upgradedVariableName = `CONTEXT_${oldVariable.issueNumber}`;
try {
await octokit.rest.actions.getRepoVariable({
owner: owner,
repo: repo,
name: upgradedVariableName,
});
console.log(
`⏭️ Variable ${upgradedVariableName} already exists. Skipping...`
);
} catch (error) {
if (error.status === 404) {
// Variable doesn't exist, create it
await octokit.rest.actions.createRepoVariable({
owner: owner,
repo: repo,
name: upgradedVariableName,
value: JSON.stringify(newVariable),
});
console.log(
`✨ Created new variable: ${upgradedVariableName}`
);
// Delete the old variable
await deleteRepositoryVariable(octokit, owner, repo, variable.name);
console.log(
`🗑️ Deleted old variable: ${variable.name}`
);
} else {
throw error;
}
}
}
}
console.log();
console.log(`🎉 SFOPS Migration completed successfully!`);
}
(async () => {
try {
await upgradeVariables();
} catch (error) {
console.error("An error occurred:", error);
process.exit(1);
}
})();