【问题标题】:Filter data obtained through GitHub API过滤通过 GitHub API 获取的数据
【发布时间】:2018-09-13 17:29:50
【问题描述】:

我创建了这个函数来获取 GitHub 问题:

retrieveEnerpriseIssues: function(repoOrg, repoName, callback) { 
  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",
      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);
    });
  }

在这个函数中调用它来处理问题,将所有不需要的键过滤成 json 格式并将其放入我的数据库中。

extractToDb: function() {
  let gitIssues = null;
  for(var i = 0; i < config.githubEnterprise.orgs.length; i++) {
    for(var j = 0; j < config.githubEnterprise.orgs[i].repos.length; j++) {
      gitHubService.retrieveEnerpriseIssues(
        config.githubEnterprise.orgs[i].owner,
        config.githubEnterprise.orgs[i].repos[j].repoName,
        function(data, err) {
          if(err) {
            console.log('err: ', err);
          } else {
            gitIssues = data;
          }
          gitIssues = JSON.stringify(gitIssues);
          gitIssues = JSON.parse(gitIssues);
          let issueFormatForDb = null;
          for(var i = 0; i < gitIssues.length; i++) {
            issueFormatForDb = gitIssues[i];
            const body = '{' +
              '"github_id": "' + issueFormatForDb.id + '",' +
              '"issue_title": "' + issueFormatForDb.title + '",' +
              '"issue_number": "' + issueFormatForDb.number + '",' +
              '"issue_url": "' + issueFormatForDb.url + '",' +
              '"issue_state": "' + issueFormatForDb.state + '"' +  
          '}';
          console.log('Body: ', body);
          getGitHubIssues.postToDb(body);
        }
      });
    }
  }
}

我想更进一步,过滤掉任何关闭状态的问题。这是如何完成的,应该在我的retrieveEnerpriseIssues 函数或我的extractToDb 中处理?

可能的解决方案 我在我的extractToDb 函数中尝试了这个:

gitIssues = JSON.parse(gitIssues);
gitIssues = _.where(gitIssues, {state: "open"});
let issueFormatForDb = null;

这是最好的解决方案还是有更好的方法?

【问题讨论】:

  • 最好使用 _.filter,或者像 gitIssues = gitIssues.filter(i =&gt; i.state === 'open') 这样的原生过滤方法,我认为 .where 在更高版本的 lodash github.com/lodash/lodash/wiki/Deprecations 中已被弃用。除此之外,它非常好。

标签: javascript node.js rest filter github-api


【解决方案1】:

正如@givehug 所说:

最好使用_.filter,或者像原生过滤器方法

gitIssues = gitIssues.filter(i => i.state === 'open')

我认为 .where 在更高版本的 lodash github.com/lodash/lodash/wiki/Deprecations 中已被弃用。除此之外,它完全没问题。

我刚刚意识到我可以用这个过滤paginate 函数中的状态:

let response = await method({
    q: "repo:" + repoOrg + "/" + repoName + " is:issue" + " label:issue_label" + " state:open",
    per_page: 100
  });

【讨论】:

    猜你喜欢
    • 2017-06-12
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多