gpt4 book ai didi

javascript - 组合两个非常相似但又包含明显差异的函数

转载 作者:行者123 更新时间:2023-11-29 23:15:48 25 4
gpt4 key购买 nike

我的 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);
});
}
}

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

最佳答案

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

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}`},
})
);

让我知道这看起来如何。

关于javascript - 组合两个非常相似但又包含明显差异的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52843093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com