funderburkjim / testing

For testing various features of github. Nothing important here.
0 stars 0 forks source link

A test issue for writing issue comments #28

Open funderburkjim opened 7 years ago

funderburkjim commented 7 years ago

This is a test issue.

funderburkjim commented 7 years ago

Debug comment posted with curl

funderburkjim commented 7 years ago

Here is the curl command that posted the prior comment to this issue:

curl -H "Authorization: token OATH-TOKEN" https://api.github.com/repos/funderburkjim/testing/issues/28/comments --request POST --data '{"body": "Debug comment posted with curl"}' 

The OATH-TOKEN must be replaced by a 'personal access token' which you obtain by visiting the url https://github.com/settings/tokens and clicking the 'Generate new token' button.

setting of scopes for the OATH-TOKEN

When you first generate a PAT (personal access token), there are certain privileges associated with that token; GitHub calls these privileges 'scopes'. You can review and alter the privileges by clicking the 'Edit' button.

The 'default' privileges are shown as admin:repo_hook, which seems to encompass two actual privileges called write:repo-hook and read:repo-hook.

While these default privileges allow certain GitHub API actions (exactly which actions is not clear), they do NOT suffice for the action of writing comments to an issue.

To post comments to an issue, one more scope is required: 'public-repo'. Setting this requires editing the token, and then clicking the 'Update Token' button.

Here is a screen shot of the Edit section that shows these three scopes (and no others)

image

funderburkjim commented 7 years ago

A comment posted with Python requests module.

funderburkjim commented 7 years ago

Using Python requests module to post issue comments

Here is a small Python program that posted the previous comment. Again, OATH-TOKEN must be replaced by a real PAT, as described above.

import requests,json

url = "https://api.github.com/repos/funderburkjim/testing/issues/28/comments"
headers = {'Authorization':'token OATH-TOKEN'}

payload = {"body": "A comment posted with Python requests module."}
r = requests.post(url,headers=headers,data=json.dumps(payload))
print r.status_code

The printed request status code is '201' -- this is the code that means success.

rate limiting

Experience has shown that there is a limit to the number of comments that can be posted in a short time period. Suppose you want to post 50 comments in your Python program. The obvious way to do this is to have your comments in a list, and do the posting in a loop:

for icomment,body in enumerate(comments):
 payload = {"body": "A comment posted with Python requests module."}
 r = requests.post(url,headers=headers,data=json.dumps(payload))
 if r.status_code == 201:
  print "posted comment #",icomment+1
 else:
  print "failed when posting comment #",icomment+1
  break

It is unlikely that all comments will be posted successfully. Thus, you need to have your program rearranged, so that it can be run several times, each with a different range of comments .