@moroo/wdio-slack-reporter
Version:
Reporter from WebdriverIO using Incoming webhook and Web API to send results to Slack.
262 lines • 8.59 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var client_exports = {};
__export(client_exports, {
SlackWebClient: () => SlackWebClient,
SlackWebhookClient: () => SlackWebhookClient
});
module.exports = __toCommonJS(client_exports);
var import_web_api = require("@slack/web-api");
var import_webhook = require("@slack/webhook");
var import_types = require("./types.js");
var import_utils = require("./utils.js");
class SlackWebClient {
_token;
_client;
/**
* Creates an instance of the Slack Web Client.
*
* @param {SlackWebClientOptions | undefined} options - The options for configuring the Slack client, excluding 'type' and 'channel'.
*/
constructor(options) {
const { token, ...rest } = options;
this._client = new import_web_api.WebClient(token, rest);
this._token = token;
}
/**
* Gets the authentication token for the Slack API.
* @returns The current token value or undefined if not set.
*/
get token() {
return this._token;
}
/**
* Gets the Slack WebClient instance.
* @returns {WebClient} The Slack WebClient instance used for API interactions.
*/
get client() {
return this._client;
}
/**
* Posts a message to a Slack channel.
*
* @param payload - The arguments for posting a message to Slack
* @returns A promise that resolves to the chat post message response
* @throws Will throw and log an error if the message posting fails
*/
async postMessage(payload) {
try {
const response = await this._client.chat.postMessage(payload);
return response;
} catch (error) {
import_utils.logger.error(error);
throw error;
}
}
/**
* Uploads files to Slack using the files.uploadV2 API endpoint.
*
* @param payload - The file upload parameters conforming to FilesUploadV2Arguments
* @param options - Optional configuration for the upload process
* @param options.waitForUpload - Whether to wait for file processing completion (default: true)
* @param options.timeout - Maximum time in milliseconds to wait for upload completion (default: 30000)
* @param options.interval - Polling interval in milliseconds when checking upload status (default: 1000)
*
* @returns Promise resolving to the Slack API response (FilesUploadV2Response)
*
* @throws Will throw and log any errors encountered during the upload process
*
* @example
* const result = await slackClient.uploadV2({
* channel_id: "C12345",
* file: someFileBuffer,
* filename: "report.pdf",
* token: "xoxb-token"
* });
*/
async uploadV2(payload, options) {
const {
waitForUpload = true,
timeout = 3e4,
interval = 1e3
} = options ?? {};
try {
const response = await this._client.filesUploadV2(payload);
if (waitForUpload && response.ok) {
const files = response.files?.flatMap((file) => file.files) ?? [];
for await (const file of files) {
try {
if (file?.id) {
await this.waitForUpload(
file.id,
payload.token,
timeout,
interval
);
}
} catch (error) {
import_utils.logger.error(error);
}
}
}
return response;
} catch (error) {
import_utils.logger.error(error);
throw error;
}
}
/**
* Waits for a file to be processed and uploaded on Slack.
* This method repeatedly checks the file info until the file's mimetype is available,
* indicating that the file has been successfully processed.
*
* @param fileId - The ID of the file to check
* @param timeout - Maximum time in milliseconds to wait for the file upload (default: 30000)
* @param interval - Time in milliseconds between each check (default: 1000)
* @returns A Promise that resolves to the FilesInfoResponse once the file is processed
* @throws {TimeoutError} If the file is not processed within the specified timeout
* @throws {Error} If any error occurs during the file info retrieval
*/
async waitForUpload(fileId, token, timeout = 3e4, interval = 1e3) {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
try {
const response = await this.filesInfo({
token: token ?? this._token,
file: fileId
});
if (response?.file?.mimetype) {
return response;
}
} catch (error) {
import_utils.logger.error(error);
throw error;
}
await (0, import_utils.waitForTimeout)(interval);
}
throw new import_types.TimeoutError(
`Timeout: No file info(mimetype) found within ${timeout / 1e3} seconds`
);
}
/**
* Retrieves information about a file.
*
* @param options - Arguments for retrieving file information
* @returns A promise that resolves to the file information response
*
* @example
* ```typescript
* const fileInfo = await client.filesInfo({
* file: "F123456789"
* });
* ```
*/
async filesInfo(options) {
try {
const response = await this._client.files.info(options);
return response;
} catch (error) {
import_utils.logger.error(error);
throw error;
}
}
/**
* Retrieves an external upload URL for files.
*
* @param options - The arguments for the external upload URL request
* @returns A promise that resolves to a FilesGetUploadURLExternalResponse
* @throws Will throw and log any errors that occur during the request
*/
async getUploadURLExternal(options) {
try {
const response = await this._client.files.getUploadURLExternal(options);
return response;
} catch (error) {
import_utils.logger.error(error);
throw error;
}
}
/**
* Completes an external file upload process.
* Calls the Slack API's files.completeUploadExternal method.
*
* @param options - Arguments required for completing an external upload
* @returns A promise resolving to the response from the Slack API
* @throws Will throw and log any errors that occur during the API call
*/
async completeUploadExternal(options) {
try {
const response = await this._client.files.completeUploadExternal(options);
return response;
} catch (error) {
import_utils.logger.error(error);
throw error;
}
}
}
class SlackWebhookClient {
_url;
_webhook;
/**
* Creates an instance of the Slack IncomingWebhook.
* @param {SlackIncomingWebhookOptions} options - Configuration options for the Slack webhook.
*/
constructor(options) {
const { webhook: url, ...rest } = options;
this._url = url;
this._webhook = new import_webhook.IncomingWebhook(url, rest);
}
/**
* Gets the URL for the Slack webhook.
* @returns The URL string for the Slack webhook.
*/
get url() {
return this._url;
}
/**
* Gets the Slack IncomingWebhook instance.
*
* @returns {IncomingWebhook} The configured Slack webhook client
*/
get webhook() {
return this._webhook;
}
/**
* Sends a payload to Slack via webhook.
*
* @param payload - The content to send to Slack. Can be either a string or an IncomingWebhookSendArguments object.
* @returns A Promise that resolves with the IncomingWebhookResult from Slack.
* @throws Will re-throw any error that occurs during the send operation after logging it.
*/
async send(payload) {
try {
const response = await this._webhook.send(payload);
return response;
} catch (error) {
import_utils.logger.error(error);
throw error;
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SlackWebClient,
SlackWebhookClient
});
//# sourceMappingURL=client.cjs.map