function()

in scripts/github-pr-title-lint.js [106:169]


      function (response) {
        if (response.statusCode < 200 || response.statusCode > 299) {
          reject(
            new Error(
              `getPullRequestTitle got an unexpected statusCode: ${response.statusCode}`,
            ),
          );
          return;
        }

        response.setEncoding('utf8');

        var body = '';
        response.on('data', function (data) {
          try {
            body += data;

            if (VERBOSE === 'true') {
              console.log('DEBUG getPullRequestTitle got data:', String(data));
            }

            // Once we get the closing title tag, we can read the pull request title and
            // close the http request.
            if (body.includes('</title>')) {
              response.removeAllListeners('data');
              response.emit('end');

              var titleStart = body.indexOf('<title>');
              var titleEnd = body.indexOf('</title>');

              // NOTE: page slice is going to be something like:
              // "<title> PR title by author · Pull Request #NUM · mozilla/web-ext · GitHub"
              var pageTitleParts = body
                .slice(titleStart, titleEnd)
                .replace('<title>', '')
                .split(' · ');

              // Check that we have really got the title of a real pull request.
              var expectedPart1 = `Pull Request #${pullRequestNumber}`;

              if (pageTitleParts[1] === expectedPart1) {
                // Remove the "by author" part.
                var prTitleEnd = pageTitleParts[0].lastIndexOf(' by ');
                resolve(pageTitleParts[0].slice(0, prTitleEnd));
              } else {
                if (VERBOSE === 'true') {
                  console.log('DEBUG getPullRequestTitle response:', body);
                }

                reject(new Error('Unable to retrieve the pull request title'));
              }

              req.abort();
            }
          } catch (err) {
            reject(err);
            req.abort();
          }
        });
        response.on('error', function (err) {
          console.error('Failed during pull request title download: ', err);
          reject(err);
        });
      },