diff --git a/.env.example b/.env.example index 7dd228e40112dd302c19f2ceee24e6b79e766578..fa9583cc4accd54676a0a3875513801ddf11dee4 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,3 @@ GITLAB_TOKEN= GITLAB_HOST= -TIMEOUT= \ No newline at end of file +GROUP_NAME=gitlab-group-name diff --git a/README.md b/README.md index 0e4a7ce05bfc7e2660a2e7073c3fce3b08486e97..cb73c7d43363d9d59d834fdfdf3c1240709d206a 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,21 @@ # РЎРєСЂРёРїС‚ для создания репозиториев gitlab для студентов -## How to run -1. `git clone https://git.miem.hse.ru/biv23x-ps/create-students-repos.git` -1. `cp .env.example .env` -1. fill .env -1. `npm i` -1. `mkdir logs` -1. `sh run.sh ` \ No newline at end of file +## Как запустить +1. Создать РіСЂСѓРїРїСѓ РІ Gitlab ручками +1. Получить Personal Access Token РІ Gitlab. Лучше выдать РІСЃРµ scoups +Р’РѕС‚ [ссылка](https://git.miem.hse.ru/-/profile/personal_access_tokens) для РњРРРњРѕРІСЃРєРѕРіРѕ Gitlab-Р° +1. Заполнить `.env ` файл +``` +GITLAB_TOKEN=<ваш сгенерированный Gitlab token> +GITLAB_HOST=<ссылка РЅР° ваш Gitlab> +GROUP_NAME=<РёРјСЏ созданной вами РіСЂСѓРїРїС‹> +TIMEOUT=<раз РІРѕ сколько милисекунд СЃРєСЂРёРїС‚ будет искать новосозданных студентов> +``` + +Пример заполнения .env для РњРРРњРѕРІСЃРєРѕРіРѕ Gitlab +``` +GITLAB_TOKEN=knmknDqqwdQuj +GITLAB_HOST=https://git.miem.hse.ru +GROUP_NAME=biv23x-ps +TIMEOUT=120000 +``` \ No newline at end of file diff --git a/src/index.js b/src/index.js index 4cd0f0ab61c6e86f0c4e970c02bd2e4359742eb1..d3e194cd2472675c8279fe8e64844f646aa64367 100644 --- a/src/index.js +++ b/src/index.js @@ -29,8 +29,8 @@ function readStudentsFile() { }); } -async function getProject(slug) { - const [project] = await api.Projects.search(slug); +async function getProject(slug, groupId) { + const [project] = await api.Projects.search(slug, { namespace_id: groupId }); return project; } @@ -62,11 +62,17 @@ async function addUserToProject(userId, projectId) { async function main() { console.info(`Start sync ${new Date().toISOString()}`); + + const group = await api.Groups.show(config.groupName); + if (!group) { + console.error(`Group ${config.groupName} in ${config.gitlabHost} not found. Create it before running this service.`); + return; + } + const students = readStudentsFile(); for (const student of students) { - try { - const project = await getProject(student.slug) || await addProjectToGroup(student.name, student.slug); + const project = await getProject(student.slug, group.id) || await addProjectToGroup(student.name, student.slug); const user = await getUserBySlug(student.slug); if (!user) { @@ -85,6 +91,8 @@ async function main() { console.log("Start program"); +main().catch(err => console.error(err)); + setInterval(() => { main().catch(err => console.error(err)); }, config.timeout);