From 65bba68feb883c3dd36650391e769a5b9b78e3b4 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Mon, 15 Jan 2024 20:46:54 +0100 Subject: [PATCH] initial commit --- deno.json | 12 ++++++++++ import_csv_users.ts | 47 ++++++++++++++++++++++++++++++++++++ import_csv_users_group.ts | 50 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 deno.json create mode 100644 import_csv_users.ts create mode 100644 import_csv_users_group.ts diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..f04eb4e --- /dev/null +++ b/deno.json @@ -0,0 +1,12 @@ +{ + "fmt": { + "singleQuote": true, + "semiColons": true, + "useTabs": true, + "indentWidth": 4 + }, + "imports": { + "std:csv": "https://deno.land/std@0.212.0/csv/mod.ts", + "bluemine": "https://deno.land/x/bluemine@0.1.3/mod.ts" + } +} diff --git a/import_csv_users.ts b/import_csv_users.ts new file mode 100644 index 0000000..7970cea --- /dev/null +++ b/import_csv_users.ts @@ -0,0 +1,47 @@ +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()}`; +} + +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 new file mode 100644 index 0000000..880fcd4 --- /dev/null +++ b/import_csv_users_group.ts @@ -0,0 +1,50 @@ +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}`); + } +}