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

[Discourse] Update schema keys to use plural form (topic_count -> topics_count) #9778

Merged

Conversation

joshuacwnewton
Copy link
Contributor

@joshuacwnewton joshuacwnewton commented Dec 3, 2023

This PR does a simple find and replace to update the Discourse schema to use the current keys found in https://meta.discourse.org/site/statistics.json, as the older keys now return "Invalid response".

Note

I was wondering, though, if the old keys should still be preserved, as to not break backwards compatibility with servers using the old names.

(I tried finding the source of the change in Discourse's release notes/changelog, but there's surprisingly little information when querying e.g. "statistics.json" or "topic_count"/"topics_count". So, I'm not sure when this change actually took place, and thus it's hard to estimate the likelihood of there being servers out there using the old keys.)

If we do want to support both sets of keys, then I'm not 100% sure what to do, as I've never written JavaScript before. (I tried reading the Joi docs, and got as far as using .or() in the schema definition for each pair of keys + using a try/catch when creating the metricIntegrations object, but I wasn't confident enough to commit that solution before discussing it first.)

Fixes #9776.

(e.g. topic_count -> topics_countN)
Copy link
Contributor

github-actions bot commented Dec 3, 2023

Messages
📖 ✨ Thanks for your contribution to Shields, @joshuacwnewton!

Generated by 🚫 dangerJS against 7814127

@joshuacwnewton joshuacwnewton changed the title Discourse Service: Update schema keys to use plural form (topic_count -> topics_count) [Discourse] Update schema keys to use plural form (topic_count -> topics_count) Dec 3, 2023
@chris48s
Copy link
Member

chris48s commented Dec 4, 2023

Hi. Thanks for submitting a PR to fix this ❤️

I agree it would be nice to maintain compatibility with both versions.

I think the way to implement this would be to define 2 schemas, one using the plurals and one using the singulars and then use Joi.alternatives() to accept either one or the other.

There's a service that uses this technique in https://github.com/badges/shields/blob/master/services/polymart/polymart-base.js that you could use as an example.

Are you up for updating to follow that approach?

@chris48s chris48s added bug Bugs in badges and the frontend service-badge New or updated service badge labels Dec 4, 2023
@joshuacwnewton
Copy link
Contributor Author

joshuacwnewton commented Dec 4, 2023

I think the way to implement this would be to define 2 schemas, one using the plurals and one using the singulars and then use Joi.alternatives() to accept either one or the other.

Thank you for the guidance! I'll give it a shot. ♥️

I'm not entirely sure how to set up the testing harness locally, so I'm going to (naively) push and hope the CI will catch any errors I make. Apologies in advance for any noise on this PR as I work things out. :)

@joshuacwnewton joshuacwnewton force-pushed the jn/9776-fix-discourse-js-schema branch 3 times, most recently from 786524c to 954866c Compare December 4, 2023 21:25
Previously, for e.g. 'topic', the call to the `DiscourseMetricIntegrationFactory` function supplied both 'topics' and 'topic_count'. And, we now need to check for 'topics_count' as well. To cover all three string variations, why not supply only 'topic' to the function, then selectively add the 's' on a case-by-case basis.

((Note: I've preserved the old metricName variable as to minimize the diff here and make my changes clearer.))
@joshuacwnewton
Copy link
Contributor Author

joshuacwnewton commented Dec 4, 2023

I'm not entirely sure if I've used the correct JS patterns/idioms, so please feel free to correct me if I've done something silly. 😅

Copy link
Contributor

github-actions bot commented Dec 6, 2023

🚀 Updated review app: https://pr-9778-badges-shields.fly.dev

Copy link
Member

@chris48s chris48s left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks 👍

@chris48s chris48s added this pull request to the merge queue Dec 6, 2023
Merged via the queue into badges:master with commit f6e40a6 Dec 6, 2023
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bugs in badges and the frontend service-badge New or updated service badge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discourse Status badge seems to be broken ("invalid response data")
2 participants