mattermost-mobile-upstream/detox/e2e/support/server_api/post.ts
yasser khan bcac248ec0
feat(e2e): Integrate TSIO reporting (#9925)
Retire legacy maestro per-platform contexts from cancel-e2e-runs and
document the canonical PR, master, and CMT status context mapping.
Wire Detox + Maestro (PR/main/CMT) into Test System IO with a single
commit status (e2e/mobile; CMT uses e2e/compatibility-matrix-testing)
and groups mobile-pr / mobile-main / cmt-mobile.

Upload shard results with correct framework labels, merge Jest shards
into TSIO Detox shape, stub missing/dead shards as failures, and post
channel summaries after status settles. Matterwick MASTER is treated as
MAIN for mobile-main routing.

Also harden report/fail paths, stage Maestro screenshots per flow for
TSIO galleries, fix a few Detox setup/testID issues, and skip known
platform flakes so the pipeline stays actionable while TSIO lands.
2026-07-23 06:43:28 +00:00

275 lines
9.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import path from 'path';
import {timeouts, wait} from '@support/utils';
import client from './client';
import {apiUploadFile, getResponseFromError} from './common';
// ****************************************************************
// Posts
// See https://api.mattermost.com/#tag/posts
//
// Exported API function should have the following:
// - documented using JSDoc
// - meaningful description
// - match the referenced API endpoints
// - parameter/s defined by `@param`
// - return value defined by `@return`
// ****************************************************************
/**
* Create a new post in a channel. To create the post as a comment on another post, provide root_id.
* See https://api.mattermost.com/#operation/CreatePost
* @param {string} baseUrl - the base server URL
* @param {string} option.channelId - The channel ID to post in
* @param {string} option.message - The message contents, can be formatted with Markdown
* @param {string} option.rootId - The post ID to comment on
* @param {Object} option.props - A general object property bag to attach to the post
* @param {string[]} option.fileIds - Array of file IDs to attach to the post (top-level API field)
* @param {Date} option.createAt - The date the post is created at
* @return {Object} returns {post} on success or {error, status} on error
*/
export const apiCreatePost = async (baseUrl: string, {channelId, message, rootId, props = {}, fileIds, createAt = 0}: any): Promise<any> => {
try {
const payload: Record<string, any> = {
channel_id: channelId,
message,
root_id: rootId,
props,
create_at: createAt,
};
if (fileIds?.length) {
payload.file_ids = fileIds;
}
const response = await client.post(`${baseUrl}/api/v4/posts`, payload);
return {post: response.data};
} catch (err) {
return getResponseFromError(err);
}
};
/**
* Get posts for a channel.
* See https://api.mattermost.com/#operation/GetPostsForChannel
* @param {string} baseUrl - the base server URL
* @param {string} channelId - The channel ID to get the posts for
* @return {Object} returns {posts} on success or {error, status} on error
*/
export const apiGetPostsInChannel = async (baseUrl: string, channelId: string): Promise<any> => {
try {
const response = await client.get(`${baseUrl}/api/v4/channels/${channelId}/posts`);
const {order, posts} = response.data;
const orderedPosts = order.map((postId: string) => posts[postId]);
return {posts: orderedPosts};
} catch (err) {
return getResponseFromError(err);
}
};
/**
* Get last post in a channel.
* @param {string} baseUrl - the base server URL
* @param {string} channelId - The channel ID to get the last post
* @return {Object} returns {post} on success or {error, status} on error
*/
export const apiGetLastPostInChannel = async (baseUrl: string, channelId: string): Promise<any> => {
await wait(timeouts.TWO_SEC);
const response = await apiGetPostsInChannel(baseUrl, channelId);
if (response.error) {
return response;
}
const {posts} = response;
if (!posts?.length) {
return {error: {message: `No posts found in channel ${channelId}`}};
}
return {post: posts[0]};
};
/**
* Patch a post.
* See https://api.mattermost.com/#operation/PatchPost
* @param {string} baseUrl - the base server URL
* @param {string} postId - the post ID
* @param {Object} postData - data to partially update a post
* @return {Object} returns {post} on success or {error, status} on error
*/
export const apiPatchPost = async (baseUrl: string, postId: string, postData: string): Promise<any> => {
try {
const response = await client.put(
`${baseUrl}/api/v4/posts/${postId}/patch`,
postData,
);
return {post: response.data};
} catch (err) {
return getResponseFromError(err);
}
};
/**
* Create an ephemeral post visible only to the given user.
* See https://api.mattermost.com/#operation/CreatePostEphemeral
* @param {string} baseUrl - base server URL
* @param {string} userId - user ID that can see the ephemeral post
* @param {{channel_id: string, message: string, root_id?: string, props?: Record<string, unknown>}} post - ephemeral post payload
* @return {Object} returns {post} on success or {error, status} on error
*/
export const apiCreatePostEphemeral = async (
baseUrl: string,
userId: string,
post: {channel_id: string; message: string; root_id?: string; props?: Record<string, unknown>},
): Promise<any> => {
try {
const response = await client.post(`${baseUrl}/api/v4/posts/ephemeral`, {
user_id: userId,
post,
});
return {post: response.data};
} catch (err) {
return getResponseFromError(err);
}
};
/**
* Create an incoming webhook.
* See https://api.mattermost.com/#operation/CreateIncomingWebhook
* @param {string} baseUrl - base server URL
* @param {{channel_id: string, display_name: string}} hook - incoming webhook payload
* @return {Object} returns {hook} on success or {error, status} on error
*/
export const apiCreateIncomingWebhook = async (
baseUrl: string,
hook: {channel_id: string; display_name: string},
): Promise<any> => {
try {
const response = await client.post(`${baseUrl}/api/v4/hooks/incoming`, hook);
return {hook: response.data};
} catch (err) {
return getResponseFromError(err);
}
};
/**
* POST a payload to an incoming webhook URL path (`/hooks/{id}`).
* @param {string} baseUrl - base server URL
* @param {string} hookId - incoming webhook id/path
* @param {Record<string, unknown>} payload - body sent to the webhook
* @return {Object} returns {data} on success or {error, status} on error
*/
export const apiPostIncomingWebhook = async (
baseUrl: string,
hookId: string,
payload: Record<string, unknown>,
): Promise<any> => {
try {
const response = await client.post(`${baseUrl}/hooks/${hookId}`, payload);
return {data: response.data};
} catch (err) {
return getResponseFromError(err);
}
};
/**
* Pin a post in its channel.
* See https://api.mattermost.com/#operation/PinPost
* @param {string} baseUrl - the base server URL
* @param {string} postId - the post ID
* @return {Object} returns {data} on success or {error, status} on error
*/
export const apiPinPost = async (baseUrl: string, postId: string): Promise<any> => {
try {
const response = await client.post(`${baseUrl}/api/v4/posts/${postId}/pin`);
return {data: response.data};
} catch (err) {
return getResponseFromError(err);
}
};
/**
* Upload a file to a channel via the Mattermost REST API.
* See https://api.mattermost.com/#operation/UploadFile
* @param {string} baseUrl - the base server URL
* @param {string} channelId - The channel ID to upload the file to
* @param {string} absFilePath - The absolute path to the file to upload
* @param {Object} [options]
* @param {boolean} [options.forBookmark] - Upload with bookmark=true so CreatorId is
* "bookmark" (required before creating a type=file channel bookmark).
* @return {Object} returns {fileId} on success or {error, status} on error
*/
export const apiUploadFileToChannel = async (
baseUrl: string,
channelId: string,
absFilePath: string,
options?: {forBookmark?: boolean},
): Promise<any> => {
const query = new URLSearchParams({channel_id: channelId});
if (options?.forBookmark) {
// Server sets FileInfo.CreatorId to "bookmark" when bookmark=true
// (required by ChannelBookmark store for type=file bookmarks).
query.set('bookmark', 'true');
}
const result = await apiUploadFile('files', absFilePath, {
url: `${baseUrl}/api/v4/files?${query.toString()}`,
method: 'POST',
});
if (result.error) {
return result;
}
const fileId = result.data?.file_infos?.[0]?.id;
if (!fileId) {
return {error: {message: 'Upload response missing file_infos[0].id'}};
}
return {fileId};
};
/**
* Upload a fixture image and create a post with that image attached.
* @param {string} baseUrl - the base server URL
* @param {string} channelId - The channel ID to post in
* @param {string} rootId - (optional) root post ID for thread replies
* @return {Object} returns {post, fileId} on success or {error, status} on error
*/
export const apiCreatePostWithImageAttachment = async (baseUrl: string, channelId: string, rootId = ''): Promise<any> => {
const absFilePath = path.resolve(__dirname, '../../support/fixtures/image.png');
const {fileId, error: uploadError} = await apiUploadFileToChannel(baseUrl, channelId, absFilePath);
if (uploadError) {
return {error: uploadError};
}
const {post, error: postError} = await apiCreatePost(baseUrl, {
channelId,
message: '',
rootId: rootId || undefined,
fileIds: [fileId],
});
if (postError) {
return {error: postError};
}
if (!post.file_ids || !post.file_ids.includes(fileId)) {
return {error: {message: `Server did not attach file to post. post.file_ids=${JSON.stringify(post.file_ids)}, fileId=${fileId}`}};
}
return {post, fileId};
};
export const Post = {
apiCreatePost,
apiCreatePostEphemeral,
apiCreateIncomingWebhook,
apiCreatePostWithImageAttachment,
apiGetLastPostInChannel,
apiGetPostsInChannel,
apiPinPost,
apiPatchPost,
apiPostIncomingWebhook,
apiUploadFileToChannel,
};
export default Post;