Estoy trabajando en un creador de sitios web personal. Mi objetivo es poder manipular mi página dentro del navegador y luego presionar el botón 'Publicar' y hacer que el nuevo archivo HTML se confirme en el repositorio de Github donde está alojado mi código.
Pude resolver todo hasta generar el HTML actualizado. Sin embargo, después de revisar la documentación de GitHub, no pude entender realmente cómo se deben procesar los archivos antes o enviarlos al repositorio usando la API. ¿Podría alguien ayudarme?
PD: estaría usando JavaScript para generar el HTML y para las llamadas a la API. Además, si hay un método mejor para lograr mi objetivo, también estoy abierto a ellos.
Usar extremos de contenido del repositorio de GitHub API v3
Es necesario tener en cuenta lo siguiente:
Aquí hay un ejemplo de trabajo con Node.js con Octokit API usando el método createOrUpdateFileContents . Lea más sobre esto aquí
Este punto final creará una nueva confirmación con el nuevo contenido del archivo, incluso puede especificar el autor y el autor.
| Actualizar el contenido del archivo en GitHub | Ver en Fusebit |
|---|
// The content of the file should be base64 const content = Buffer.from(`Hello world at ${new Date().toUTCString()}`, 'utf8').toString('base64'); // If the file already exists, you need to provide the sha in order to update the file content. const file_sha = await getFileShaIfExists(octokit, owner, repo, path); const fileContent = await octokit.rest.repos.createOrUpdateFileContents({ owner, repo, path, sha: file_sha, message: 'This is the commit message generated via GitHub API', content, committer: { name: 'demo', email: 'demo@example.com' }, author: { name: 'demo', email: 'demo@example.com' } }); const { commit: { html_url } } = fileContent.data; console.log(`Content updated, see changes at ${html_url}`);