diff --git a/deno.json b/deno.json index f04eb4e..9022fef 100644 --- a/deno.json +++ b/deno.json @@ -1,7 +1,7 @@ { "fmt": { "singleQuote": true, - "semiColons": true, + "semiColons": false, "useTabs": true, "indentWidth": 4 }, diff --git a/mod.ts b/mod.ts index debb314..2be5471 100644 --- a/mod.ts +++ b/mod.ts @@ -1,19 +1,29 @@ -import { Redmine } from 'bluemine'; -import * as CSV from 'std:csv'; -import { CsvEntry } from './type.ts'; +import { Redmine } from 'bluemine' +import * as CSV from 'std:csv' +import { CsvEntry } from './type.ts' if (import.meta.main) { - const endpoint = Deno.env.get('ENDPOINT') - const apiKey = Deno.env.get('API_KEY') + 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') + 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 redmine = new Redmine({ endpoint, apiKey }) - const file = await Deno.readTextFile(prompt('Full path of the CSV file ?:') ?? ''); + const file = await Deno.readTextFile( + prompt('Full path of the CSV file ?:') ?? '', + ) - const csv = CSV.parse(file.replaceAll(';', ','), { - skipFirstRow: true, - }) as CsvEntry[]; -} \ No newline at end of file + const csv = CSV.parse(file.replaceAll(';', ','), { + skipFirstRow: true, + }) as CsvEntry[] +} diff --git a/src/append_to_group.ts b/src/append_to_group.ts index 11dc043..3c70d9a 100644 --- a/src/append_to_group.ts +++ b/src/append_to_group.ts @@ -1,35 +1,35 @@ -import { Redmine } from 'bluemine'; -import { CsvEntry } from '../type.ts'; +import { Redmine } from 'bluemine' +import { CsvEntry } from '../type.ts' 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}`); - } - } + 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/src/create_users.ts b/src/create_users.ts index f4b7f64..5c8a4f3 100644 --- a/src/create_users.ts +++ b/src/create_users.ts @@ -1,32 +1,34 @@ -import { Redmine } from 'bluemine'; -import { CsvEntry } from '../type.ts'; +import { Redmine } from 'bluemine' +import { CsvEntry } from '../type.ts' 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 }); + 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/type.ts b/type.ts index 5927f34..ad5980c 100644 --- a/type.ts +++ b/type.ts @@ -1,5 +1,5 @@ export type CsvEntry = { - firstname: string; - lastname: string; - mail: string; -}; \ No newline at end of file + firstname: string + lastname: string + mail: string +}