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!

Sergei Danielian
  • 4,938
  • 4
  • 36
  • 58
  • What exactly is the MyRepo.wiki.git? – SpankMe Jan 29 '13 at 16:50
  • Just an example. It is about any repository with `wiki` suffix. – Sergei Danielian Jan 29 '13 at 16:51
  • I don't understand: GitHub wikis are just ordinary git repositories [as described in this answer](http://stackoverflow.com/a/3484073/165988). 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. – NT3RP Feb 01 '13 at 05:35
  • @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. – Sergei Danielian Feb 01 '13 at 07:05
  • @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 :( – Sergei Danielian Feb 01 '13 at 07:08
  • 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. – m.brindley Feb 02 '13 at 03:14
  • Hi, m.brindley. I have to store just generated test reports in a Wiki repo. It is request from the customer. – Sergei Danielian Feb 02 '13 at 10:33

1 Answers1

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("git@github.com:user/project.wiki.git", "some-path")
repo.index.add(["your-new-file"])
repo.index.commit("your message to the world")
repo.remotes.origin.push()
Community
  • 1
  • 1
payload
  • 422
  • 4
  • 8