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

Continue request --> got migration; affects [travis wordpress myget jenkins-plugin node nuget] #7215

Merged
merged 2 commits into from
Nov 3, 2021

Conversation

chris48s
Copy link
Member

@chris48s chris48s commented Nov 2, 2021

Closes #7214
Refs #4655

This PR migrates regularUpdate() from request and callbacks to got and async/await and all the knock-on effects of that.

@chris48s chris48s added service-badge New or updated service badge core Server, BaseService, GitHub auth, Shared helpers dependencies Related to dependency updates labels Nov 2, 2021
@shields-ci
Copy link

shields-ci commented Nov 2, 2021

Warnings
⚠️ This PR modified service code for jenkins 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 6cd6d3f

})
)
}
const { buffer } = await checkErrorResponse({})(
Copy link
Member Author

Choose a reason for hiding this comment

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

I decided to replace the bespoke error handling here with just checkErrorResponse(). I don't think it was really buying us anything

Copy link
Member

Choose a reason for hiding this comment

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

i think that's reasonable, assuming we don't have a large bucket somewhere of these intermediate errors that are providing helpful context (I don't think I've ever seen one)

@@ -1,93 +1,62 @@
import requestModule from 'request'
Copy link
Member Author

Choose a reason for hiding this comment

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

This file is the last file left in core/legacy/ and it has no unit tests although it is not completely untested because the service tests for all the services in the title exercise this code.

My instinct is that having migrated this to got and async/await we should move this out of core/legacy/ and write a proper test suite for it. Before I go ahead with that, it would be useful to understand why this is in legacy. Maybe @paulmelnikow can chime in on this one. Does this sufficiently modernise it, or is there another reason we want to ditch this?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to go ahead and merge this so we can get #7214 resolved, but it would be useful to get some input on this anyway and I will follow up

@shields-cd shields-cd temporarily deployed to shields-staging-pr-7215 November 2, 2021 20:19 Inactive
Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

couple small questions, otherwise lgtm 👍

regularUpdateCache[url] = { timestamp, data }
cb(null, data)
})
const data = scraper(reqData)
Copy link
Member

Choose a reason for hiding this comment

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

it's a small diff but admittedly my eyes are having difficulty following it, though curious if there's any potential behavioral concerns since the scraper call site is no longer happening within a try block?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this took me a while to unpick too.

You'll notice that the old regularUpdate function takes a callback function as a param (cb) in the sig, but when we're invoking regularUpdate() we are not actually passing it a callback. This is because we're wrapping it in promisify() from the standard library. One of the things this does is it passes our function a callback that rejects the promise if we pass it an error

So

function regularUpdate(params, cb) {
  try {
    // do a thing
  } catch (e) {
    cb(e)
    return
  }
}

is there to catch any exception we throw and pass it into the callback function, but its not actually handling it pre-se - its just allowing us to catch it when we resolve the promise (maybe "bubbling" it is the right word for this?). i.e: it allows us to do:

try:
  await promisify(regularUpdate)(params)
} catch (e) {
  // either handle the error cleanly, or ignore it and leave it sentry
}

Now that regularUpdate is an async function, we can safely just throw in an async function, so

function regularUpdate(params, cb) {
  try {
    // do a thing
  } catch (e) {
    cb(e)
    return
  }
}

in callback-land can just be replaced with

async function regularUpdate(params) {
  // do a thing
}

in async/await-land because:

try:
  await regularUpdate(params)
} catch (e) {
  // either handle the error cleanly, or ignore it and leave it sentry
}

is already a thing.

Copy link
Member

Choose a reason for hiding this comment

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

got it, thanks for the explanation!

})
)
}
const { buffer } = await checkErrorResponse({})(
Copy link
Member

Choose a reason for hiding this comment

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

i think that's reasonable, assuming we don't have a large bucket somewhere of these intermediate errors that are providing helpful context (I don't think I've ever seen one)

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 dependencies Related to dependency updates service-badge New or updated service badge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: githubApiProvider.request is not a function
4 participants