🖥

#Github # Update API using API or delete only a specific label # pytho

2019/04/12に公開

<ul>
<li> Specification to receive editing information on json standard input (array of objects) </li>
<li> The action to delete a specific label is to obtain existing issue information, process the array of labels in a python script, and pass it to the edit API. </li>
</ul>

<h1> python </h1>

<pre> <code class="py">#!/usr/bin/env python3

-- coding: utf-8 --

import os, json, re, requests, sys, funcy

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

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

session = requests.Session()
session.auth = (USER_NAME, API_KEY)

print(edits)

def get_issue(issue):
issue_api_url = 'https://api.github.com/repos/' + issue['owner'] + '/' + issue['repository'] + '/issues/' + str(issue['number'])
res = requests.get(issue_api_url)
return(res.json())

results = []

for edit in edits:
issue_number = str(edit['number'])
owner = edit['owner']
repository = edit['repository']

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

update = {}

if 'title' in edit:
update['title'] = edit['title']

if 'body' in edit:
update['body'] = edit['body']

if 'labels' in edit:
update['labels'] = edit['labels']

if 'remove_labels' in edit:
issue = get_issue(edit)
label_names = list(funcy.pluck('name', issue['labels']))

update['labels'] = list(set(label_names) - set(edit['remove_labels']))

res = session.post(edit_api_url, json.dumps(update))

results.append(res.json())

print(json.dumps(results))

</code> </pre>

<h1> Specify and edit labels </h1>

<pre> <code>$ echo '[{ "number": 97, "owner": "YumaInaura", "repository": "playground", "labels": ["label1","label2","label3"] }]' | USER_NAME=YumaInaura API_KEY=$(cat ~/.secret/github-api-key) ./edit-issue.py

[{'number': 97, 'owner': 'YumaInaura', 'repository': 'playground', 'labels': ['label1', 'label2', 'label3']}]
[{"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": "x", "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": [{"id": 1315925096, "node_id": "MDU6TGFiZWwxMzE1OTI1MDk2", "url": "https://api.github.com/repos/YumaInaura/playground/labels/label1", "name": "label1", "color": "ededed", "default": false}, {"id": 1315925097, "node_id": "MDU6TGFiZWwxMzE1OTI1MDk3", "url": "https://api.github.com/repos/YumaInaura/playground/labels/label2", "name": "label2", "color": "ededed", "default": false}, {"id": 1315925098, "node_id": "MDU6TGFiZWwxMzE1OTI1MDk4", "url": "https://api.github.com/repos/YumaInaura/playground/labels/label3", "name": "label3", "color": "ededed", "default": false}], "state": "open", "locked": false, "assignee": null, "assignees": [], "milestone": null, "comments": 0, "created_at": "2019-04-12T02:25:03Z", "updated_at": "2019-04-12T08:38:30Z", "closed_at": null, "author_association": "OWNER", "body": "ohg\nwhatyes", "closed_by": null}]
</code> </pre>

<h1> Delete a specific label </h1>

<pre> <code>$ echo '[{ "number": 97, "owner": "YumaInaura", "repository": "playground", "remove_labels": ["label2"] }]' | USER_NAME=YumaInaura API_KEY=$(cat ~/.secret/github-api-key) ./edit-issue.py

[{'number': 97, 'owner': 'YumaInaura', 'repository': 'playground', 'remove_labels': ['label2']}]
[{"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": "x", "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": [{"id": 1315925096, "node_id": "MDU6TGFiZWwxMzE1OTI1MDk2", "url": "https://api.github.com/repos/YumaInaura/playground/labels/label1", "name": "label1", "color": "ededed", "default": false}, {"id": 1315925098, "node_id": "MDU6TGFiZWwxMzE1OTI1MDk4", "url": "https://api.github.com/repos/YumaInaura/playground/labels/label3", "name": "label3", "color": "ededed", "default": false}], "state": "open", "locked": false, "assignee": null, "assignees": [], "milestone": null, "comments": 0, "created_at": "2019-04-12T02:25:03Z", "updated_at": "2019-04-12T08:38:37Z", "closed_at": null, "author_association": "OWNER", "body": "ohg\nwhatyes", "closed_by": null}]

</code> </pre>

<h1> Edit the title </h1>

<pre> <code>$ echo '[{ "number": 97, "owner": "YumaInaura", "repository": "playground", "title" : "AS" }]' | USER_NAME=YumaInaura API_KEY=$(cat ~/.secret/github-api-key) ./edit-issue.py
[{'number': 97, 'owner': 'YumaInaura', 'repository': 'playground', 'title': 'AS'}]
[{"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": "AS", "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": [{"id": 1315925096, "node_id": "MDU6TGFiZWwxMzE1OTI1MDk2", "url": "https://api.github.com/repos/YumaInaura/playground/labels/label1", "name": "label1", "color": "ededed", "default": false}, {"id": 1315925098, "node_id": "MDU6TGFiZWwxMzE1OTI1MDk4", "url": "https://api.github.com/repos/YumaInaura/playground/labels/label3", "name": "label3", "color": "ededed", "default": false}], "state": "open", "locked": false, "assignee": null, "assignees": [], "milestone": null, "comments": 0, "created_at": "2019-04-12T02:25:03Z", "updated_at": "2019-04-12T08:43:41Z", "closed_at": null, "author_association": "OWNER", "body": "ohg\nwhatyes", "closed_by": null}]
</code> </pre>

Original by Github issue

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

チャットメンバー募集

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

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

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-12

Discussion