【问题标题】:github issues api and jq filter and grepgithub 发布 api 和 jq 过滤器和 grep
【发布时间】:2018-02-07 09:00:28
【问题描述】:

我正在使用 GitHub 企业 API 来获取我的一个存储库中的问题列表。 但是,我需要过滤输出,以便获得自上周以来创建的问题列表。

下面是我到目前为止到达的地方并且工作正常。我需要帮助来过滤自上周以来创建的问题。

#!/bin/sh
tot_page=`curl -k -I https://<my_pat>:x-oauth-basic@github.developer.blah.com/api/v3/repos/droppin/remotedev/issues?state=all\&per_page=10 2>/dev/null |  awk -F'[&=<>]' '{for(i=1;i<=NF;i++) if($i ~ /^page$/) {kk=$(i+1)}} END{print kk}'`;
echo "$tot_page"
for ((i=1;i<=tot_page;i++))
do
        curl -k https://<my_pat>:x-oauth-basic@github.developer.blah.com/api/v3/repos/droppin/remotedev/issues?state=all\&per_page=2\&page=$i 2>/dev/null | 
        jq -r '.[] | 
          "\(.number)" + "\t\t" + "\(.assignee.login)" 
           + "\t\t\t" +  "\(.state)" + "\t\t" 
           + "\(.created_at) | date("'
done

感谢您对此的帮助。谢谢。

根据要求提供示例 JSON 输出。

[
      {
    "url": "https://api.github.com/repos/stedolan/jq/issues/1558",
    "repository_url": "https://api.github.com/repos/stedolan/jq",
    "labels_url": "https://api.github.com/repos/stedolan/jq/issues/1558/labels{/name}",
    "comments_url": "https://api.github.com/repos/stedolan/jq/issues/1558/comments",
    "events_url": "https://api.github.com/repos/stedolan/jq/issues/1558/events",
    "html_url": "https://github.com/stedolan/jq/pull/1558",
    "id": 282346710,
    "number": 1558,
    "title": "Fix a try-catch example",
    "user": {
      "login": "zogwarg",
      "id": 6682272,
      "avatar_url": "https://avatars3.githubusercontent.com/u/6682272?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/zogwarg",
      "html_url": "https://github.com/zogwarg",
      "followers_url": "https://api.github.com/users/zogwarg/followers",
      "following_url": "https://api.github.com/users/zogwarg/following{/other_user}",
      "gists_url": "https://api.github.com/users/zogwarg/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/zogwarg/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/zogwarg/subscriptions",
      "organizations_url": "https://api.github.com/users/zogwarg/orgs",
      "repos_url": "https://api.github.com/users/zogwarg/repos",
      "events_url": "https://api.github.com/users/zogwarg/events{/privacy}",
      "received_events_url": "https://api.github.com/users/zogwarg/received_events",
      "type": "User",
      "site_admin": false
    },
    "labels": [

    ],
    "state": "open",
    "locked": false,
    "assignee": null,
    "assignees": [

    ],
    "milestone": null,
    "comments": 0,
    "created_at": "2017-12-15T07:56:31Z",
    "updated_at": "2017-12-15T07:56:31Z",
    "closed_at": null,
    "author_association": "NONE",
    "pull_request": {
      "url": "https://api.github.com/repos/stedolan/jq/pulls/1558",
      "html_url": "https://github.com/stedolan/jq/pull/1558",
      "diff_url": "https://github.com/stedolan/jq/pull/1558.diff",
      "patch_url": "https://github.com/stedolan/jq/pull/1558.patch"
    },
    "body": "Fixing a bad syntax in a try-catch example\r\n\r\nIt was probably based on the inputs builtin definition,\r\nThe if statement was messed up and the semi-colon actually belongs to the function definition."
  },
  {
    "url": "https://api.github.com/repos/stedolan/jq/issues/1557",
    "repository_url": "https://api.github.com/repos/stedolan/jq",
    "labels_url": "https://api.github.com/repos/stedolan/jq/issues/1557/labels{/name}",
    "comments_url": "https://api.github.com/repos/stedolan/jq/issues/1557/comments",
    "events_url": "https://api.github.com/repos/stedolan/jq/issues/1557/events",
    "html_url": "https://github.com/stedolan/jq/issues/1557",
    "id": 282289185,
    "number": 1557,
    "title": "Compiling for tvOS",
    "user": {
      "login": "larryaasen",
      "id": 325973,
      "avatar_url": "https://avatars1.githubusercontent.com/u/325973?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/larryaasen",
      "html_url": "https://github.com/larryaasen",
      "followers_url": "https://api.github.com/users/larryaasen/followers",
      "following_url": "https://api.github.com/users/larryaasen/following{/other_user}",
      "gists_url": "https://api.github.com/users/larryaasen/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/larryaasen/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/larryaasen/subscriptions",
      "organizations_url": "https://api.github.com/users/larryaasen/orgs",
      "repos_url": "https://api.github.com/users/larryaasen/repos",
      "events_url": "https://api.github.com/users/larryaasen/events{/privacy}",
      "received_events_url": "https://api.github.com/users/larryaasen/received_events",
      "type": "User",
      "site_admin": false
    },
    "labels": [

    ],
    "state": "open",
    "locked": false,
    "assignee": null,
    "assignees": [

    ],
    "milestone": null,
    "comments": 0,
    "created_at": "2017-12-15T01:05:47Z",
    "updated_at": "2017-12-15T01:05:47Z",
    "closed_at": null,
    "author_association": "CONTRIBUTOR",
    "body": "I attempted to update the compile-ios.sh file for tvOS to build the library for use in an Apple TV app. I am not able to get it to link. Has anyone been able to compile the library for tvOS?\r\n\r\nHere is the error message:\r\n/opt/local/bin/ranlib: object: .libs/libonig.a(regerror.o) malformed object (unknown load command 1)\r\n\r\nAttached the script file:\r\n[compile-tvos.sh.zip](https://github.com/stedolan/jq/files/1561265/compile-tvos.sh.zip)\r\n"
  }
]

【问题讨论】:

  • 请提供最小的 JSON 样本。
  • 嗨@peak,与几个实体共享示例 JSON..
  • @pprmdas,好的,发布预期结果
  • 预期输出应该是 github 问题列表以及上周创建的 assignees

标签: linux date time github-api jq


【解决方案1】:

无耻的自我广告。你可以用 jf 巧妙地做到这一点

pip install jf
curl "https://api.github.com/repos/alhoo/jf/issues" | jf 'filter(age(x.created_at) < age("1 week"))'

【讨论】:

    【解决方案2】:

    使用 jq:

    # Input should be a date of the form: 2017-12-15T01:05:47
    def older(days):
      (now - (strptime("%Y-%m-%dT%T") | mktime)) #  seconds 
      >  (days*24*3600);
    
    .[]
    | (.created_at | sub("Z.*";"")) as $date 
    | select($date | older(7) | not)
    | [.number, .user.login, .state, .created_at]
    | @tsv
    

    上述内容可能需要在访问适当的受让人信息方面进行调整。

    【讨论】:

    • 我能得到linux的命令吗?我猜def是python的,我不知道python..
    • 调用可能像:jq -r -f program.jq
    猜你喜欢
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    相关资源
    最近更新 更多