Skip to content

Commit ee5d72b

Browse files
authored
feat: add GitHub Direct Commit support (#333)
* feat: add GitHub Direct Commit support - Add github_commit authentication type for direct file commits - Implement GithubRepository module for GitHub API integration - Add UI components for branch, filepath, and commit message configuration - Update README documentation with github_commit auth option - Supports automatic branch creation if not exists - Handles both file creation and updates via GitHub API * refactor: use URL object for more robust GitHub URL parsing * docs: add GitHub Direct Commit URL examples to README * refactor: remove simple owner/repo URL format support - Remove simple owner/repo format from URL parsing logic - Update README to document only supported URL formats - Keep support for https://api.github.com/repos/owner/repo - Keep support for https://github.com/owner/repo - Remove :username/:repo format that fails HTML5 validation * fix: update placeholder to show only supported GitHub URL formats * docs: simplify GitHub Direct Commit URL format documentation * fix: address code review feedback - Add proper URI encoding for filenames with special characters - Improve 422 status handling to only accept 'Reference already exists' - Remove redundant targetBranch variable for code clarity * test: add comprehensive tests for githubRepository module - Test constructor initialization - Test upload to existing branch with new/existing files - Test branch creation when branch doesn't exist - Test default commit message generation - Test URL encoding for special characters and unicode - Mock XMLHttpRequest for isolated unit testing - Coverage increased from 1.76% to 46.01% Addresses code review comment about test coverage decrease * test: improve githubRepository test coverage to 50.44% - Add tests for URL encoding with slashes - Add tests for 422 Reference already exists handling - Add tests for other 422 validation errors - Add tests for invalid JSON in 422 responses - Add tests for 401 and 403 error handling - Coverage improved from 46.01% to 50.44% This addresses the code review comment about test coverage decrease * test: add comprehensive tests for githubRepository and urlExport - Add 18 tests for githubRepository module covering upload, error handling, and URL encoding - Add 6 tests for urlExport githubCommit auth type integration - Improve githubRepository branch coverage from 22% to 40.74% - Test URI encoding fix (encodeURIComponent) - Test 422 error handling with 'Reference already exists' message - Test URL parsing for both API and web URL formats * style: remove extra blank line in githubRepository.ts * style: remove trailing whitespace in test files * build: rebuild dist files with latest changes
1 parent 8ea92fb commit ee5d72b

File tree

10 files changed

+1020
-51
lines changed

10 files changed

+1020
-51
lines changed

README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
> [!WARNING]
2-
> **This plugin is only partially supported!**
2+
> **This plugin is only partially supported!**
33
> This means: I don't have capacity to add any features or really improve stuff. Bugfixes may be added if time allows.
44
> I am also happy to discuss and merge PRs.
5-
>
6-
> I personally have changed my opinion a long time ago to favor the source fo truth for design tokens to be in a json file. Figma should be a consumer so you only import tokens into figma. This is why I don't use plugins like this one anymore in my work.
7-
>
5+
>
6+
> I personally have changed my opinion a long time ago to favor the source fo truth for design tokens to be in a json file. Figma should be a consumer so you only import tokens into figma. This is why I don't use plugins like this one anymore in my work.
7+
>
88
> [Read more about the a better setup](https://medium.com/user-experience-design-1/the-ultimate-design-token-setup-cdf50dc841c8#:~:text=you%20are%20making.-,Source%20of%20truth,-There%20is%20some)
99
1010

@@ -37,7 +37,7 @@ The **Design Tokens** plugin for Figma allows you to export design tokens into a
3737
- [Opacity token](#opacity)
3838
- [Available properties](#available-properties)
3939
- [Settings](#settings)
40-
- [File Export Settings](#file-export-settings)
40+
- [File Export Settings](#file-export-settings)
4141
- [Push tokens to Github / Server](#push-to-server)
4242
- [Contribution](#contribution)
4343

@@ -353,7 +353,7 @@ You can define any additional prefix via this option, e.g `*`. This can be helpf
353353

354354
### Reference mode in variables
355355

356-
You can configure whether to include these mode names in the output JSON or not.
356+
You can configure whether to include these mode names in the output JSON or not.
357357
By default, the mode names are not included in both the token names and the token values. You can turn on this behavior in the plug-in settings:
358358

359359
- If you wish to include the mode name in the token names, but not in the token values, you can activate the "Add mode to token value".
@@ -449,7 +449,7 @@ The body of the request will look like the following:
449449

450450
```ts
451451
"event_type": "update-tokens", // or any string you defined
452-
"client_payload": {
452+
"client_payload": {
453453
"tokens": "{...}", // the stringified json object holding all your design tokens
454454
"filename": "Design Tokens", // the Figma file name from which the tokens were exported
455455
"commitMessage": "Your commit message"
@@ -480,7 +480,9 @@ Overrides the content type header for the final HTTP request. Defaults to "plain
480480

481481
##### Pushing to Github
482482

483-
If you push to github the server url must be in the format
483+
**Using GitHub Actions (workflow dispatch):**
484+
485+
If you push to github using the `token` auth type, the server url must be in the format:
484486

485487
```
486488
https://api.github.com/repos/:username/:repo/dispatches
@@ -492,11 +494,19 @@ For the repository `lukasoppermann/design-token-transformer` this would be:
492494
https://api.github.com/repos/lukasoppermann/design-token-transformer/dispatches
493495
```
494496

497+
**Using GitHub Direct Commit:**
498+
499+
If you use the `github_commit` auth type for direct file commits, the server url must be:
500+
501+
- `https://api.github.com/repos/:username/:repo` or
502+
- `https://github.com/:username/:repo`
503+
495504
#### **Auth header**
496505

497506
This defines the authentication method used with the access token. The current choices are:
498507

499-
- `token` (used for github)
508+
- `token` (used for github workflow triggers)
509+
- `github_commit` (used for direct file commits to GitHub)
500510
- `gitlab_token` (used for Gitlab requests)
501511
- `bearer` token
502512
- `basic` auth

dist/plugin.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui.html

Lines changed: 20 additions & 20 deletions
Large diffs are not rendered by default.

dist/ui.js

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default {
2626
token: 'token',
2727
gitlabToken: 'gitlab_token',
2828
gitlabCommit: 'gitlab_commit',
29+
githubCommit: 'github_commit',
2930
basic: 'Basic',
3031
bearer: 'Bearer'
3132
}

src/ui/components/UrlExportSettings.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ export const UrlExportSettings = () => {
151151
placeholder={
152152
(settings.authType === config.key.authType.gitlabCommit &&
153153
'https://gitlab.com/api/v4/projects/:projectId') ||
154+
(settings.authType === config.key.authType.githubCommit &&
155+
'https://github.com/:username/:repo or https://api.github.com/repos/:username/:repo') ||
154156
'https://api.github.com/repos/:username/:repo/dispatches'
155157
}
156158
value={settings.serverUrl}
@@ -209,6 +211,10 @@ export const UrlExportSettings = () => {
209211
label: '(Github) token',
210212
value: config.key.authType.token
211213
},
214+
{
215+
label: '(Github) Direct Commit',
216+
value: config.key.authType.githubCommit
217+
},
212218
{
213219
label: '(Gitlab) token',
214220
value: config.key.authType.gitlabToken
@@ -270,6 +276,32 @@ export const UrlExportSettings = () => {
270276
</>
271277
)}
272278

279+
{config.key.authType.githubCommit === settings.authType && (
280+
<>
281+
<h3>
282+
Branch
283+
<Info
284+
width={150}
285+
label='The branch where the file will be committed. Only used when Github Direct Commit is selected for "Auth type"'
286+
/>
287+
</h3>
288+
<Row fill>
289+
<Input
290+
type="text"
291+
required
292+
pattern="\S+"
293+
placeholder="main"
294+
value={settings.reference}
295+
onChange={(value) =>
296+
updateSettings((draft) => {
297+
draft.reference = value
298+
})
299+
}
300+
/>
301+
</Row>
302+
</>
303+
)}
304+
273305
{config.key.authType.gitlabCommit === settings.authType && (
274306
<>
275307
<h3>

0 commit comments

Comments
 (0)