forked from gaurav5430/node-gitlab-2-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlabHelper.ts
More file actions
127 lines (114 loc) · 3.28 KB
/
gitlabHelper.ts
File metadata and controls
127 lines (114 loc) · 3.28 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
import { Gitlab } from '@gitbeaker/node';
import { GitlabSettings } from './settings';
import axios from 'axios';
export default class GitlabHelper {
// Wait for this issue to be resolved
// https://github.com/jdalrymple/gitbeaker/issues/793
gitlabApi: InstanceType<typeof Gitlab>;
gitlabUrl?: string;
gitlabToken: string;
gitlabProjectId: number;
host: string;
projectPath?: string;
constructor(
gitlabApi: InstanceType<typeof Gitlab>,
gitlabSettings: GitlabSettings
) {
this.gitlabApi = gitlabApi;
this.gitlabUrl = gitlabSettings.url;
this.gitlabToken = gitlabSettings.token;
this.gitlabProjectId = gitlabSettings.projectId;
this.host = gitlabSettings.url ? gitlabSettings.url : 'http://gitlab.com';
}
/**
* List all projects that the GitLab user is associated with.
*/
async listProjects() {
try {
const projects = await this.gitlabApi.Projects.all({ membership: true });
// print each project with info
for (let i = 0; i < projects.length; i++) {
console.log(
projects[i].id.toString(),
'\t',
projects[i].name,
'\t--\t',
projects[i]['description']
);
}
// instructions for user
console.log('\n\n');
console.log(
'Select which project ID should be transported to github. Edit the settings.js accordingly. (gitlab.projectID)'
);
console.log('\n\n');
} catch (err) {
console.error('An Error occured while fetching all GitLab projects:');
console.error(err);
}
}
/**
* Stores project path in a field
*/
async registerProjectPath(project_d: number) {
try {
const project = await this.gitlabApi.Projects.show(project_d);
this.projectPath = project['path_with_namespace'];
} catch (err) {
console.error('An Error occured while fetching all GitLab projects:');
console.error(err);
}
}
/**
* Gets all notes for a given issue.
*/
async getIssueNotes(issueIid: number) {
try {
return (await this.gitlabApi.IssueNotes.all(
this.gitlabProjectId,
issueIid,
{}
)) as any[];
} catch (err) {
console.error(`Could not fetch notes for GitLab issue #${issueIid}.`);
return [];
}
}
/**
* Gets attachment using http get
*/
async getAttachment(relurl: string) {
try {
const host = this.host.endsWith('/') ? this.host : this.host + '/';
const attachmentUrl = host + this.projectPath + relurl;
const data = (await axios.get(attachmentUrl, {responseType: 'arraybuffer'})).data;
return Buffer.from(data, 'binary')
} catch (err) {
console.error(`Could not download attachment #${relurl}.`);
return null;
}
}
/**
* Gets all branches.
*/
async getAllBranches() {
return (await this.gitlabApi.Branches.all(this.gitlabProjectId)) as any[];
}
/**
* Gets all notes for a given merge request.
*/
async getAllMergeRequestNotes(pullRequestIid: number) {
try {
return (this.gitlabApi.MergeRequestNotes.all(
this.gitlabProjectId,
pullRequestIid,
{}
) as any) as any[];
} catch (err) {
console.error(
`Could not fetch notes for GitLab merge request #${pullRequestIid}.`
);
return [];
}
}
}