2024-01-15 22:55:53 +01:00
|
|
|
import { Redmine } from 'bluemine'
|
|
|
|
import * as CSV from 'std:csv'
|
2024-01-15 23:12:49 +01:00
|
|
|
import { CsvEntry } from './type.ts'
|
2024-01-15 22:55:53 +01:00
|
|
|
import { createUsers } from './src/create_users.ts'
|
|
|
|
import { appendToGroup } from './src/append_to_group.ts'
|
2024-01-15 23:12:49 +01:00
|
|
|
import { Command } from 'cliffy'
|
2024-01-15 22:55:53 +01:00
|
|
|
|
2024-01-15 23:12:49 +01:00
|
|
|
await new Command()
|
|
|
|
.name('redmine-csv-loader')
|
|
|
|
.description('Create Redmine users from CSV and add users to a group.')
|
|
|
|
.version('0.1.0')
|
|
|
|
.option('-g, --group <group:string>', 'Name of the group to add users in.')
|
2024-01-15 23:32:51 +01:00
|
|
|
.option(
|
2024-01-15 23:35:24 +01:00
|
|
|
'-a, --only-add [only-add:boolean]',
|
2024-01-15 23:32:51 +01:00
|
|
|
"Don't create users, only add them to a group",
|
|
|
|
)
|
2024-01-15 23:12:49 +01:00
|
|
|
.option('-d, --endpoint <endpoint:string>', 'Redmine server address.', {
|
|
|
|
default: 'https://projets.cohabit.fr/redmine',
|
|
|
|
})
|
|
|
|
.option(
|
|
|
|
'-k, --api-key <api-key:string>',
|
|
|
|
'Your Redmine API Key (see: https://www.redmine.org/boards/2/topics/53956/).',
|
|
|
|
{ required: true },
|
2024-01-15 22:55:53 +01:00
|
|
|
)
|
2024-01-15 23:12:49 +01:00
|
|
|
.arguments('<csv:file>')
|
2024-01-16 11:18:23 +01:00
|
|
|
.action(async ({ group, endpoint, apiKey, onlyAdd }, filePath) => {
|
2024-01-15 23:12:49 +01:00
|
|
|
const redmine = new Redmine({ endpoint, apiKey })
|
2024-01-15 22:55:53 +01:00
|
|
|
|
2024-01-16 11:18:23 +01:00
|
|
|
const file = await Deno.readTextFile(filePath)
|
2024-01-15 22:55:53 +01:00
|
|
|
|
2024-01-15 23:12:49 +01:00
|
|
|
const csv = CSV.parse(file.replaceAll(';', ','), {
|
|
|
|
skipFirstRow: true,
|
|
|
|
}) as CsvEntry[]
|
2024-01-15 22:55:53 +01:00
|
|
|
|
2024-01-15 23:32:51 +01:00
|
|
|
if (!onlyAdd) createUsers(redmine, csv)
|
2024-01-15 23:12:49 +01:00
|
|
|
if (group) {
|
2024-01-16 11:19:19 +01:00
|
|
|
appendToGroup(redmine, csv, group)
|
2024-01-15 23:12:49 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.parse()
|