How to upload files to GitHub with GDscript?

Godot Version

4.0

Question

I’m working on a game with user-generated levels and I want to use GitHub as a way to store the level files, which are JSON files. I have a repository made for this and a fine-grained access token that can access the repository.
However, I don’t know to do this. I probably need the HTTPRequest node. Can anyone help me?

I’m trying to understand this in my own head…

How do you want users to submit the JSON files?

To actually store the files you need to use git, but I don’t think you intend for your users to use git, or have access to git?

I want to store them in a GitHub repository I have on my account, and when users click the share button, the game should make a commit on the repository to add the file.

So you want to give write-access to one of your github repositories to all your users.

I’m not sure, if this is a feasible approach, but you might find infos about how to do that on the github-side here:

1 Like

I like this approach, I was thinking you would need to package git inside Godot to interact with the repository. Setting up an app as a server seem like the better approach, but if there are usage limits that our poster here would need to worry about.

What I was looking for was what code I needed in godot to do this

I actually found a way to do what I want with JavaScript. Now I need to somehow do it in GDscript.
Here’s the code:

async function upload() {
  const message = 'Test commit';
  const content = 'Test message';
  const owner = 'My Username';
  const repo = 'My repo';
  const path = 'test.txt';
  const auth = 'My Access Token';

  const existingFile = await (await fetch(
    `https://api.github.com/repos/${owner}/${repo}/contents/${path}`,
    {
      method: 'GET',
      headers: {
        Accept: 'application/vnd.github+json',
        Authorization: `Bearer ${auth}`
      }
    }
  )).json();

  await (await fetch(
    `https://api.github.com/repos/${owner}/${repo}/contents/${path}`,
    {
      method: 'PUT',
      headers: {
        Accept: 'application/vnd.github+json',
        Authorization: `Bearer ${auth}`
      },
      body: JSON.stringify({
        message: message,
        content: btoa(content),
        sha: existingFile.sha,
      }),
    }
  )).json();
} upload()