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

Add renderDateBadge helper; affects [aur BitbucketLastCommit chrome date eclipse factorio galaxytoolshed GiteaLastCommit GistLastCommit GithubCreatedAt GithubHacktoberfest GithubIssueDetail GithubLastCommit GithubReleaseDate GitlabLastCommit maven npm openvsx snapcraft SourceforgeLastCommit steam vaadin visualstudio wordpress] #10682

Merged
merged 5 commits into from
Nov 17, 2024

Conversation

chris48s
Copy link
Member

There's a couple of motivations behind this PR.

1. Validation

Dayjs is quite flexible in the inputs it accepts, but it also allows you to construct invalid date objects. So for example dayjs('not-a-date') doesn't throw, but also doesn't yield a useful date object. I don't love this behaviour tbh, but dayjs was the easiest thing to migrate to following the demise of moment.js and I don't really have the appetite to replace it.

The way round this is: If we aren't able to validate our input sufficiently before constructing a date object, we need to then check the date is valid before we do anything with it.

So really what we want to be doing is:

import dayjs from 'dayjs'

const date = dayjs(data['last-updated'])

if (!date.isValid()) {
  throw new InvalidResponse({ prettyMessage: 'invalid date' })
}

// do stuff with date

The problem is: This is a bit cumbersome, and sometimes we do it wrong. For example, there are places in the codebase where we are not checking .isValid() at all and there are some places where we are (incorrectly) checking .isValid instead of .isValid() e.g:

https://github.com/badges/shields/blob/master/services/npm/npm-last-update.service.js#L84-L88

Whoops! Anyway, we're not really setting ourselves up for success here.

So I wanted to write the correct validation once in a helper function, and then use it everywhere.

2. Reduce duplication

There are lots and lots of places where we are writing some slight variant of:

static render(date) {
  const color = age(date)
  const message = formatDate(date)
  return { color, message }
}

in service classes. There is scope for us to write one helper function, use it in lots of places, and clean up a couple of inconsistencies.


To be honest, I've done a bit too much stuff all in one commit here, so this is probably a bit of a pain to review. Ideally I would have split out some of the "moving stuff from one file to another" work into separate commits. On this occasion, that's just how it went down. Sorry :(

Fundamentally, the new code in this PR is parseDate and renderDateBadge. age, formatDate and formatRelativeDate (plus their tests) are just moving from one file to another with no significant modifications. Then everything else is just updating services to use parseDate and renderDateBadge.

@chris48s chris48s added the core Server, BaseService, GitHub auth, Shared helpers label Nov 15, 2024
Copy link
Contributor

github-actions bot commented Nov 15, 2024

Warnings
⚠️ This PR modified service code for aur but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for bitbucket but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for chrome-web-store but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for date but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for eclipse-marketplace but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for factorio-mod-portal but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for galaxytoolshed but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for gitea but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for github/gist but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for github but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for gitlab but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for maven-central but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for npm but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for open-vsx but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for snapcraft but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for sourceforge but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for steam but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for vaadin-directory but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for visual-studio-marketplace but not its test code.
That's okay so long as it's refactoring existing code.
⚠️ This PR modified service code for wordpress but not its test code.
That's okay so long as it's refactoring existing code.
Messages
📖 ✨ Thanks for your contribution to Shields, @chris48s!

Generated by 🚫 dangerJS against 9ce8948

it is going to be unlikely we'll invoke either of these
directly now, but lets calidate here too
@jNullj jNullj self-assigned this Nov 16, 2024
Copy link
Contributor

@jNullj jNullj left a comment

Choose a reason for hiding this comment

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

I double checked all services relevant are present and I think the PR covers all revelant usage for the refactor.
I have 2 comments in the review.

services/text-formatters.js Outdated Show resolved Hide resolved
services/galaxytoolshed/galaxytoolshed-activity.service.js Outdated Show resolved Hide resolved
@chris48s
Copy link
Member Author

Thanks - both useful comments 👍

Copy link
Contributor

@jNullj jNullj left a comment

Choose a reason for hiding this comment

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

LGTM, run in docker and tried a few of the services as a sanity test in addition to the review/CI.

@chris48s chris48s added this pull request to the merge queue Nov 17, 2024
Merged via the queue into master with commit 5cdef88 Nov 17, 2024
38 checks passed
@chris48s chris48s deleted the render-date-helper20241115 branch November 17, 2024 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Server, BaseService, GitHub auth, Shared helpers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants