From c43a64033b1e6476dacaadc75b5edb1809f844bb Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Mon, 15 Jan 2024 21:01:07 +0100 Subject: [PATCH] refactor: extract subscripts in functions --- .env | 2 ++ import_csv_users.ts | 66 +++++++++++++++-------------------- import_csv_users_group.ts | 72 +++++++++++++++++---------------------- mod.ts | 19 +++++++++++ 4 files changed, 80 insertions(+), 79 deletions(-) create mode 100644 .env create mode 100644 mod.ts diff --git a/.env b/.env new file mode 100644 index 0000000..efc79bf --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +API_KEY="" +ENDPOINT="https://projets.cohabit.fr/redmine" \ No newline at end of file diff --git a/import_csv_users.ts b/import_csv_users.ts index 7970cea..5c6dc4d 100644 --- a/import_csv_users.ts +++ b/import_csv_users.ts @@ -1,47 +1,37 @@ -import * as CSV from 'std:csv'; import { Redmine } from 'bluemine'; -const endpoint = 'https://projets.cohabit.fr/redmine'; -const apiKey = ''; -const redmine = new Redmine({ endpoint, apiKey }); - type CsvEntry = { firstname: string; lastname: string; mail: string; }; -const file = await Deno.readTextFile( - 'C:/Users/Julien/Documents/Stage/FabLab/entrepreneur_full.csv', -); -const csv = CSV.parse(file.replaceAll(';', ','), { - skipFirstRow: true, -}) as CsvEntry[]; - -const userIds: number[] = []; - -function capitalize(str: string): string { - const [first, ...tail] = str.split(''); - return `${first.toLocaleUpperCase()}${tail.join('').toLocaleLowerCase()}`; +async function createUsers(redmine: Redmine, csv: CsvEntry[]) { + const userIds: number[] = []; + + function capitalize(str: string): string { + const [first, ...tail] = str.split(''); + return `${first.toLocaleUpperCase()}${tail.join('').toLocaleLowerCase()}`; + } + + for (const { firstname, lastname, mail } of csv) { + const login = `${firstname.toLowerCase()}.${lastname.toLowerCase()}`; + + try { + const { user } = await redmine.users.create({ + firstname: capitalize(firstname), + lastname: capitalize(lastname), + mail: mail, + login, + sendCreationMail: true, + generatePassword: true, + }); + + userIds.push(user.id); + } catch { + console.error(login); + } + } + + await redmine.unstableGroups.create({ name: 'entrepreneur-01-24', userIds }); } - -for (const { firstname, lastname, mail } of csv) { - const login = `${firstname.toLowerCase()}.${lastname.toLowerCase()}`; - - try { - const { user } = await redmine.users.create({ - firstname: capitalize(firstname), - lastname: capitalize(lastname), - mail: mail, - login, - sendCreationMail: true, - generatePassword: true, - }); - - userIds.push(user.id); - } catch { - console.error(login); - } -} - -await redmine.unstableGroups.create({ name: 'entrepreneur-01-24', userIds }); diff --git a/import_csv_users_group.ts b/import_csv_users_group.ts index 880fcd4..a74c707 100644 --- a/import_csv_users_group.ts +++ b/import_csv_users_group.ts @@ -1,50 +1,40 @@ -import * as CSV from 'std:csv'; import { Redmine } from 'bluemine'; -const endpoint = 'https://projets.cohabit.fr/redmine'; -const apiKey = ''; -const redmine = new Redmine({ endpoint, apiKey }); - type CsvEntry = { firstname: string; lastname: string; mail: string; }; -const file = await Deno.readTextFile( - 'C:/Users/Julien/Documents/Stage/FabLab/entrepreneur_full.csv', -); -const csv = CSV.parse(file.replaceAll(';', ','), { - skipFirstRow: true, -}) as CsvEntry[]; - -const users: { id: number; login: string }[] = []; - -for (const { firstname, lastname } of csv) { - const login = `${firstname.toLowerCase()}.${lastname.toLowerCase()}`; - - try { - const list = await redmine.users.list({ limit: 1, name: login }); - const user = list.users[0]; - - console.log(`get user: ${user.login}#${user.id}`); - users.push(user); - } catch { - console.error(`error list: ${login}`); - } -} - -const { groups } = await redmine.unstableGroups.list() as unknown as { - groups: { id: number; name: string }[]; -}; - -const { id } = groups.filter(({ name }) => name === 'entrepreneur-01-24')[0]; - -for (const user of users) { - try { - console.error(`ok: ${user.login}#${user.id}`); - await redmine.unstableGroups.addUser(id, user); - } catch { - console.error(`error: ${user.login}`); - } +async function addUsersToGroup(redmine: Redmine, csv: CsvEntry[]) { + const users: { id: number; login: string }[] = []; + + for (const { firstname, lastname } of csv) { + const login = `${firstname.toLowerCase()}.${lastname.toLowerCase()}`; + + try { + const list = await redmine.users.list({ limit: 1, name: login }); + const user = list.users[0]; + + console.log(`get user: ${user.login}#${user.id}`); + users.push(user); + } catch { + console.error(`error list: ${login}`); + } + } + + const { groups } = await redmine.unstableGroups.list() as unknown as { + groups: { id: number; name: string }[]; + }; + + const { id } = groups.filter(({ name }) => name === 'entrepreneur-01-24')[0]; + + for (const user of users) { + try { + console.error(`ok: ${user.login}#${user.id}`); + await redmine.unstableGroups.addUser(id, user); + } catch { + console.error(`error: ${user.login}`); + } + } } diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..7d7c4e6 --- /dev/null +++ b/mod.ts @@ -0,0 +1,19 @@ +import { Redmine } from 'bluemine'; +import * as CSV from 'std:csv'; + +if (import.meta.main) { + const endpoint = Deno.env.get('ENDPOINT') + const apiKey = Deno.env.get('API_KEY') + + if (apiKey === undefined) throw new Error('Redmine REST API key is missing, please define API_KEY') + if (endpoint === undefined) throw new Error('Redmine REST endpoint is missing, please define ENDPOINT') + + const redmine = new Redmine({ endpoint, apiKey }); + + const file = await Deno.readTextFile( + 'path', + ); + const csv = CSV.parse(file.replaceAll(';', ','), { + skipFirstRow: true, + }) as CsvEntry[]; +} \ No newline at end of file