-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
Fix last updated time misleading, only show when file content change or otherwise when it first created #1023
Conversation
Deploy preview for docusaurus-preview ready! Built with commit b38dd21 |
or otherwise when it is first created Fix facebook#1015
f736f9c
to
26957af
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks awesome @fiennyangeln! I have some minor comments but we're good to land this after they're addressed.
const timeSpan = spawn | ||
.sync('git', ['log', '-1', '--format=%ct', filepath]) | ||
.stdout.toString('utf-8'); | ||
// To differentiate between content change and file renaming / moving, use --summary |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for only noticing this now, but we should add a try/catch around the code within getGitLastUpdated
and return null
in the catch
case something blows up (for instance, git is not installed on the device, or if the repository is a Mercurial-based one).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually im hoping we remove cross-spawn
dependency and use existing shelljs
to call the bash command.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@endiliey yeah i see some v2 code uses shell.exec()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
V1 too. Check v1/lib/publish-pages.js
v1/lib/core/utils.js
Outdated
@@ -38,10 +38,37 @@ function idx(target, keyPaths) { | |||
); | |||
} | |||
|
|||
function isNormalInteger(str) { | |||
return /^\+?(0|[1-9]\d*)$/.test(str); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use this regex instead: ^\d+$
, which is simpler. This regex means the line only contains integers. Check out https://regex101.com/r/mp3HYD/2/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be noted that above (fienny's PR) allow an optional +
at the beginning of string
Yangshun's recommended regex doesnt allow that.
If the optional +
is intended, use /^\+?\d+$/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if there is normal
and abnormal
integer from the function naming.
isInt
is fine o.o
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The numbers are timestamps, why do we need the +? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yangshun yup i dont think we need the +
v1/lib/core/utils.js
Outdated
.split('\n') | ||
.filter(String); | ||
|
||
const timeSpan = records.find( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks right, but could you test on a file that only has one commit?
Could you also test on files that have 0 commits, aka newly-created files that haven't been checked into Git yet. I suspect we this part might crash.
We don't want the page to blow up for any newly-created pages else they wouldn't be able to preview it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We currently have test that check for file with only one commit, non existing and those with no git timestamp in here v1/lib/core/__tests__/utils.test.js
v1/lib/core/utils.js
Outdated
.filter(String); | ||
|
||
const timeSpan = records.find( | ||
(item, index, arr) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rewrite this part as such to make the logic clearer:
const timeSpan = records.find((item, index, arr) => {
const isTimestamp = isNormalInteger(item);
const isLastItem = index + 2 <= arr.length; // Note: Let's use <= instead of === to be safer.
const nextItemIsTimestamp = isNormalInteger(arr[index + 1]);
return isTimestamp && (isLastItem || nextItemIsTimestamp);
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if we use <= it will return true for all element except the last
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See review
- Uses shelljs instead of cross-spawn - Make logic clearer
c10ef21
to
d0eab47
Compare
d0eab47
to
e3f3ca3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a quick chat with @endiliey and @yangshun, we probably don't want to execute git commands in directly within the unit tests.
@yangshun - I like the mocking idea. Could you provide a suggestion to @fiennyangeln on how to do that?
v1/lib/core/__tests__/utils.test.js
Outdated
const tempFilePath2 = path.join(__dirname, '__fixtures__', '.temp2'); | ||
fs.writeFileSync(tempFilePath2, 'Lorem ipsum :)'); | ||
|
||
shell.exec(`git add ${tempFilePath2}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't execute shell commands in tests. Use Jest mocking to mock out the entire shell
module and the methods that your method calls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay!!! Will try it :D
@JoelMarcey Have left some comments 😄 Thanks for your patience @fiennyangeln! |
I initially try to mock the whole shelljs. But it returns error shell.exec is not a function when i try to provide the mockResolvedValue I think it is because of the inner code of shelljs who run a forEach to require each of its method which make it a promise. I tried moving the jest.mock inside beforeAll and also adding babel-dynamic-import but it did not solve the problem. In the end, I decided to just mock shelljs.exec since it is the only function used anyway
I initially try to mock the whole shelljs. But it returns error |
@yangshun please review :D |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. @fiennyangeln Thanks for fixing! There are some small improvements that can be made but I'll address them 😄
Motivation
Fix #1015
Have you read the Contributing Guidelines on pull requests?
Yes
Test Plan
cd v1 && yarn start
), check in the siteRelated PRs
No