Work with GitHub Wiki repository using Python
Asked Answered
S

1

8

Is there any way to programmatically (using libraries like PyGithub, GitPython or dulwich) load any file right into MyRepo.wiki.git repository? Using Python, of course.

I can easily upload a file right into MyRepo.git repository with a help of PyGithub, but, unfortunately, this library has no API or ways to work with MyRepo.wiki.git repository.

Here is how I can upload a file to MyRepo.git repository:

github_repo = github_account.get_user().get_repo('MyRepo')

head_ref = gh_repo.get_git_ref("heads/%s" % github_branch)
latest_commit = gh_repo.get_git_commit(head_ref.object.sha)

base_tree = latest_commit.tree

new_tree = gh_repo.create_git_tree(
    [github.InputGitTreeElement(
        path="test.txt",
        mode='100755' if github_executable else '100644',
        type='blob',
        content="test"
    )],
    base_tree)

new_commit = gh_repo.create_git_commit(
    message="test commit message",
    parents=[latest_commit],
    tree=new_tree)

head_ref.edit(sha=new_commit.sha, force=False)

So, how can I do the same but with MyRepo.wiki.git repository? If you can provide an example using PyGithub library - it would be really great.

P.S. Can I do this using Gollum API?

P.P.S. Has not anybody worked with *.wiki.git using any kind of python library? I do not believe :(

P.P.P.S. If I was not clear enough: I DO NOT want to create a local repository in any way. All I want to modify repo structure on-the-fly - just how my example does. But with *.wiki.git repository.

Thank you!

Shaven answered 29/1, 2013 at 16:35 Comment(7)
What exactly is the MyRepo.wiki.git?Sunfast
Just an example. It is about any repository with wiki suffix.Shaven
I don't understand: GitHub wikis are just ordinary git repositories as described in this answer. If you can already clone the wiki as a separate repository, and there you have a way to manipulate a general git repository, then you have a solution.Mireille
@NT3RP. I can manipulate with my own public and private repository. But I am a contributor to third party private repository. And I can't request those repository directly - only in performing a loop with comparing if the next repo is of my interest. Also, I can do things like: ...getRepo("myrepo.git"), but this doesn't work with the following: ...getRepo("myrepo.wiki.git"). Please, note, I indeed can use git tool but I want to do everything from Python library.Shaven
@NT3RP. Please note, I can find that mentioned third party repo using cycle and get_repos() function (in case of PyGithub). But *.wiki.git repository is not included in that list. The only way I can find out if the repo has a Wiki, is request the found repo using has_wiki property: repo.has_wiki. And that's all :(Shaven
PyGithub doesn't look like it is built to support the attached wiki repositories (not included in the retrieved repo list) so if you need them, I don't think it's suitable. You could use GitPython quite easily from my experience - you'd lose all of the nice GitHub traversal functionality but I have just used a list of repos to work with in a settings.py file without problems. What kind of automation would you need to do on a wiki repository though? I suppose it could be useful for pydocs.Daven
Hi, m.brindley. I have to store just generated test reports in a Wiki repo. It is request from the customer.Shaven
B
3

You can't access a github wiki via the github web API, which PyGithub uses exclusivly. But you can point your GitPython at the git URL of the wiki. After that you can access files in this git repo as with any other repo.

Edited

As you pointed out, that you restrict yourself not to create a local clone of the git repository I recommend following:

Read the code in https://github.com/github/gollum/blob/master/lib/gollum/frontend/app.rb which defines maybe all of the external HTTP interface.

A nicely wrapper around it for Python doesn't exist. But when you do make one (partially) than I recommend using a REST client library like mentioned here: https://stackoverflow.com/questions/2176561/which-is-the-best-python-library-to-make-rest-request-like-put-get-delete-pos

If you now think, that your restriction could be changed:

The documentation provides a good tutorial covering everything one needs with a little git knowledge. It boils down to:

import git
repo = git.Repo.clone_from("[email protected]:user/project.wiki.git", "some-path")
repo.index.add(["your-new-file"])
repo.index.commit("your message to the world")
repo.remotes.origin.push()
Boll answered 3/2, 2013 at 15:22 Comment(3)
Could you provide a simple example?Shaven
If you read clearly my question, it was about how to change *.wiki.git repository without creating any local repository (please take a look at my example in the question's description).Shaven
Thank you for trying to help me. I appreciate this. I will check your suggestion (I hope, that Gollum will give me a hint anyway).Shaven

© 2022 - 2024 — McMap. All rights reserved.