#JSON を標準入力として受け取り #Github #API を叩いて 複数の Issue  を作成する #python スクリプトの例

- USER_NAME / API_KEY を環境変数で渡す
- JSONオブジェクトを持った配列を標準入力として渡す
- OWNER / REPOSITORY / タイトル / Description / ラベルを JSON で指定できるようにしておく

```py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# https://developer.github.com/v3/issues/#edit-an-issue
# https://gist.github.com/JeffPaine/3145490

import os, json, re, requests, sys

API_KEY = os.environ.get('API_KEY')
USER_NAME = os.environ.get('USER_NAME')

create_issues = json.loads(sys.stdin.read())

session = requests.Session()
session.auth = (USER_NAME, API_KEY)
 
results = []

for create_issue in create_issues:
  owner = os.environ.get('OWNER') if os.environ.get('OWNER') else create_issue['owner']
  repository = os.environ.get('REPOSTORY') if os.environ.get('REPOSTORY') else create_issue['repository']

  api_url = 'https://api.github.com/repos/%s/%s/issues' % (owner, repository)

  title = create_issue['title']
  body = create_issue['body']
  labels = create_issue['labels'] if 'labels' in create_issue and create_issue['labels'] else []

  create = {
    'title': title,
    'body': body,
    'labels': labels
  }

  res = session.post(api_url, json.dumps(create))

  results.append(res.json())

print(json.dumps(results))
 

```

# exe

```
echo '[{"owner":"YumaInaura", "repository": "playground", "title": "ye", "body": "some\\nwhat" },{"owner":"YumaInaura", "repository": "playground", "title": "waa", "body": "ohg\\nwhatyes" }]' | USER_NAME=YumaInaura API_KEY=YOUR_API_KEY ./create-issue.py | jq .
```

```json
[
  {
    "url": "https://api.github.com/repos/YumaInaura/playground/issues/96",
    "repository_url": "https://api.github.com/repos/YumaInaura/playground",
    "labels_url": "https://api.github.com/repos/YumaInaura/playground/issues/96/labels{/name}",
    "comments_url": "https://api.github.com/repos/YumaInaura/playground/issues/96/comments",
    "events_url": "https://api.github.com/repos/YumaInaura/playground/issues/96/events",
    "html_url": "https://github.com/YumaInaura/playground/issues/96",
    "id": 432358468,
    "node_id": "MDU6SXNzdWU0MzIzNTg0Njg=",
    "number": 96,
    "title": "ye",
    "user": {
      "login": "YumaInaura",
      "id": 13635059,
      "node_id": "MDQ6VXNlcjEzNjM1MDU5",
      "avatar_url": "https://avatars2.githubusercontent.com/u/13635059?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/YumaInaura",
      "html_url": "https://github.com/YumaInaura",
      "followers_url": "https://api.github.com/users/YumaInaura/followers",
      "following_url": "https://api.github.com/users/YumaInaura/following{/other_user}",
      "gists_url": "https://api.github.com/users/YumaInaura/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/YumaInaura/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/YumaInaura/subscriptions",
      "organizations_url": "https://api.github.com/users/YumaInaura/orgs",
      "repos_url": "https://api.github.com/users/YumaInaura/repos",
      "events_url": "https://api.github.com/users/YumaInaura/events{/privacy}",
      "received_events_url": "https://api.github.com/users/YumaInaura/received_events",
      "type": "User",
      "site_admin": false
    },
    "labels": [],
    "state": "open",
    "locked": false,
    "assignee": null,
    "assignees": [],
    "milestone": null,
    "comments": 0,
    "created_at": "2019-04-12T02:25:02Z",
    "updated_at": "2019-04-12T02:25:02Z",
    "closed_at": null,
    "author_association": "OWNER",
    "body": "some\nwhat",
    "closed_by": null
  },
  {
    "url": "https://api.github.com/repos/YumaInaura/playground/issues/97",
    "repository_url": "https://api.github.com/repos/YumaInaura/playground",
    "labels_url": "https://api.github.com/repos/YumaInaura/playground/issues/97/labels{/name}",
    "comments_url": "https://api.github.com/repos/YumaInaura/playground/issues/97/comments",
    "events_url": "https://api.github.com/repos/YumaInaura/playground/issues/97/events",
    "html_url": "https://github.com/YumaInaura/playground/issues/97",
    "id": 432358471,
    "node_id": "MDU6SXNzdWU0MzIzNTg0NzE=",
    "number": 97,
    "title": "waa",
    "user": {
      "login": "YumaInaura",
      "id": 13635059,
      "node_id": "MDQ6VXNlcjEzNjM1MDU5",
      "avatar_url": "https://avatars2.githubusercontent.com/u/13635059?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/YumaInaura",
      "html_url": "https://github.com/YumaInaura",
      "followers_url": "https://api.github.com/users/YumaInaura/followers",
      "following_url": "https://api.github.com/users/YumaInaura/following{/other_user}",
      "gists_url": "https://api.github.com/users/YumaInaura/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/YumaInaura/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/YumaInaura/subscriptions",
      "organizations_url": "https://api.github.com/users/YumaInaura/orgs",
      "repos_url": "https://api.github.com/users/YumaInaura/repos",
      "events_url": "https://api.github.com/users/YumaInaura/events{/privacy}",
      "received_events_url": "https://api.github.com/users/YumaInaura/received_events",
      "type": "User",
      "site_admin": false
    },
    "labels": [],
    "state": "open",
    "locked": false,
    "assignee": null,
    "assignees": [],
    "milestone": null,
    "comments": 0,
    "created_at": "2019-04-12T02:25:03Z",
    "updated_at": "2019-04-12T02:25:03Z",
    "closed_at": null,
    "author_association": "OWNER",
    "body": "ohg\nwhatyes",
    "closed_by": null
  }
]
```

 # Image


![image](https://user-images.githubusercontent.com/13635059/56008040-b8f0dc80-5d15-11e9-9368-1faf1c489e8d.png)
---
![image](https://user-images.githubusercontent.com/13635059/56008038-b7bfaf80-5d15-11e9-888b-a92db85879d4.png)



# Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/1206








<!-- Update From Qiita API -->

# チャットメンバー募集


何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->