Repository navigation
Expand file tree
/
Copy pathmigration-source.ts
More file actions
215 lines (201 loc) · 6.54 KB
/
Copy pathmigration-source.ts
File metadata and controls
215 lines (201 loc) · 6.54 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
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
207
208
209
210
211
212
213
214
215
import type { GitHubGraphQL } from "$lib/server/core/github";
export async function getOrgId(gql: GitHubGraphQL, org: string): Promise<string> {
const result = await gql<{ organization: { id: string } }>(
`query($login: String!) { organization(login: $login) { id } }`,
{ login: org },
);
return result.organization.id;
}
export async function getOrgDatabaseId(gql: GitHubGraphQL, org: string): Promise<string> {
const result = await gql<{ organization: { databaseId: number } }>(
`query($login: String!) { organization(login: $login) { databaseId } }`,
{ login: org },
);
return String(result.organization.databaseId);
}
export async function createGitHubMigrationSource(
gql: GitHubGraphQL,
orgId: string,
): Promise<string> {
return createTypedMigrationSource(gql, orgId, {
name: "GHEC Source",
sourceUrl: "https://github.com",
type: "GITHUB_ARCHIVE",
});
}
export async function createBitbucketCloudMigrationSource(
gql: GitHubGraphQL,
orgId: string,
): Promise<string> {
return createTypedMigrationSource(gql, orgId, {
name: "Bitbucket Cloud Source",
sourceUrl: "https://bitbucket.org",
type: "GITHUB_ARCHIVE",
});
}
export async function createBitbucketMigrationSource(
gql: GitHubGraphQL,
orgId: string,
): Promise<string> {
return createTypedMigrationSource(gql, orgId, {
name: "Bitbucket Server Source",
sourceUrl: "https://not-used",
type: "BITBUCKET_SERVER",
});
}
export async function createGitLabMigrationSource(
gql: GitHubGraphQL,
orgId: string,
): Promise<string> {
return createTypedMigrationSource(gql, orgId, {
name: "GitLab Source",
sourceUrl: "https://not-used",
type: "GITLAB",
});
}
async function createTypedMigrationSource(
gql: GitHubGraphQL,
orgId: string,
source: { name: string; sourceUrl: string; type: string },
): Promise<string> {
const result = await gql<{ createMigrationSource: { migrationSource: { id: string } } }>(
`mutation($name: String!, $sourceUrl: String!, $ownerId: ID!, $type: MigrationSourceType!) {
createMigrationSource(input: { name: $name, url: $sourceUrl, ownerId: $ownerId, type: $type }) {
migrationSource { id }
}
}`,
{
// A lost response can leave a created source without a usable ID.
request: { retries: 0 },
name: source.name,
sourceUrl: source.sourceUrl,
ownerId: orgId,
type: source.type,
},
);
const id = result.createMigrationSource?.migrationSource?.id;
if (typeof id !== "string" || !id.trim() || id !== id.trim())
throw new Error("GHEC did not acknowledge the migration source ID");
return id;
}
export interface StartMigrationOptions {
migrationSourceId: string;
sourceRepoUrl: string;
orgId: string;
repoName: string;
sourceToken: string;
targetToken: string;
gitArchiveUrl: string;
metadataArchiveUrl: string;
skipReleases?: boolean;
targetRepoVisibility?: string;
lockSource?: boolean;
}
export async function startRepositoryMigration(
gql: GitHubGraphQL,
options: StartMigrationOptions,
): Promise<string> {
const mutation = `mutation startRepositoryMigration(
$sourceId: ID!, $ownerId: ID!, $sourceRepositoryUrl: URI!,
$repositoryName: String!, $continueOnError: Boolean!,
$gitArchiveUrl: String, $metadataArchiveUrl: String,
$accessToken: String!, $githubPat: String, $skipReleases: Boolean,
$targetRepoVisibility: String, $lockSource: Boolean
) {
startRepositoryMigration(input: {
sourceId: $sourceId, ownerId: $ownerId,
sourceRepositoryUrl: $sourceRepositoryUrl,
repositoryName: $repositoryName, continueOnError: $continueOnError,
gitArchiveUrl: $gitArchiveUrl, metadataArchiveUrl: $metadataArchiveUrl,
accessToken: $accessToken, githubPat: $githubPat,
skipReleases: $skipReleases, targetRepoVisibility: $targetRepoVisibility,
lockSource: $lockSource
}) { repositoryMigration { id } }
}`;
const result = await gql<{
startRepositoryMigration: { repositoryMigration: { id: string } };
}>(mutation, {
// A 5xx can arrive after acceptance; do not replay this mutation in the SDK.
request: { retries: 0 },
sourceId: options.migrationSourceId,
ownerId: options.orgId,
sourceRepositoryUrl: options.sourceRepoUrl,
repositoryName: options.repoName,
continueOnError: true,
gitArchiveUrl: options.gitArchiveUrl || null,
metadataArchiveUrl: options.metadataArchiveUrl || null,
accessToken: options.sourceToken,
githubPat: options.targetToken,
skipReleases: options.skipReleases ?? false,
targetRepoVisibility: options.targetRepoVisibility || null,
lockSource: options.lockSource ?? false,
});
const id = result.startRepositoryMigration?.repositoryMigration?.id;
if (typeof id !== "string" || !id.trim() || id !== id.trim()) {
throw new Error("GHEC did not return a valid migration ID");
}
return id;
}
export interface GitHubMigrationStatus {
state: string;
repositoryName: string;
warningsCount: number;
failureReason: string;
migrationLogUrl: string;
}
export async function getRepositoryMigration(
gql: GitHubGraphQL,
migrationId: string,
): Promise<GitHubMigrationStatus> {
const result = await gql<{ node: GitHubMigrationStatus }>(
`query($id: ID!) {
node(id: $id) {
... on Migration {
id, sourceUrl, migrationLogUrl, migrationSource { name },
state, warningsCount, failureReason, repositoryName
}
}
}`,
{ id: migrationId },
);
return result.node;
}
export async function abortRepositoryMigration(
gql: GitHubGraphQL,
migrationId: string,
): Promise<boolean> {
const result = await gql<{ abortRepositoryMigration: { success: boolean } }>(
`mutation($migrationId: ID!) {
abortRepositoryMigration(input: { migrationId: $migrationId }) { success }
}`,
{ migrationId },
);
return result.abortRepositoryMigration?.success === true;
}
export async function getRepositoryNodeId(
gql: GitHubGraphQL,
owner: string,
name: string,
): Promise<string> {
const result = await gql<{ repository: { id: string } }>(
`query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) { id }
}`,
{ owner, name },
);
return result.repository.id;
}
export async function archiveRepository(
gql: GitHubGraphQL,
repositoryId: string,
): Promise<boolean> {
const result = await gql<{ archiveRepository: { repository: { isArchived: boolean } } }>(
`mutation($repositoryId: ID!) {
archiveRepository(input: { repositoryId: $repositoryId }) {
repository { isArchived }
}
}`,
{ repositoryId },
);
return result.archiveRepository?.repository?.isArchived === true;
}