PyGithub icon indicating copy to clipboard operation
PyGithub copied to clipboard

PyGithub example usage

Open sfdye opened this issue 7 years ago • 24 comments

Motivation: #862 #456 #321 #584 #762

PyGithub is a great library, however it may not be intuitive to use at the beginning. Not everyone is a Github API expert, so I am trying to provide some working examples (recipes) that will hopefully help you get started.

The skeleton has been setup here and the example docs is already live on readthedocs as usual:

https://pygithub.readthedocs.io/en/latest/examples.html

Note that the list will expand and any PR to add more examples is extremely welcome 😄

If you want to see any examples not covered here, just leave a comment.

sfdye avatar Aug 21 '18 08:08 sfdye

@sfdye readd the login example please ^^_

Abdur-rahmaanJ avatar Aug 21 '18 09:08 Abdur-rahmaanJ

I just started with PyGithub, and found some examples, so obviously some work has been done—thanks, people!

But I don't see any examples for how to listen for events, e.g. pushevent and pullrequestevent. Could examples for those be added?

mscheper avatar Oct 11 '18 18:10 mscheper

Thanks, @LeviMatus!

mscheper avatar Oct 15 '18 18:10 mscheper

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 14 '18 19:12 stale[bot]

this is not stale

sfdye avatar Dec 20 '18 02:12 sfdye

I am Writing Organization examples now

goodship1 avatar Feb 02 '19 22:02 goodship1

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 03 '19 22:04 stale[bot]

I am new to PyGithub SDK and trying to do below operations. However, having difficulty going through documents to know the usage.

  1. get a file content from a specific branch

command line equivalent:

git checkout <<branch>> and then get file content

Issue:

I am able to get file content from master branch using this sdk but not from specific branch

  1. repo tags and its details

command line equivalent:

git tag -v <<tag_id>>

  1. Update that file

I see here the docs https://pygithub.readthedocs.io/en/latest/examples/Repository.html#update-a-file-in-the-repository

  1. Update user details for commit
git config user.email "[email protected]"
git config user.name "cicd"
git commit --amend --reset-author --no-edit
  1. Git pull that branch before commit as other automation might have committed some other file.
  2. git commit in the branch
git pull --no-edit
git add $FILE_NAME
git commit -m "updating $FILE_NAME"
git pull --no-edit && git push origin

I am also trying to find the method/api of this sdk to do that, but if someone have it ready, please help with the same.

yks0000 avatar May 28 '19 13:05 yks0000

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 27 '19 14:07 stale[bot]

Hello, I'm a relatively new developer and this is helping a lot. Is any one planning to do examples for github app? Thanks!

mancuss avatar Oct 29 '19 10:10 mancuss

I am new to PyGithub SDK and trying to do below operations. However, having difficulty going through documents to know the usage.

  1. get a file content from a specific branch

command line equivalent:

git checkout <<branch>> and then get file content

Issue:

I am able to get file content from master branch using this sdk but not from specific branch

  1. repo tags and its details

command line equivalent:

git tag -v <<tag_id>>

  1. Update that file

I see here the docs https://pygithub.readthedocs.io/en/latest/examples/Repository.html#update-a-file-in-the-repository

  1. Update user details for commit
git config user.email "[email protected]"
git config user.name "cicd"
git commit --amend --reset-author --no-edit
  1. Git pull that branch before commit as other automation might have committed some other file.
  2. git commit in the branch
git pull --no-edit
git add $FILE_NAME
git commit -m "updating $FILE_NAME"
git pull --no-edit && git push origin

I am also trying to find the method/api of this sdk to do that, but if someone have it ready, please help with the same.

hi! I'm trying to get the content of an specific branch too. Did you get it? Thanks!

estagumor avatar Dec 04 '19 10:12 estagumor

@estagumor If you still need this answer the key is the ref parameter in the get_contents function. The default is "master". Here's an example getting the contents of the "0.6.X" branch of the scikit-learn repo.

from github import Github

g = Github("USERNAME", "PASSWORD")
repo = g.get_repo("scikit-learn/scikit-learn")
repo.get_contents("", ref = "0.6.X")

gfleetwood avatar May 03 '20 06:05 gfleetwood

@sfdye I just started working with PyGithub and it works great. However even if i'am familiar with the git api, it would be great if you can provide a working example of how to set create / set branch protection rules.

I tried for example: g.get_repo('SECRET/SECRET').get_branch('master').add_required_signatures()

qbits-rwx avatar Jun 18 '20 12:06 qbits-rwx

@qbits-rwx I think the method you need is: https://pygithub.readthedocs.io/en/latest/github_objects/Branch.html#github.Branch.Branch.edit_protection

https://developer.github.com/v3/repos/branches/#update-branch-protection

sfdye avatar Jun 18 '20 12:06 sfdye

Yes this did the trick. Unfortunately, require signed commits, is not implemented. Would be nice to have, as it is a policy we need to set on every branch.

qbits-rwx avatar Jun 18 '20 14:06 qbits-rwx

Feel free to send a FR or PR

sfdye avatar Jun 19 '20 00:06 sfdye

#1628 A full example to upload multipe files in one commit

xpdable avatar Aug 10 '20 11:08 xpdable

Hey, I was going through Pull Requests examples, is there any way to get a pull request by its HTML? Or is there any function available to convert HTML URL to Github API query?

shriaas2898 avatar Aug 22 '20 11:08 shriaas2898

Thanks a lot for these examples! What I'm missing is an example for exception handling. For instance, it wasn't immediately clear to me how to catch a github.GithubException.UnknownObjectException.

NicoWeio avatar Mar 11 '21 14:03 NicoWeio

Test show the test result

wsjohnso007 avatar Oct 26 '21 14:10 wsjohnso007

Yes this did the trick. Unfortunately, require signed commits, is not implemented. Would be nice to have, as it is a policy we need to set on every branch.

@qbits-rwx Following should help with "require signed commit" setting on a branch: https://pygithub.readthedocs.io/en/latest/github_objects/Branch.html#github.Branch.Branch.add_required_signatures

nitishagar avatar Dec 05 '21 12:12 nitishagar

Hi sfdye, Can you add a example about add comment in pull request and update comment in pull request, many thanks.

wangzelin007 avatar Feb 11 '22 03:02 wangzelin007

How to download a repo ??

VarnaX avatar Mar 02 '22 12:03 VarnaX

@VarnaX git clone it or recursively download its files using a for loop

sortalost avatar May 02 '22 04:05 sortalost