mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -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 !
137 lines
6.7 KiB
JavaScript
137 lines
6.7 KiB
JavaScript
// Planka (https://github.com/plankanban/planka, self-hosted) client for the
|
|
// import page (models/lib/apiImportServices.js).
|
|
// credentials: { url, username, password } - the Planka address and a login.
|
|
// The password goes in the body of the login request only, never in a URL;
|
|
// the token it returns is used for this fetch and revoked at the end.
|
|
//
|
|
// Planka's REST API (server/config/routes.js), v1 and v2 alike:
|
|
// POST /api/access-tokens { emailOrUsername, password } -> { item: token }
|
|
// GET /api/projects -> { items: projects, included: { boards } }
|
|
// GET /api/boards/:id -> { item: board, included: { lists, cards, ... } }
|
|
// DELETE /api/access-tokens/me logs out
|
|
// Comments: v2 has GET /api/cards/:cardId/comments ({ items: [{ text, userId }] });
|
|
// v1 has them as actions, GET /api/cards/:cardId/actions, type 'commentCard'
|
|
// with data.text. Both return 50 at a time, newest first, and take ?beforeId=.
|
|
|
|
export const MAX_COMMENT_CARDS = 5000;
|
|
export const MAX_COMMENT_PAGES = 100;
|
|
const PAGE = 50;
|
|
|
|
function base(credentials) {
|
|
const url = credentials && typeof credentials.url === 'string' ? credentials.url.trim().replace(/\/+$/, '') : '';
|
|
if (!/^https?:\/\//i.test(url)) throw new Error('The Planka address must start with https:// or http://');
|
|
return url;
|
|
}
|
|
|
|
const idOf = value => {
|
|
const id = typeof value === 'number' && Number.isSafeInteger(value) && value > 0 ? String(value) : typeof value === 'string' ? value.trim() : '';
|
|
return /^[A-Za-z0-9_-]{1,64}$/.test(id) ? id : '';
|
|
};
|
|
|
|
async function login(credentials, request) {
|
|
const url = base(credentials);
|
|
const username = credentials && typeof credentials.username === 'string' ? credentials.username.trim() : '';
|
|
const password = credentials && typeof credentials.password === 'string' ? credentials.password : '';
|
|
if (!username || !password) throw new Error('A Planka username and password are required');
|
|
const answer = await request(`${url}/api/access-tokens`, {
|
|
method: 'POST', headers: {}, body: { emailOrUsername: username, password },
|
|
});
|
|
const token = answer && typeof answer.item === 'string' ? answer.item : '';
|
|
// Planka asks for a TOTP code or accepting its terms with a pending token
|
|
// instead; neither can be answered from the import page.
|
|
if (!token) throw new Error('Planka did not log in (two-factor login or terms to accept are not supported)');
|
|
return { url, headers: { Authorization: `Bearer ${token}` } };
|
|
}
|
|
|
|
async function logout(session, request) {
|
|
try { await request(`${session.url}/api/access-tokens/me`, { method: 'DELETE', headers: session.headers }); } catch (error) { /* the token expires anyway */ }
|
|
}
|
|
|
|
const get = (session, path, request) => request(`${session.url}${path}`, { method: 'GET', headers: session.headers });
|
|
|
|
export async function list(credentials, request) {
|
|
const session = await login(credentials, request);
|
|
try {
|
|
const answer = await get(session, '/api/projects', request);
|
|
if (!answer || !Array.isArray(answer.items)) throw new Error('Planka returned an unexpected answer for its projects');
|
|
const projects = new Map(answer.items.filter(p => p && idOf(p.id)).map(p => [idOf(p.id), p]));
|
|
const boards = answer.included && Array.isArray(answer.included.boards) ? answer.included.boards : [];
|
|
return boards
|
|
.filter(board => board && idOf(board.id))
|
|
.sort((a, b) => (Number(a.position) || 0) - (Number(b.position) || 0))
|
|
.map(board => {
|
|
const project = projects.get(idOf(board.projectId));
|
|
const projectName = project && typeof project.name === 'string' ? project.name : '';
|
|
const name = typeof board.name === 'string' && board.name ? board.name : 'Untitled';
|
|
return { id: idOf(board.id), name: projectName ? `${projectName} / ${name}` : name };
|
|
});
|
|
} finally {
|
|
await logout(session, request);
|
|
}
|
|
}
|
|
|
|
// One card's comments, every page: v2's comments, or v1's comment actions
|
|
// when the v2 route is not there. `version` remembers which one answered.
|
|
async function cardComments(session, cardId, request, state) {
|
|
const out = [];
|
|
let beforeId = '';
|
|
for (let page = 0; page < MAX_COMMENT_PAGES; page += 1) {
|
|
const query = beforeId ? `?beforeId=${encodeURIComponent(beforeId)}` : '';
|
|
let answer;
|
|
if (state.version !== 1) {
|
|
try {
|
|
answer = await get(session, `/api/cards/${encodeURIComponent(cardId)}/comments${query}`, request);
|
|
state.version = 2;
|
|
} catch (error) {
|
|
if (error && error.status === 404 && !state.version) state.version = 1;
|
|
else throw error;
|
|
}
|
|
}
|
|
if (state.version === 1) answer = await get(session, `/api/cards/${encodeURIComponent(cardId)}/actions${query}`, request);
|
|
const items = answer && Array.isArray(answer.items) ? answer.items : [];
|
|
const users = answer && answer.included && Array.isArray(answer.included.users) ? answer.included.users : [];
|
|
users.forEach(user => { if (user && idOf(user.id)) state.users[idOf(user.id)] = { username: user.username || '', name: user.name || '' }; });
|
|
for (const item of items) {
|
|
if (!item) continue;
|
|
const text = state.version === 1
|
|
? (item.type === 'commentCard' && item.data && typeof item.data.text === 'string' ? item.data.text : '')
|
|
: (typeof item.text === 'string' ? item.text : '');
|
|
if (text) out.push({ text, userId: idOf(item.userId) || null, createdAt: item.createdAt || null });
|
|
}
|
|
if (items.length < PAGE) break;
|
|
beforeId = idOf(items[items.length - 1] && items[items.length - 1].id);
|
|
if (!beforeId) break;
|
|
}
|
|
return out.reverse();
|
|
}
|
|
|
|
export async function fetch(credentials, id, request) {
|
|
const boardId = idOf(id);
|
|
if (!boardId) throw new Error('Choose a Planka board');
|
|
const session = await login(credentials, request);
|
|
try {
|
|
const planka = await get(session, `/api/boards/${encodeURIComponent(boardId)}`, request);
|
|
if (!planka || !planka.item || !planka.included) throw new Error('Planka returned an unexpected answer for the board');
|
|
const cards = Array.isArray(planka.included.cards) ? planka.included.cards : [];
|
|
const comments = {};
|
|
const state = { version: 0, users: {} };
|
|
let skipped = 0;
|
|
let asked = 0;
|
|
for (const card of cards) {
|
|
const cardId = idOf(card && card.id);
|
|
if (!cardId) continue;
|
|
// v2 counts a card's comments; a card with none needs no request.
|
|
if (card.commentsTotal === 0) continue;
|
|
if (asked >= MAX_COMMENT_CARDS) { skipped += 1; continue; }
|
|
asked += 1;
|
|
const list = await cardComments(session, cardId, request, state);
|
|
if (list.length) comments[cardId] = list;
|
|
}
|
|
const result = { planka, comments, commentUsers: state.users };
|
|
if (skipped) result.commentsSkipped = skipped;
|
|
return result;
|
|
} finally {
|
|
await logout(session, request);
|
|
}
|
|
}
|