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

Is it possible to do async function (e.g., get data from APIs) within custom Handlebars extension? #876

Closed
LouisSung opened this issue Nov 3, 2020 · 3 comments

Comments

@LouisSung
Copy link
Contributor

Hello, I am a beginner to Azure DevOps and extensions and our team is using XplatGenerateReleaseNotes:v3.x to generate reports.

After reading the documentation, I realize that some custom Handlebars extension could be implemented by exporting functions in YAML.
However, I wonder if it's possible to do some advanced operations such as fetching external data and use them in the report?

I've gave it a simple try but seems like Handlebars doesn't support asynchronous call.
I wonder if my code could be modify to support such a feature or it's just not possible for this extension?


The code looks like:

customHandlebarsExtensionCode: |
    module.exports = {
    async foo() {
        const https = require('https');

        function getRequest(options) {
          return new Promise ((resolve, reject) => {
            let req = https.request(options);
            req.on('response', res => { resolve(res); });
            req.on('error', err => { reject(err); });
          }); 
        }

        return await getRequest({
          hostname: 'api.github.com',
          port: 443,
          path: '/users/octocat',
          method: 'GET'
        });
    }
};
Data from RESTful API: {{foo}}

And the log:

2020-11-03T02:57:42.5744402Z (node:87037) UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT 18.179.245.253:443
2020-11-03T02:57:42.5745830Z     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)
2020-11-03T02:57:42.5747028Z (node:87037) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
2020-11-03T02:57:42.5748700Z (node:87037) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
@rfennell
Copy link
Owner

rfennell commented Nov 3, 2020

This is something I have not tried, I don't do that much Node//Handlebars development beyond this task.

I would have to do some tests in my command line test harness. I will look but not sure when I will have the time

@rfennell
Copy link
Owner

rfennell commented Nov 4, 2020

I have done some research and you are correct that out the box (as I have used) Handlebars does not allow async custom extensions.

To make this work I would have to make use one of the methods listed here. My concern is that this is an old thread and I have not found anything newer.

Given the lack of a definitive solution, I have no plans to alter my current implementation to support async extensions.

@LouisSung
Copy link
Contributor Author

LouisSung commented Nov 4, 2020

OK~~
Actually I've traced the implementation of loading custom Handlebars extensions yesterday and realized that it would be hard to make some changes since it's an issue of Handlebars itself.

Also, it's important to keep the sustainability of a long-term supported extension like yours.
Therefore, we'll try to create an Azure DevOps extension our own for the preprocessing (e.g., get data from APIs) and see if these data could be read or loaded later by your extension :D

I would like to thank you again for the quick reply and spending time on surveying this issue.
Hope this discussion could also help those who have the same needs :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants