-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add Token Studio sync action (#85)
Adding a GH action to sync tokens studio [category:Infrastructure]
- Loading branch information
1 parent
eaa6465
commit 1127cbb
Showing
4 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
name: Tokens Studio Sync | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
token_type: | ||
description: 'Choose which tokens to sync' | ||
type: choice | ||
options: | ||
- 'all' | ||
- 'base' | ||
- 'brand' | ||
- 'system' | ||
default: 'all' | ||
|
||
jobs: | ||
install: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: Workday/canvas-kit-actions/install@v1 | ||
with: | ||
node_version: 18.x | ||
|
||
sync_tokens: | ||
runs-on: ubuntu-latest | ||
needs: install | ||
|
||
steps: | ||
- name: Sync Tokens | ||
shell: bash | ||
working-directory: ./canvas-tokens | ||
run: yarn tokens-config sync ${{ inputs.token_type }} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
create_pull_request: | ||
runs-on: ubuntu-latest | ||
needs: sync_tokens | ||
|
||
steps: | ||
- name: Create Sync PR | ||
shell: bash | ||
working-directory: ./canvas-tokens | ||
run: yarn tokens-config create-pull | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {canvasTokensRepoParams, ghClient} from './api-client'; | ||
import {RestEndpointMethodTypes} from '@octokit/rest'; | ||
|
||
async function getBranches() { | ||
try { | ||
const {data} = await ghClient.repos.listBranches({ | ||
owner: canvasTokensRepoParams.owner, | ||
repo: canvasTokensRepoParams.repo, | ||
}); | ||
return data; | ||
} catch (error: any) { | ||
console.error('Error: Failed to get branches.', error.message); | ||
} | ||
} | ||
|
||
type CreateBranchParams = RestEndpointMethodTypes['git']['createRef']['parameters']; | ||
type UpdateBranchParams = RestEndpointMethodTypes['git']['updateRef']['parameters']; | ||
|
||
async function createBranch(params: CreateBranchParams) { | ||
try { | ||
await ghClient.git.createRef(params); | ||
} catch (error: any) { | ||
console.error('Error: Failed to create branch.', error.message); | ||
} | ||
} | ||
|
||
async function updateBranch(params: UpdateBranchParams) { | ||
try { | ||
await ghClient.git.updateRef(params); | ||
} catch (error: any) { | ||
console.error('Error: Failed to update branch.', error.message); | ||
} | ||
} | ||
|
||
export async function createSyncBranch() { | ||
const branches = await getBranches(); | ||
if (branches) { | ||
const syncBranch = branches.find(branch => branch.name === canvasTokensRepoParams.syncBranch); | ||
const mainBranch = branches.find( | ||
branch => branch.name === canvasTokensRepoParams.defaultBranch | ||
); | ||
const syncBranchRef = `refs/heads/${canvasTokensRepoParams.syncBranch}`; | ||
// The main branch should always be available, but TS doesn't know that, so we have this extra conditional. | ||
if (mainBranch) { | ||
// If the sync branch doesn't exist, create it | ||
if (!syncBranch) { | ||
await createBranch({ | ||
owner: canvasTokensRepoParams.owner, | ||
repo: canvasTokensRepoParams.repo, | ||
ref: syncBranchRef, | ||
sha: mainBranch.commit.sha, | ||
}); | ||
} else { | ||
// If the sync branch already exists, force update to ensure it's up-to-date with main | ||
await updateBranch({ | ||
owner: canvasTokensRepoParams.owner, | ||
repo: canvasTokensRepoParams.repo, | ||
ref: syncBranchRef, | ||
sha: mainBranch.commit.sha, | ||
force: true, | ||
}); | ||
} | ||
} | ||
} | ||
} |