Skip to content

Commit 8591f52

Browse files
committed
Use browser cookie to download binary attachments from GitLab
1 parent 47ef965 commit 8591f52

4 files changed

Lines changed: 15 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ Go to [Settings / Access Tokens](https://gitlab.com/profile/personal_access_toke
5656

5757
Leave it null for the first run of the script. Then the script will show you which projects there are. Can be either string or number.
5858

59+
#### gitlab.sessionCookie
60+
61+
GitLab's API [does not allow downloading of attachments](https://gitlab.com/gitlab-org/gitlab/-/issues/24155) and only images can be downloaded using HTTP. To work around this limitation and enable binary attachments to be migrated one can use the session cookie set in the browser after logging in to the gitlab instance. The cookie is named `_gitlab_session`.
62+
5963
### github
6064

6165
#### github.baseUrl

sample_settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default {
55
// url: 'https://gitlab.mycompany.com',
66
token: '{{gitlab private token}}',
77
projectId: null,
8+
sessionCookie: null,
89
},
910
github: {
1011
// baseUrl: 'https://gitlab.mycompany.com:123/etc',

src/gitlabHelper.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class GitlabHelper {
1111
gitlabUrl?: string;
1212
gitlabToken: string;
1313
gitlabProjectId: number;
14+
sessionCookie: string;
1415

1516
host: string;
1617
projectPath?: string;
@@ -24,6 +25,7 @@ export default class GitlabHelper {
2425
this.gitlabToken = gitlabSettings.token;
2526
this.gitlabProjectId = gitlabSettings.projectId;
2627
this.host = gitlabSettings.url ? gitlabSettings.url : 'http://gitlab.com';
28+
this.sessionCookie = gitlabSettings.sessionCookie;
2729
}
2830

2931
/**
@@ -92,7 +94,13 @@ export default class GitlabHelper {
9294
try {
9395
const host = this.host.endsWith('/') ? this.host : this.host + '/';
9496
const attachmentUrl = host + this.projectPath + relurl;
95-
const data = (await axios.get(attachmentUrl, {responseType: 'arraybuffer'})).data;
97+
const data = (await axios.get(attachmentUrl, {
98+
responseType: 'arraybuffer',
99+
headers: {
100+
// HACK: work around GitLab's API lack of GET for attachments
101+
// See https://gitlab.com/gitlab-org/gitlab/-/issues/24155
102+
Cookie: `_gitlab_session=${this.sessionCookie}`
103+
}})).data;
96104
return Buffer.from(data, 'binary')
97105
} catch (err) {
98106
console.error(`Could not download attachment #${relurl}.`);

src/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface GitlabSettings {
4242
url?: string;
4343
token: string;
4444
projectId: number;
45+
sessionCookie: string;
4546
}
4647

4748
export interface S3Settings {

0 commit comments

Comments
 (0)