【问题标题】:Combine two very similar functions that contain distinct differences结合两个非常相似但包含明显差异的函数
【发布时间】:2018-10-16 19:51:51
【问题描述】:

我的 Node.js 应用程序中有两个函数:

retrieveIssues: function(githubAppId, pemFilePath, repoOrg, repoName, callback) {
  const octokit = require('@octokit/rest')();
  let data = null;
  gitInstallationAccessToken.genInstallationAccessToken(githubAppId, pemFilePath, (installationAccessToken) => {

  octokit.authenticate({
    type: 'app',
    token: `${installationAccessToken}`
  });

  async function paginate(method) {
    let response = await method({
      q: "repo:" + repoOrg + "/" + repoName + " is:issue" + " state:open",
    per_page: 100
    });
    data = response.data.items;
    var count = 0;
    while (octokit.hasNextPage(response)) {
      count++;
      console.log(`request n°${count}`);
      response = await octokit.getNextPage(response);
      data = data.concat(response.data.items);
    }
    return data;
  }

  paginate(octokit.search.issues)
    .then(data => {
      callback(data);
    })
    .catch(error => {
      console.log(error);
    });
  });
}

retrieveEnerpriseIssues: function(repoOrg, repoName, callback) { 
  const octokit = require('@octokit/rest')({
    baseUrl: config.githubEnterprise.baseUrl
  });
  let data = null;

  // token auth
  octokit.authenticate({
    type: 'basic',
    username: config.githubEnterprise.username,
    password: config.githubEnterprise.token
  });

  async function paginate(method) {
    let response = await method({
      q: "repo:" + repoOrg + "/" + repoName + " is:issue" + " label:sdk" + " state:open",
      per_page: 100
    });
    data = response.data.items;
    var count = 0;
    while (octokit.hasNextPage(response)) {
      count++;
      console.log(`request n°${count}`);
      response = await octokit.getNextPage(response);
      data = data.concat(response.data.items);
    }
    return data;
  }

  paginate(octokit.search.issues)
    .then(data => {
      callback(data);
    })
    .catch(error => {
      console.log(error);
    });
  }
}

第一个访问公共 GitHub,第二个访问私有 Github。虽然有一些非常明显的差异(身份验证类型和传递的参数数量等),但它们非常相似。我想知道是否可以将这些重构为单个函数,或者这是否是一个好主意。如果可能并且可以改进我的代码,这是如何完成的?

【问题讨论】:

  • 有什么问题?只需结合这些功能添加一些条件...
  • 在使用 Promise 和/或 async/await 时不要使用 callback 参数!只是return 那个paginate(octokit.search.issues);

标签: javascript node.js function refactoring


【解决方案1】:

您可以,并且考虑到重复的数量,可能应该重构。没有任何测试并且没有运行代码的能力有点棘手,但也许这可以解决问题?

retrieve: function({repoOrg, repoName, callback, octoKitArgs, octoKitAuthArgs}) {
  const octokit = require('@octokit/rest')(octoKitArgs);
  let data = null;

  octokit.authenticate(octoKitAuthArgs);

  async function paginate(method) {
    let response = await method({
      q: "repo:" + repoOrg + "/" + repoName + " is:issue" + " label:sdk" + " state:open",
      per_page: 100
    });
    data = response.data.items;
    var count = 0;
    while (octokit.hasNextPage(response)) {
      count++;
      console.log(`request n°${count}`);
      response = await octokit.getNextPage(response);
      data = data.concat(response.data.items);
    }
    return data;
  }

  paginate(octokit.search.issues)
    .then(data => {
      callback(data);
    })
    .catch(error => {
      console.log(error);
    });
}

// call as private github
retrieve({
  repoOrg: "",
  reportName: "",
  callback: () => {},
  octoKitArgs: {baseUrl: config.githubEnterprise.baseUrl},
  octoKitAuthArgs: {type: 'basic', username: config.githubEnterprise.username, password: config.githubEnterprise.token},
});

// call as public github
gitInstallationAccessToken.genInstallationAccessToken(githubAppId, pemFilePath, (installationAccessToken) =>
  retrieve({
    repoOrg: "",
    reportName: "",
    callback: () => {},
    octoKitArgs: undefined,
    octoKitAuthArgs: {type: 'app', token: `${installationAccessToken}`},
  })
);

让我知道这看起来如何。

【讨论】:

  • 非常接近。我可以看到retrieve函数中的return data实际上返回了数据。现在我需要处理它。现在看看企业调用我已经这样做了:``` ... config.githubEnterprise.username,密码:config.githubEnterprise.token},function(data,err){if(err){console.log( '错误:',错误); } else { console.log('data: ', data); } ``` 我没有看到任何错误或数据。我做错了吗?
  • 对不起,我不知道,就像我说我无法运行它,我只是编辑并发布它。如果您有任何测试或测试工具或其他东西,可能在 github gist 或其他东西中。 if 语句的作用是什么?它是否被传递到 octokit.authenticate 函数中?
  • 实际上,当我将callback: () => {}, 更改为callback: function(data, err) { if(err) { console.log('err: ', err); } else { console.log('data: ', data); } 时,它可以工作。我还将return data; 更改为callback(data)
  • 我更新了您的答案,以反映我如何更改您的代码以使其按我的需要工作。如果您认为我所做的有任何问题,或者我有什么可以改进的地方,我很乐意收到反馈。
  • 如果它对你有用,那就太好了,我很高兴能帮上忙。回调函数可以是你喜欢的任何东西,我把它们设置为空,这样你就可以看到需要什么,因为我不确定你想要回调做什么。干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
相关资源
最近更新 更多