UNPKG

bugzilla

Version:

A NodeJS module to access Bugzilla instances through the REST API.

155 lines 6.01 kB
import { URL, URLSearchParams } from 'url'; import { PublicLink, params, PasswordLink, ApiKeyLink } from './link'; import { FilteredQuery } from './query'; import { HistoryLookupSpec, BugSpec, UserSpec, VersionSpec, CommentsSpec, CreatedCommentSpec, CreatedBugSpec, UpdatedBugTemplateSpec, AttachmentsSpec, CreatedAttachmentSpec, UpdatedAttachmentTemplateSpec, } from './types'; import { array, object } from './validators'; export default class BugzillaAPI { constructor(instance, user, password, restrictLogin = false) { let url = instance instanceof URL ? instance : new URL(instance); if (!user) { this.link = new PublicLink(url); } else if (password !== undefined) { this.link = new PasswordLink(url, user, password, restrictLogin); } else { this.link = new ApiKeyLink(url, user); } } async version() { let version = await this.link.get('version', object(VersionSpec)); return version.version; } whoami() { return this.link.get('whoami', object(UserSpec)); } async bugHistory(bugId, since) { var _a; let searchParams; if (since) { searchParams = new URLSearchParams(); searchParams.set('new_since', (_a = since.toISODate()) !== null && _a !== void 0 ? _a : ''); } let bugs = await this.link.get(`bug/${bugId}/history`, object(HistoryLookupSpec), searchParams); let [bug] = bugs.bugs; if (!bug) { throw new Error('Bug not found.'); } return bug.history; } searchBugs(query) { return new FilteredQuery(async (includes, excludes) => { let search = params(query); if (includes) { search.set('include_fields', includes.join(',')); } if (excludes) { search.set('exclude_fields', excludes.join(',')); } let result = await this.link.get('bug', object({ bugs: array(object(BugSpec, includes, excludes)), }), search); return result.bugs; }); } getBugs(ids) { return this.searchBugs({ id: ids.join(','), }); } quicksearch(query) { return this.searchBugs({ quicksearch: query, }); } advancedSearch(query) { let searchParams; if (query instanceof URL) { searchParams = query.searchParams; } else if (typeof query == 'string' && query.startsWith('http')) { searchParams = new URL(query).searchParams; } else if (query instanceof URLSearchParams) { searchParams = query; } else { searchParams = new URLSearchParams(query); } searchParams.delete('list_id'); return this.searchBugs(searchParams); } async getComment(commentId) { let comment = await this.link.get(`bug/comment/${commentId}`, object(CommentsSpec)); if (!comment) { throw new Error(`Failed to get comment #${commentId}.`); } return comment.comments.get(commentId); } async getBugComments(bugId) { var _a; let comments = await this.link.get(`bug/${bugId}/comment`, object(CommentsSpec)); if (!comments) { throw new Error(`Failed to get comments of bug #${bugId}.`); } return (_a = comments.bugs.get(bugId)) === null || _a === void 0 ? void 0 : _a.comments; } async createComment(bugId, comment, options = {}) { var _a; const content = { comment, is_private: (_a = options.is_private) !== null && _a !== void 0 ? _a : false, }; let commentStatus = await this.link.post(`bug/${bugId}/comment`, object(CreatedCommentSpec), content); if (!commentStatus) { throw new Error('Failed to create comment.'); } return commentStatus.id; } async createBug(bug) { let bugStatus = await this.link.post('bug', object(CreatedBugSpec), bug); if (!bugStatus) { throw new Error('Failed to create bug.'); } return bugStatus.id; } async updateBug(bugIdOrAlias, data) { let response = await this.link.put(`bug/${bugIdOrAlias}`, object(UpdatedBugTemplateSpec), data); if (!response) { throw new Error(`Failed to update bug #${bugIdOrAlias}.`); } return response.bugs; } async getAttachment(attachmentId) { let attachment = await this.link.get(`bug/attachment/${attachmentId}`, object(AttachmentsSpec)); if (!attachment) { throw new Error(`Failed to get attachment #${attachmentId}.`); } return attachment.attachments.get(attachmentId); } async getBugAttachments(bugId) { let attachments = await this.link.get(`bug/${bugId}/attachment`, object(AttachmentsSpec)); if (!attachments) { throw new Error(`Failed to get attachments of bug #${bugId}.`); } return attachments.bugs.get(bugId); } async createAttachment(bugId, attachment) { const dataBase64 = { data: Buffer.from(attachment.data).toString('base64'), }; let attachmentStatus = await this.link.post(`bug/${bugId}/attachment`, object(CreatedAttachmentSpec), Object.assign(Object.assign({}, attachment), dataBase64)); if (!attachmentStatus) { throw new Error('Failed to create attachment.'); } return attachmentStatus.ids; } async updateAttachment(attachmentId, data) { let response = await this.link.put(`bug/attachment/${attachmentId}`, object(UpdatedAttachmentTemplateSpec), data); if (!response) { throw new Error(`Failed to update attachment #${attachmentId}.`); } return response.attachments; } } //# sourceMappingURL=index.js.map