mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Decided on 2026-10-14. On the import page each of the six asks for what its service needs - a self-hosted Planka its address, username and password; the others an access token (Basecamp also its account id) - lists what that account can import, and fetches the chosen board, list or project. The credentials go with each call and are used for that call's requests only; they are never stored or logged, and the page clears secret fields once a fetch has used them. The fetched JSON fills the import text box and the import goes on as for a pasted export: the service's parser (models/lib/<key>ApiFormat.js) into the generalized importer, with the members step and the loss report. The same JSON can be pasted, or posted to POST /api/boards/import/<key>. - server/apiImport.js: apiImportList and apiImportFetch, signed in only, off with the Admin Panel's import switch, not in Sandstorm, rate limited; credentials checked against the service's own fields (models/lib/apiImportServices.js). - server/lib/apiImportRequest.js: every request through the SSRF guard - pinned address, no redirects, bounded in time and size - so a Planka address cannot point the server at its own network; an administrator can allow such a host with API_IMPORT_ALLOWED_PRIVATE_HOSTS. An error names only the origin and the status. - models/lib/apiImport/<key>.js: each service's client, plain Node with the request injected, paging bounded and the token only in a header. - Each has a docs page under docs/Features/ImportExport/, listed in the coverage index and External-Tools.md. Tests: tests/<key>ApiImport.test.cjs (client with a fake request, every parsed field, wrong shapes), tests/apiImport.test.cjs (credentials, the gate, the wiring, the shapes refused), api-import.e2e.js (the form for every service, fetched JSON imported through the page, refusals incl. a private address). Live tests, read only, run from build.sh / build.bat -> Tests -> Live tests with your own accounts (releases/live-tests/README.md); they were not run here. New strings translated in all locales. Also: the release risk baseline allows the services' API addresses, and the guards that count import creators, list the import sources and pin the platform fetch() calls know the new ones. Thanks to xet7 !
140 lines
6.9 KiB
JavaScript
140 lines
6.9 KiB
JavaScript
// Basecamp (Basecamp 4 / 3 API, https://github.com/basecamp/bc3-api) client
|
|
// for the import page (models/lib/apiImportServices.js).
|
|
// credentials: { account, token } - the account id, the number after
|
|
// basecamp.com/ in the address, and an OAuth 2 access token. The token goes in
|
|
// the Authorization header only, never in a URL. Basecamp refuses a request
|
|
// without a User-Agent naming the app.
|
|
//
|
|
// One project is fetched: its card tables (the dock's kanban_board) with each
|
|
// column's cards, and its to-do set's to-do lists with their pending and
|
|
// completed to-dos. The flat routes (/card_tables/2.json) are the ones bc3-api
|
|
// documents as canonical; the project-scoped /buckets/1/... ones are legacy.
|
|
//
|
|
// Paging: Basecamp sends the next page in a Link header, which `request` does
|
|
// not hand back (it resolves to the JSON body only). Every collection also
|
|
// answers ?page=N, so pages are read N = 1, 2, ... until an empty one, at most
|
|
// MAX_PAGES of them.
|
|
|
|
const HOST = 'https://3.basecampapi.com';
|
|
const USER_AGENT = 'WeKan (https://wekan.fi)';
|
|
export const MAX_PAGES = 200;
|
|
export const MAX_PROJECTS = 5000;
|
|
export const MAX_CARD_TABLES = 20;
|
|
export const MAX_LISTS = 200;
|
|
export const MAX_ITEMS = 20000;
|
|
export const MAX_COMMENTS = 20000;
|
|
|
|
function base(credentials) {
|
|
const account = credentials && typeof credentials.account === 'string' ? credentials.account.trim() : '';
|
|
if (!/^\d{1,20}$/.test(account)) throw new Error('The Basecamp account id is the number in the Basecamp address');
|
|
return `${HOST}/${account}`;
|
|
}
|
|
|
|
function headers(credentials) {
|
|
const token = credentials && typeof credentials.token === 'string' ? credentials.token.trim() : '';
|
|
if (!token) throw new Error('A Basecamp access token is required');
|
|
return { Authorization: `Bearer ${token}`, Accept: 'application/json', 'User-Agent': USER_AGENT };
|
|
}
|
|
|
|
const idOf = value => {
|
|
const id = typeof value === 'number' && Number.isSafeInteger(value) && value > 0 ? String(value) : typeof value === 'string' ? value.trim() : '';
|
|
return /^\d{1,20}$/.test(id) ? id : '';
|
|
};
|
|
|
|
const get = (url, credentials, request) => request(url, { method: 'GET', headers: headers(credentials) });
|
|
|
|
// Every item of a paged collection: ?page=1, 2, ... until an empty page.
|
|
async function paged(url, credentials, request, limit, what) {
|
|
const items = [];
|
|
const join = url.includes('?') ? '&' : '?';
|
|
for (let page = 1; ; page += 1) {
|
|
if (page > MAX_PAGES) throw new Error(`Basecamp returned too many pages of ${what}`);
|
|
const answer = await get(`${url}${join}page=${page}`, credentials, request);
|
|
if (!Array.isArray(answer)) throw new Error(`Basecamp returned an unexpected answer for ${what}`);
|
|
if (!answer.length) return items;
|
|
items.push(...answer);
|
|
if (items.length > limit) throw new Error(`Basecamp has more than ${limit} ${what} to import`);
|
|
}
|
|
}
|
|
|
|
// A URL Basecamp handed back (comments_url) is only followed below the
|
|
// account's own API address, so the token is never sent anywhere else.
|
|
function ownUrl(value, root) {
|
|
if (typeof value !== 'string') return '';
|
|
let parsed;
|
|
try { parsed = new URL(value); } catch (error) { return ''; }
|
|
if (parsed.origin !== HOST || parsed.username || parsed.password) return '';
|
|
return parsed.pathname.startsWith(`${new URL(root).pathname}/`) ? `${parsed.origin}${parsed.pathname}` : '';
|
|
}
|
|
|
|
export async function list(credentials, request) {
|
|
const root = base(credentials);
|
|
const projects = await paged(`${root}/projects.json`, credentials, request, MAX_PROJECTS, 'projects');
|
|
return projects
|
|
.filter(project => project && idOf(project.id))
|
|
.map(project => ({ id: idOf(project.id), name: typeof project.name === 'string' && project.name.trim() ? project.name.trim() : idOf(project.id) }));
|
|
}
|
|
|
|
export async function fetch(credentials, id, request) {
|
|
const root = base(credentials);
|
|
const projectId = idOf(id);
|
|
if (!projectId) throw new Error('Choose a Basecamp project to import');
|
|
const project = await get(`${root}/projects/${projectId}.json`, credentials, request);
|
|
if (!project || typeof project !== 'object' || Array.isArray(project)) throw new Error('Basecamp returned an unexpected project');
|
|
const dock = (Array.isArray(project.dock) ? project.dock : []).filter(tool => tool && tool.enabled !== false && idOf(tool.id));
|
|
const budget = { items: 0, comments: 0 };
|
|
|
|
// Comments of a recording, only when it says it has some.
|
|
const commentsOf = async (recording) => {
|
|
const count = Number(recording.comments_count ?? recording.comment_count) || 0;
|
|
const url = count > 0 ? ownUrl(recording.comments_url, root) : '';
|
|
if (!url) return [];
|
|
const comments = await paged(url, credentials, request, MAX_COMMENTS - budget.comments, 'comments');
|
|
budget.comments += comments.length;
|
|
return comments;
|
|
};
|
|
const withComments = async (items) => {
|
|
budget.items += items.length;
|
|
if (budget.items > MAX_ITEMS) throw new Error(`Basecamp has more than ${MAX_ITEMS} cards and to-dos to import`);
|
|
const out = [];
|
|
for (const item of items) {
|
|
if (!item || typeof item !== 'object') continue;
|
|
out.push({ ...item, comments: await commentsOf(item) });
|
|
}
|
|
return out;
|
|
};
|
|
|
|
const cardTables = [];
|
|
for (const tool of dock.filter(tool => tool.name === 'kanban_board').slice(0, MAX_CARD_TABLES)) {
|
|
const table = await get(`${root}/card_tables/${idOf(tool.id)}.json`, credentials, request);
|
|
const lists = [];
|
|
for (const column of (table && Array.isArray(table.lists) ? table.lists : []).slice(0, MAX_LISTS)) {
|
|
const listId = column && idOf(column.id);
|
|
if (!listId) continue;
|
|
const cards = await paged(`${root}/card_tables/lists/${listId}/cards.json`, credentials, request, MAX_ITEMS - budget.items, 'cards');
|
|
lists.push({ id: listId, title: column.title, type: column.type, color: column.color, cards: await withComments(cards) });
|
|
}
|
|
cardTables.push({ id: idOf(tool.id), title: (table && table.title) || tool.title, lists });
|
|
}
|
|
|
|
const todolists = [];
|
|
for (const tool of dock.filter(tool => tool.name === 'todoset').slice(0, MAX_CARD_TABLES)) {
|
|
const lists = await paged(`${root}/todosets/${idOf(tool.id)}/todolists.json`, credentials, request, MAX_LISTS, 'to-do lists');
|
|
for (const todolist of lists) {
|
|
const listId = todolist && idOf(todolist.id);
|
|
if (!listId) continue;
|
|
const pending = await paged(`${root}/todolists/${listId}/todos.json`, credentials, request, MAX_ITEMS - budget.items, 'to-dos');
|
|
const done = await paged(`${root}/todolists/${listId}/todos.json?completed=true`, credentials, request, MAX_ITEMS - budget.items - pending.length, 'to-dos');
|
|
todolists.push({ id: listId, title: todolist.title || todolist.name, description: todolist.description,
|
|
todos: await withComments([...pending, ...done]) });
|
|
}
|
|
}
|
|
|
|
return {
|
|
basecamp: true,
|
|
project: { id: projectId, name: project.name, description: project.description, created_at: project.created_at },
|
|
cardTables,
|
|
todolists,
|
|
};
|
|
}
|