Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn logging if thread_ts is not a string #1223

Merged
merged 2 commits into from May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/web-api/src/WebClient.spec.js
Expand Up @@ -227,6 +227,57 @@ describe('WebClient', function () {
});
});
});

const threadTsTestPatterns = [
{ method: 'chat.postEphemeral' },
{ method: 'chat.postMessage' },
{ method: 'chat.scheduleMessage' },
{ method: 'files.upload' },
];

threadTsTestPatterns.reduce((acc, { method, args }) => {
const threadTs = [{ thread_ts: 1503435956.000247, text: 'text' }]
.map(v => ({ method, args: Object.assign({}, v, args) }))
return acc.concat(threadTs)
}, []).forEach(({ method, args }) => {
it(`should send warning to logs when thread_ts in ${method} arguments is a float`, function () {
const logger = {
debug: sinon.spy(),
info: sinon.spy(),
warn: sinon.spy(),
error: sinon.spy(),
setLevel: sinon.spy(),
setName: sinon.spy(),
};
const warnClient = new WebClient(token, { logLevel: LogLevel.WARN, logger });
return warnClient.apiCall(method, args)
.then(() => {
assert.isTrue(logger.warn.callCount === 4);
});
});
});

threadTsTestPatterns.reduce((acc, { method, args }) => {
const threadTs = [{ thread_ts: '1503435956.000247', text: 'text' }]
.map(v => ({ method, args: Object.assign({}, v, args) }))
return acc.concat(threadTs)
}, []).forEach(({ method, args }) => {
it(`should not send warning to logs when thread_ts in ${method} arguments is a string`, function () {
const logger = {
debug: sinon.spy(),
info: sinon.spy(),
warn: sinon.spy(),
error: sinon.spy(),
setLevel: sinon.spy(),
setName: sinon.spy(),
};
const warnClient = new WebClient(token, { logLevel: LogLevel.WARN, logger });
return warnClient.apiCall(method, args)
.then(() => {
assert.isTrue(logger.warn.calledThrice);
});
});
});
});

describe('with OAuth scopes in the response headers', function () {
Expand Down
17 changes: 17 additions & 0 deletions packages/web-api/src/WebClient.ts
Expand Up @@ -156,6 +156,7 @@ export class WebClient extends Methods {

warnDeprecations(method, this.logger);
warnIfFallbackIsMissing(method, this.logger, options);
warnIfThreadTsIsNotString(method, this.logger, options);

if (typeof options === 'string' || typeof options === 'number' || typeof options === 'boolean') {
throw new TypeError(`Expected an options argument but instead received a ${typeof options}`);
Expand Down Expand Up @@ -619,6 +620,7 @@ function warnDeprecations(method: string, logger: Logger): void {
* Log a warning when using chat.postMessage without text argument or attachments with fallback argument
* @param method api method being called
* @param logger instance of we clients logger
* @param options arguments for the Web API method
*/
function warnIfFallbackIsMissing(method: string, logger: Logger, options?: WebAPICallOptions): void {
const targetMethods = ['chat.postEphemeral', 'chat.postMessage', 'chat.scheduleMessage', 'chat.update'];
Expand All @@ -645,3 +647,18 @@ function warnIfFallbackIsMissing(method: string, logger: Logger, options?: WebAP
}
}
}

/**
* Log a warning when thread_ts is not a string
* @param method api method being called
* @param logger instance of web clients logger
* @param options arguments for the Web API method
*/
function warnIfThreadTsIsNotString(method: string, logger: Logger, options?: WebAPICallOptions): void {
const targetMethods = ['chat.postEphemeral', 'chat.postMessage', 'chat.scheduleMessage', 'files.upload'];
const isTargetMethod = targetMethods.includes(method);

if (isTargetMethod && options?.thread_ts !== undefined && typeof options?.thread_ts !== 'string') {
logger.warn(`The given thread_ts value in the request payload for a ${method} call is a float value. We highly recommend using a string value instead.`);
}
}