Skip to content

Commit

Permalink
chore: Add Token Studio sync action (#85)
Browse files Browse the repository at this point in the history
Adding a GH action to sync tokens studio

[category:Infrastructure]
  • Loading branch information
alanbsmith committed Mar 5, 2024
1 parent eaa6465 commit 1127cbb
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
49 changes: 49 additions & 0 deletions .github/workflows/tokens-studio-sync.yml
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 }}
2 changes: 2 additions & 0 deletions scripts/tokens-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {syncBaseConfig} from './utils/sync-base-config';
import {syncBrandConfig} from './utils/sync-brand-config';
import {syncSystemConfig} from './utils/sync-sys-config';
import {createSyncPullRequest} from './utils/pull-request';
import {createSyncBranch} from './utils/create-sync-branch';

const syncTypeArg = new Argument('type', 'Specify which type of tokens to sync')
.choices(['all', 'base', 'brand', 'system'])
Expand All @@ -27,6 +28,7 @@ program
.description('Sync Canvas Tokens repo with Tokens Studio config')
.addArgument(syncTypeArg)
.action(async type => {
await createSyncBranch();
switch (type) {
case 'base':
await syncBaseConfig();
Expand Down
2 changes: 1 addition & 1 deletion scripts/utils/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ config();
// Client for GitHub
export const ghClient = new Octokit({
auth: process.env.GITHUB_TOKEN,
baseUrl: process.env.GITHUB_BASE_URL,
baseUrl: 'https://api.github.com',
});

export const tokensStudioRepoParams = {
Expand Down
65 changes: 65 additions & 0 deletions scripts/utils/create-sync-branch.ts
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,
});
}
}
}
}

0 comments on commit 1127cbb

Please sign in to comment.