-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
128 lines (118 loc) · 3.04 KB
/
lib.js
File metadata and controls
128 lines (118 loc) · 3.04 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
require("dotenv").config();
const fs = require("fs");
const axiosBase = require("axios");
// Prepare axios for GitLab API
const gitlab = axiosBase.create({
baseURL: process.env.GITLAB_API_URL,
headers: {
"Content-Type": "application/json",
"PRIVATE-TOKEN": process.env.GITLAB_TOKEN,
},
responseType: "json",
});
// Prepare axios for GitHub API
const github = axiosBase.create({
baseURL: process.env.GITHUB_API_URL,
headers: {
"Content-Type": "application/json",
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
responseType: "json",
});
/**
* Loads the previously-saved information of the GitHub repos.
* @param {String} filename File name
*/
function loadReposInfo(filename) {
try {
return JSON.parse(fs.readFileSync(filename, "utf8"));
} catch (error) {
return []; // fallback
}
}
/**
* Saves the current information of the GitHub repos.
* @param {String} filename File name
* @param {Object} repos Repos information
*/
function saveReposInfo(filename, repos) {
fs.writeFileSync(filename, JSON.stringify(repos, null, " "));
}
async function getGithubReposPage(url) {
const result = await github.get(url);
let next = null;
if (result.headers && result.headers.link) {
// extract next url from "link" header
const matches = /\<([^<>]+)\>; rel\="next"/.exec(result.headers.link);
if (matches) {
next = matches[1];
}
}
const data =
(result.data &&
result.data.length > 0 &&
result.data.map(({ id, name, updated_at, pushed_at }) => ({
id,
name,
updated_at,
pushed_at,
}))) ||
null;
return {
next,
data,
};
}
/**
* Gets a list of repos in GitHub.
* @param {String} ownerType Resource type of owner (orgs|users)
* @param {String} owner Owner name
*/
async function getGithubRepos(ownerType, owner) {
let url = `${ownerType}/${owner}/repos?sort=full_name`;
const array = [];
while (url) {
const { next, data } = await getGithubReposPage(url);
if (data) array.push(data);
url = next;
}
return array.flat();
}
/**
* Gets a project in GitLab.
* @param {String} projectName Name of target project
*/
async function getGitlabProject(projectName) {
const result = await gitlab.get("projects", {
params: { search: projectName },
});
return result.data.find((x) => x.name === projectName);
}
/**
* Deletes the project from GitLab.
* @param {Number} projectId Id of target project
*/
async function deleteGitlabProject(projectId) {
await gitlab.delete(`projects/${projectId}`);
}
/**
* Starts to import GitHub repo to GitLab.
* @param {Number} repoId ID of the GitHub repo
* @param {String} targetNamespace Namespace in GitLab
*/
async function importFromGithub(repoId, targetNamespace) {
const data = {
personal_access_token: process.env.GITHUB_TOKEN,
repo_id: repoId,
target_namespace: targetNamespace,
};
await gitlab.post("import/github", data);
}
module.exports = {
loadReposInfo,
saveReposInfo,
getGithubRepos,
getGitlabProject,
deleteGitlabProject,
importFromGithub,
};