Skip to content

Commit

Permalink
chore: add debug for gitlab sop
Browse files Browse the repository at this point in the history
  • Loading branch information
ly525 committed Aug 21, 2022
1 parent 03c2dde commit 65749a3
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules/
public/
themes/
package-lock.json
sop/gitlab/ymm.js
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
"url": "https://github.com/ly525/blog/issues"
},
"homepage": "https://github.com/ly525/blog#readme",
"dependencies": {
"acyort-archives": "^1.0.4",
"acyort-paginator": "^0.1.3",
"acyort-rss": "^0.1.4"
}
"dependencies": {},
"devDependencies": {
"axios": "^0.27.2",
"fs-extra": "^10.1.0"
},
"type": "module"
}
68 changes: 45 additions & 23 deletions sop/gitlab/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@ import axios from "axios";
import fs from "fs-extra";
import path from "path";
import { execSync } from "child_process";
// import { fileURLToPath } from "url";

let log = console.log
let log = console.log;

/**
* clone 或者 pull gitlab 仓库
* @param {*} setting
* @param {*} args
* @returns
* @param {*} setting
* @param {*} args
* @returns
*/
export async function runCloneGitLab(setting, args) {
const { folder, uid, realRun } = args
log = setting.log || log
log = setting.log || log;

log(setting.welcomeTip(setting));
const { folder, uid, realRun } = args;

const repos = await fetchReposByUser(setting.reposByUidApi({ uid }));
if (!realRun) return printReposOverview(repos)
if (!realRun) return printReposOverview(setting, repos);

const pullSuccessRepos = repos.map((item) => item.name); // 拉取成功的repos
const pullFailedRepos = []; // 拉取失败的repos

cdReposFolder(setting)
for (const key in repos) {
if (!Object.prototype.hasOwnProperty.call(repos, key)) return;
const { name: repoName, git: repoUrl } = repos[key];

try {
const repoFolder = path.join(folder, repoName);
log(`[仓库名称]: ${repoName}, 即将 clone 到本地路径是:${repoFolder}`);
Expand All @@ -46,14 +50,12 @@ export async function runCloneGitLab(setting, args) {
}

// 打印成功的仓库名
pullSuccessRepos.length > 0 &&
log(`clone 成功仓库: ${pullSuccessRepos}`);
pullSuccessRepos.length > 0 && log(`clone 成功仓库: ${pullSuccessRepos}`);
// 打印失败的仓库名
pullFailedRepos.length > 0 &&
log(`clone 失败仓库: ${pullFailedRepos}, 请联系 TL 或 导师 开通相关权限`);
}


function runCloneOrPull(command, repoName) {
execSync(command, () => logError(repoName));
}
Expand Down Expand Up @@ -84,26 +86,24 @@ async function fetchReposByUser(apiUrl) {
* @param {*} repos
* @returns
*/
function getGroupsByRepos(repos) {
function getGroupsByRepos(setting, repos) {
const reg = new RegExp(`^git@${setting.gitLabHost}:(.*)\/[^/]*$`);

const groups = repos.map((item) => item.git.replace(reg, "$1"));
const uniqueGroups = [...new Set(groups)];
return uniqueGroups;
}


/**
* 打印概览信息
* @param {*} repos
* @param {*} repos
*/
function printReposOverview(repos) {
const groups = getGroupsByRepos(repos);
function printReposOverview(setting, repos) {
const groups = getGroupsByRepos(setting, repos);

log(`
即将 clone 仓库的基本信息如下:
${groups.length} 个Group,
${printGroupUrls(groups)}
${printGroupUrls(setting, groups)}
${repos.length} 个Repo
Expand All @@ -113,16 +113,38 @@ function printReposOverview(repos) {
`);
}


/**
* 打印 GitLab Group 清单
*
*
* @param groups
* @returns
*/
function printGroupUrls(groups) {
return groups
.map((group) => `https://${settings.gitLabHost}/${group}`)
function printGroupUrls(setting, groups) {
const groupsInfo = groups
.map((group, index) => {
const groupUrl = `https://${setting.gitLabHost}/${group}`;
const addGroupMemberUrl = `${groupUrl}/-/group_members`;
return `[${index}] ${group}, 访问 ${addGroupMemberUrl} 开通权限`;
})
.join("\r\n ");

return `
${groups.length} 个Group, 如果没有权限,请联系 TL或导师访问如下地址, 开通权限:
${groupsInfo}
`;
}


function cdReposFolder(setting) {
if (!setting.folder) return
// const __filename = fileURLToPath(import.meta.url);
// const __dirname = path.dirname(__filename);
// console.log('__dirname', __dirname)
// console.log('process.cwd()', process.cwd())
const reposFolder = path.join(process.cwd(), setting.folder);
if (!fs.existsSync(reposFolder)) {
fs.mkdirSync(reposFolder)
process.chdir(reposFolder)
}
}
82 changes: 82 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.npmmirror.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^0.27.2:
version "0.27.2"
resolved "https://registry.npmmirror.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
dependencies:
follow-redirects "^1.14.9"
form-data "^4.0.0"

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.npmmirror.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.npmmirror.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==

follow-redirects@^1.14.9:
version "1.15.1"
resolved "https://registry.npmmirror.com/follow-redirects/-/follow-redirects-1.15.1.tgz#0ca6a452306c9b276e4d3127483e29575e207ad5"
integrity sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.npmmirror.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

fs-extra@^10.1.0:
version "10.1.0"
resolved "https://registry.npmmirror.com/fs-extra/-/fs-extra-10.1.0.tgz#02873cfbc4084dde127eaa5f9905eef2325d1abf"
integrity sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"

graceful-fs@^4.1.6, graceful-fs@^4.2.0:
version "4.2.10"
resolved "https://registry.npmmirror.com/graceful-fs/-/graceful-fs-4.2.10.tgz#147d3a006da4ca3ce14728c7aefc287c367d7a6c"
integrity sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==

jsonfile@^6.0.1:
version "6.1.0"
resolved "https://registry.npmmirror.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae"
integrity sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==
dependencies:
universalify "^2.0.0"
optionalDependencies:
graceful-fs "^4.1.6"

[email protected]:
version "1.52.0"
resolved "https://registry.npmmirror.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==

mime-types@^2.1.12:
version "2.1.35"
resolved "https://registry.npmmirror.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
dependencies:
mime-db "1.52.0"

universalify@^2.0.0:
version "2.0.0"
resolved "https://registry.npmmirror.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"
integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==

0 comments on commit 65749a3

Please sign in to comment.