StaticJsCMS / static-cms

A Git-based CMS for Static Site Generators
https://staticjscms.netlify.app/
MIT License
591 stars 53 forks source link

feat: Support Github App tokens for Github backend #1100

Open soceanainn opened 6 months ago

soceanainn commented 6 months ago

This change introduces support for Github App tokens. Supporting Github App tokens will allow for custom authentication through e.g. AWS Lambda.

There are essentially two differences when dealing with app tokens:

  1. When fetching the repo and checking $.permissions.push in response (hasWriteAccess function), this value will never be true for app tokens (even those with appropriate permissions).
  2. We can't fetch a user using the Github user endpoint for those tokens

For (1) we simply bypass the additional permissions check (validating that the repo can be fetched by the app token is probably enough for most cases).

For (2) we need to create a mock user object for use by StaticCMS (discussed below).

This change also aims to eliminate any redundant calls to fetch the user / repo from Github across the API and implementation code, by having at most one call each.

Creating a mock user for app token

Normally we fetch a GithubUser object using the Github API user endpoint and use that to represent the user (based on the token obtained through auth page).Github App tokens won't have a user defined for them so we cannot use the user endpoint.

Luckily we only seem to use three fields from this object in code: name, login and avatar_url. We can workaround this issue by setting defaults for those three fields by fetching the app, and using name and slug from response to set name and login respectively. avatar_url can be built from id in response.

netlify[bot] commented 6 months ago

Deploy Preview for demo-staticjscms ready!

Name Link
Latest commit bbd1f99d701f408c3c7a61a1464bcc2f0ad9bc2c
Latest deploy log https://app.netlify.com/sites/demo-staticjscms/deploys/660d268cfc2e7c0008510c9a
Deploy Preview https://deploy-preview-1100.demo.staticcms.org
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] commented 6 months ago

Deploy Preview for staticjscms ready!

Name Link
Latest commit bbd1f99d701f408c3c7a61a1464bcc2f0ad9bc2c
Latest deploy log https://app.netlify.com/sites/staticjscms/deploys/660d268cc351d10008e0e2fc
Deploy Preview https://deploy-preview-1100.staticcms.org
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

codecov[bot] commented 6 months ago

Codecov Report

Attention: Patch coverage is 15.38462% with 22 lines in your changes are missing coverage. Please review.

Project coverage is 55.79%. Comparing base (734cecd) to head (bbd1f99).

Files Patch % Lines
...ckages/core/src/backends/github/implementation.tsx 5.55% 15 Missing and 2 partials :warning:
packages/core/src/backends/github/API.ts 37.50% 4 Missing and 1 partial :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1100 +/- ## ========================================== - Coverage 55.83% 55.79% -0.04% ========================================== Files 259 259 Lines 12362 12378 +16 Branches 3110 3116 +6 ========================================== + Hits 6902 6906 +4 - Misses 5048 5058 +10 - Partials 412 414 +2 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.