The Wayback Machine - https://web.archive.org/web/20220106190946/https://github.com/PyGithub/PyGithub/issues/874
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyGithub example usage #874

Open
sfdye opened this issue Aug 21, 2018 · 21 comments
Open

PyGithub example usage #874

sfdye opened this issue Aug 21, 2018 · 21 comments

Comments

@sfdye
Copy link
Member

@sfdye sfdye commented Aug 21, 2018

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.

@Abdur-rahmaanJ
Copy link

@Abdur-rahmaanJ Abdur-rahmaanJ commented Aug 21, 2018

@sfdye readd the login example please ^^_

vishalsodani added a commit to vishalsodani/PyGithub that referenced this issue Oct 6, 2018
vishalsodani added a commit to vishalsodani/PyGithub that referenced this issue Oct 6, 2018
vishalsodani added a commit to vishalsodani/PyGithub that referenced this issue Oct 8, 2018
vishalsodani added a commit to vishalsodani/PyGithub that referenced this issue Oct 8, 2018
sfdye added a commit that referenced this issue Oct 8, 2018
This PR add the follow code examples

* Issue examples (get list, get specific issue, create issue)
* Milestone examples (get list, get specific milestone, create milestone)

Ref.: #874
sfdye added a commit that referenced this issue Oct 11, 2018
## What does this change do?
Adds a small example of how to get at the date a commit took place.

### Changes
#### doc/examples/Commit.rst
Adds a block of code that demonstrates how to get at a commit's date.

## Motivation
This example is in the spirit of #874.

When I began using PyGithub it was not readily apparent to me where the commit date was being abstracted to. Being unfamiliar with Git's API, it took some convincing for me to be satisfied that the `Commit.commit.author/committer.date` path would get me the commit date.

I would hope that this would save somebody a little bit of time in the future.

## How has this been tested?
I've built the docs locally and made sure the HTML is generated as expected.

## Screenshots
![2018-10-11 3](https://user-images.githubusercontent.com/16039329/46782728-add4d980-ccfd-11e8-88dd-1e48be629eb5.png)
@mscheper
Copy link

@mscheper mscheper commented Oct 11, 2018

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
Copy link

@mscheper mscheper commented Oct 15, 2018

Thanks, @LeviMatus!

sfdye added a commit that referenced this issue Oct 16, 2018
## What does this change do?
Adds a small demo implementation of how one might use webhooks to listen for
new events in a given repository.

### Additions
#### doc/examples/Webhook.rst
Adds a new `.rst` file, `Webhook.rst`, which can be used for demonstrating the usage of Webhooks in PyGithub.

The example that I've added shows how to continually listen for events using Pyramid in conjunction with PyGithub.

## Motivation
This example is in the spirit of #874. [It was asked](#874 (comment)) that someone provide an example of how to listen to events from a repository.

## How has this been tested?
I've built the docs locally and made sure the HTML is generated as expected.

## Screenshots
![webhook1](https://user-images.githubusercontent.com/16039329/46991191-40e58900-d0db-11e8-87e4-d5cf2cc18b0a.png)
![webhook2](https://user-images.githubusercontent.com/16039329/46991193-42af4c80-d0db-11e8-9b20-678f70e6baee.png)
s-t-e-v-e-n-k added a commit to s-t-e-v-e-n-k/PyGithub that referenced this issue Oct 18, 2018
In the spirit of PyGithub#874, add some examples for working with Branch
objects. It doesn't add everything, but it's a start.
sfdye added a commit that referenced this issue Oct 18, 2018
In the spirit of #874, add some examples for working with Branch
objects. It doesn't add everything, but it's a start.
@stale
Copy link

@stale stale bot commented Dec 14, 2018

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
Copy link

@stale stale bot commented Apr 3, 2019

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 stale bot added the stale label Apr 3, 2019
@sfdye sfdye removed the stale label Apr 4, 2019
@yks0000
Copy link

@yks0000 yks0000 commented May 28, 2019

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.

@stale
Copy link

@stale stale bot commented Jul 27, 2019

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 stale bot added the stale label Jul 27, 2019
@stale stale bot closed this Aug 3, 2019
@sfdye sfdye reopened this Sep 13, 2019
@stale stale bot removed the stale label Sep 13, 2019
@mancuss
Copy link

@mancuss mancuss commented Oct 29, 2019

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

@estagumor
Copy link

@estagumor estagumor commented Dec 4, 2019

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!

@gfleetwood
Copy link

@gfleetwood gfleetwood commented May 3, 2020

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

@qbits-rwx
Copy link

@qbits-rwx qbits-rwx commented Jun 18, 2020

@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
Copy link

@qbits-rwx qbits-rwx commented Jun 18, 2020

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.

@sfdye
Copy link
Member Author

@sfdye sfdye commented Jun 19, 2020

Feel free to send a FR or PR

@xpdable
Copy link

@xpdable xpdable commented Aug 10, 2020

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

@shriaas2898
Copy link

@shriaas2898 shriaas2898 commented Aug 22, 2020

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?

candrikos added a commit to candrikos/PyGithub that referenced this issue Sep 25, 2020
candrikos added a commit to candrikos/PyGithub that referenced this issue Sep 25, 2020
This PR add the follow code examples

* Issue examples (get list, get specific issue, create issue)
* Milestone examples (get list, get specific milestone, create milestone)

Ref.: PyGithub#874
candrikos added a commit to candrikos/PyGithub that referenced this issue Sep 25, 2020
## What does this change do?
Adds a small example of how to get at the date a commit took place.

### Changes
#### doc/examples/Commit.rst
Adds a block of code that demonstrates how to get at a commit's date.

## Motivation
This example is in the spirit of PyGithub#874.

When I began using PyGithub it was not readily apparent to me where the commit date was being abstracted to. Being unfamiliar with Git's API, it took some convincing for me to be satisfied that the `Commit.commit.author/committer.date` path would get me the commit date.

I would hope that this would save somebody a little bit of time in the future.

## How has this been tested?
I've built the docs locally and made sure the HTML is generated as expected.

## Screenshots
![2018-10-11 3](https://user-images.githubusercontent.com/16039329/46782728-add4d980-ccfd-11e8-88dd-1e48be629eb5.png)
candrikos added a commit to candrikos/PyGithub that referenced this issue Sep 25, 2020
## What does this change do?
Adds a small demo implementation of how one might use webhooks to listen for
new events in a given repository.

### Additions
#### doc/examples/Webhook.rst
Adds a new `.rst` file, `Webhook.rst`, which can be used for demonstrating the usage of Webhooks in PyGithub.

The example that I've added shows how to continually listen for events using Pyramid in conjunction with PyGithub.

## Motivation
This example is in the spirit of PyGithub#874. [It was asked](PyGithub#874 (comment)) that someone provide an example of how to listen to events from a repository.

## How has this been tested?
I've built the docs locally and made sure the HTML is generated as expected.

## Screenshots
![webhook1](https://user-images.githubusercontent.com/16039329/46991191-40e58900-d0db-11e8-87e4-d5cf2cc18b0a.png)
![webhook2](https://user-images.githubusercontent.com/16039329/46991193-42af4c80-d0db-11e8-9b20-678f70e6baee.png)
candrikos added a commit to candrikos/PyGithub that referenced this issue Sep 25, 2020
In the spirit of PyGithub#874, add some examples for working with Branch
objects. It doesn't add everything, but it's a start.
@NicoWeio
Copy link
Contributor

@NicoWeio NicoWeio commented Mar 11, 2021

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.

@wsjohnso007
Copy link

@wsjohnso007 wsjohnso007 commented Oct 26, 2021

Test show the test result

@nitishagar
Copy link

@nitishagar nitishagar commented Dec 5, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet