From 8ad3c1dba48d1e083120c7a7e9c1ae9987b64303 Mon Sep 17 00:00:00 2001
From: Andrey Piskunov <adpiskunov@edu.hse.ru>
Date: Mon, 27 Jan 2025 15:21:21 +0300
Subject: [PATCH] :bug: Fix find project by group

---
 .env.example |  2 +-
 README.md    | 26 +++++++++++++++++++-------
 src/index.js | 16 ++++++++++++----
 3 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/.env.example b/.env.example
index 7dd228e..fa9583c 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 0e4a7ce..cb73c7d 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 4cd0f0a..d3e194c 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);
-- 
GitLab