PyGithub example usage

Created on 21 Aug 2018  ·  19Comments  ·  Source: PyGithub/PyGithub

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.

documentation good first issue high priority

Most helpful comment

I am Writing Organization examples now

All 19 comments

@sfdye readd the login example please ^^_

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?

Thanks, @LeviMatus!

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.

this is not stale

I am Writing Organization examples now

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.

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.

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.

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

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 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")

@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()

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.

Feel free to send a FR or PR

1628 A full example to upload multipe files in one commit

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?

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.

Was this page helpful?
0 / 5 - 0 ratings