1

I'd like to be able to publish to Github one way. I've read part of the API here at https://docs.github.com/en/rest/quickstart.

It looks like it's possible to read resources from github using this API but I didn't see if it's possible to publish to a repository.

I believe I'd need the user to have git installed and run that. But I'm in a web based environment. That web based environment can't access or run local git calls.

Since my goal is only to push a complete set of files (no diffs) to a specific repository folder, is that possible with the github API?

For example, consider a web form that has an upload feature. In the web page you choose a file with a file input and then send that file with a network request like so:

var request = new XMLHttpRequest(); var formData = new FormData(); formData.append('file', file); request.addEventListener('load', responseHandler, false); request.open('POST', '/upload', true); request.send(formData);

I want to do the same thing. Basically, upload a file or files to a folder in my repository. I can probably reuse the credentials if I'm logged into Github in another tab or authorize through github login.

UPDATE:
This is for the user to be able to post to their own repository not mine. The use case is that the user wants one way write support to a directory of their repository (the github pages directory). They don't need diffs or git or anything else because they are creating the content on their local machine.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • 3
    This feels like a misuse of Git? – matt Mar 09 '23 at 20:17
  • 1
    See: https://docs.github.com/en/rest/git/commits?apiVersion=2022-11-28#create-a-commit and https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28 – jessehouwing Mar 09 '23 at 20:33
  • 1
    https://stackoverflow.com/a/61704866/7976758 Found in https://stackoverflow.com/search?q=%5Bgithub-api%5D+commit+file – phd Mar 09 '23 at 20:57
  • @ikegami If the user is logged in then they may be able to reuse the session. but now that I think about it, this is not unlike a form on github.com with an upload button. and obviously, it can't be insecure lol – 1.21 gigawatts Mar 09 '23 at 21:32
  • @matt maybe but related to git where git may not be available – 1.21 gigawatts Mar 09 '23 at 21:33
  • @ikegami they would be posting to their own repository not mine. i'll update the question details. they would use my app to post. i will have to figure out authorization. but i believe that github has app authorization. but, if it's their own repo that should reuse logged in credentials maybe. see new notes. ie publishing to github pages. – 1.21 gigawatts Mar 09 '23 at 21:50
  • @jessehouwing that looks promising – 1.21 gigawatts Mar 09 '23 at 21:54
  • 1
    You can use an Oauth app on github so allow them to authenticate and grant your app access. https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authenticating-to-the-rest-api-with-an-oauth-app – jessehouwing Mar 09 '23 at 22:04

1 Answers1

3

You can setup a OAuth app which people will have to install into their account. You can initiate that installation flow from your app.

An Oauth app will let you act on behalf the logged in user. This sounds like the scenario you're after.

With the OAuth token in hand, you can call create or update file API to upload the new file.

// Octokit.js
// https://github.com/octokit/core.js#readme
const octokit = new Octokit({
  auth: 'YOUR-TOKEN'
})

await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}', {
  owner: 'OWNER',
  repo: 'REPO',
  path: 'PATH',
  message: 'my commit message',
  committer: {
    name: 'Monalisa Octocat',
    email: 'octocat@github.com'
  },
  content: 'bXkgbmV3IGZpbGUgY29udGVudHM=',
  headers: {
    'X-GitHub-Api-Version': '2022-11-28'
  }
})
jessehouwing
  • 106,458
  • 22
  • 256
  • 341