How can I commit files to Github by Mathematica?

Another option for individual files and stuff is to use the GitHub API with a custom plug-in to the built-in service connection framework.

Here's a service connection I wrote for it:

PacletInstall["ServiceConnection_GitHub",
 "Site"->"http://www.wolframcloud.com/objects/b3m2a1.paclets/PacletServer"
 ]

Then we load it like this:

$gh = ServiceConnect@"GitHub"

github connection

We can see what requests are there:

In[73]:= $gh["Requests"]

Out[73]= {"AddFile", "ArchiveLink", "Authentication", "BranchInfo", \
"BranchProtection", "CreateOrganizationRepository", \
"CreateRepository", "DeleteFile", "DeleteRepository", \
"EditRepository", "ID", "ImportFile", "Information", "ListBranches", \
"ListMyRepositories", "ListOrganizationRepositories", \
"ListRepositories", "ListUserRepositories", "Name", "RawRequests", \
"ReadMeContents", "RemoveBranchProtection", "RepositoryContributors", \
"RepositoryInfo", "RepositoryLanguages", "RepositoryTags", \
"RepositoryTeams", "RequestData", "RequestParameters", \
"SetBranchProtection", "UpdateFile"}

So we'll start by making a testing repo. First we'll see what we need:

In[74]:= $gh["RequestParameters", 
  "Request" -> "CreateRepository"]["Required"]

Out[74]= {"name"}

So we'll give it a name like api-testing-repo:

In[80]:= $gh["CreateRepository", "name" -> "api-testing-repo"][{"id", 
  "name"}]//Normal

Out[80]= <|"id" -> 93099247, "name" -> "api-testing-repo"|>

Then we upload a file (note that it has to be Base64 encoded):

$gh[
  "AddFile",
  "user" -> "b3m2a1",
  "repo" -> "api-testing-repo",
  "path" -> "test.txt",
  "content" -> Developer`EncodeBase64@"file_text_goes_here",
  "message" -> "file upload test"
  ]["content", "sha"]

And we can see it exists:

file exists

Now we'll delete the file:

$gh[
 "DeleteFile",
 "user" -> "b3m2a1",
 "repo" -> "api-testing-repo",
 "path" -> "test.txt",
 "sha" -> "6e560c960830951d7d77b0960cd961173ed86ce0",
 "message" -> "file delete test"
 ]

And we'll see it no longer exists:

no more file

And now for good measure we delete the repo:

$gh[
 "DeleteRepository",
 "user" -> "b3m2a1",
 "repo" -> "api-testing-repo"
 ]

Publish to GitHub with Mathematica on Windows


I'm on windows 8.1

Let's just concentrate on how to commit the files. [git push]

Method


1 Auto type username and password


download an assistant tool小助手[Permanently authenticating with Git repositories]

my git-credential-winstore.exe is put and installed here

"E:\\Users\\Hyper\\AppData\\Local\\GitHub\\PortableGit_\fed20eba68b3e238e49a47cdfed0a45783d93651\\bin"

2 Put commands in the .bat file


Put the commands set used in GitShell in the .bat file of the portable version of GitHub.

batFile =   "E:\\Users\\Hyper\\AppData\\Local\\GitHub\\PortableGit_\fed20eba68b3e238e49a47cdfed0a45783d93651\\git-cmd.bat";

batSample =   "@rem Do not use \"echo off\" to not affect any child calls.  @setlocal    @rem Get the abolute path to the current directory, which is \assumed to be the  @rem Git installation root.  @for /F \"delims=\" %%I in (\"%~dp0\") do @set git_install_root=%%~fI  @set PATH=%git_install_root%\\bin;%git_install_root%\\mingw\\bin;%\git_install_root%\\cmd;%PATH%    @if not exist \"%HOME%\" @set HOME=%HOMEDRIVE%%HOMEPATH%  @if not exist \"%HOME%\" @set HOME=%USERPROFILE%    @set PLINK_PROTOCOL=ssh  @if not defined TERM set TERM=msys    @cd %HOME%  ::@start %COMSPEC%    ::@start \"\" cmd /k @cd /d documents\\github && @git    cd /d documents\\github\\hypergroups.github.com  ";

commandsSet = "git add .  git commit -a -m \"" <> windowTitle[EvaluationNotebook[]] <> "\"  git push"

(*
    git add .git commit -a -m "Publish to GitHub with Mathematica on Windows"git push
*)

batFileToExport = batSample <> commandsSet(*<>"\npause"*);

Export["bat.txt", batFileToExport];DeleteFile@batFile;CopyFile["bat.txt", batFile];

3 Run the .bat file

enter image description here


batFile // SystemOpen

References


Also, I asked here

I've redone this succesfully.

Tags:

Web Access