-
Notifications
You must be signed in to change notification settings - Fork 0
feat(linear): add Worker-safe planner contract #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
khaliqgant
wants to merge
2
commits into
main
Choose a base branch
from
feat/linear-worker-planner-contract
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,61 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { builtinModules } from 'node:module'; | ||
| import test from 'node:test'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { build } from 'esbuild'; | ||
|
|
||
| import * as plannerContract from '../planner-contract.js'; | ||
|
|
||
| // @ts-expect-error The planner subpath deliberately has no fifth public type export. | ||
| type NoPlannerContractTypeExport = import('../planner-contract.js').LinearPathObjectType; | ||
|
|
||
| test('planner-contract exposes only the Worker planning surface', () => { | ||
| assert.deepEqual(Object.keys(plannerContract).sort(), [ | ||
| 'LINEAR_OBJECT_TYPES', | ||
| 'linearStatePath', | ||
| 'linearStatesIndexPath', | ||
| 'normalizeNangoLinearModel', | ||
| ]); | ||
|
|
||
| assert.equal(plannerContract.normalizeNangoLinearModel('LinearState'), 'state'); | ||
| for (const inheritedKey of ['constructor', 'toString', '__proto__', 'hasOwnProperty']) { | ||
| assert.throws( | ||
| () => plannerContract.normalizeNangoLinearModel(inheritedKey), | ||
| new RegExp(`Unsupported Linear object type: ${inheritedKey}`, 'u'), | ||
| ); | ||
| } | ||
| assert.equal(plannerContract.linearStatePath(' state/id '), '/linear/states/state%2Fid.json'); | ||
| assert.equal(plannerContract.linearStatesIndexPath(), '/linear/states/_index.json'); | ||
| }); | ||
|
|
||
| test('planner-contract public subpath has a pure Worker transitive import graph', async () => { | ||
| const packageRoot = fileURLToPath(new URL('../..', import.meta.url)); | ||
| const result = await build({ | ||
| absWorkingDir: packageRoot, | ||
| bundle: true, | ||
| conditions: ['worker', 'browser', 'import'], | ||
| format: 'esm', | ||
| logLevel: 'silent', | ||
| metafile: true, | ||
| platform: 'browser', | ||
| stdin: { | ||
| contents: `export * from '@relayfile/adapter-linear/planner-contract';`, | ||
| loader: 'js', | ||
| resolveDir: packageRoot, | ||
| sourcefile: 'worker-entry.js', | ||
| }, | ||
| write: false, | ||
| }); | ||
|
|
||
| const runtimeInputs = Object.keys(result.metafile.inputs) | ||
| .filter((input) => input !== 'worker-entry.js') | ||
| .map((input) => input.replaceAll('\\\\', '/')); | ||
|
|
||
| assert.deepEqual(runtimeInputs, ['dist/planner-contract.js']); | ||
|
|
||
| const nodeBuiltins = new Set(builtinModules.flatMap((name) => [name, `node:${name}`])); | ||
| const importedPaths = Object.values(result.metafile.inputs) | ||
| .flatMap((input) => input.imports) | ||
| .map((entry) => entry.path); | ||
| assert.deepEqual(importedPaths.filter((path) => nodeBuiltins.has(path)), []); | ||
| }); | ||
This file contains hidden or 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 hidden or 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,112 @@ | ||
| /** | ||
| * Minimal Linear record-planning contract for Worker/browser consumers. | ||
| * | ||
| * Keep this module dependency-free: the `./planner-contract` package subpath is | ||
| * deliberately safe to include in strict Worker bundles. | ||
| */ | ||
|
|
||
| export const LINEAR_OBJECT_TYPES = [ | ||
| 'comment', | ||
| 'cycle', | ||
| 'issue', | ||
| 'label', | ||
| 'milestone', | ||
| 'project', | ||
| 'roadmap', | ||
| 'state', | ||
| 'team', | ||
| 'user', | ||
| ] as const; | ||
|
|
||
| type LinearPathObjectType = (typeof LINEAR_OBJECT_TYPES)[number]; | ||
|
|
||
| const LINEAR_PATH_ROOT = '/linear'; | ||
|
|
||
| const OBJECT_TYPE_ALIASES: Readonly<Record<string, LinearPathObjectType>> = { | ||
| comment: 'comment', | ||
| comments: 'comment', | ||
| linearcomment: 'comment', | ||
| cycle: 'cycle', | ||
| cycles: 'cycle', | ||
| linearcycle: 'cycle', | ||
| issue: 'issue', | ||
| issues: 'issue', | ||
| issue_label: 'label', | ||
| issuelabel: 'label', | ||
| linearissue: 'issue', | ||
| linearissuelabel: 'label', | ||
| label: 'label', | ||
| labels: 'label', | ||
| linearlabel: 'label', | ||
| milestone: 'milestone', | ||
| milestones: 'milestone', | ||
| projectmilestone: 'milestone', | ||
| projectmilestones: 'milestone', | ||
| linearmilestone: 'milestone', | ||
| project: 'project', | ||
| projects: 'project', | ||
| linearproject: 'project', | ||
| roadmap: 'roadmap', | ||
| roadmaps: 'roadmap', | ||
| linearroadmap: 'roadmap', | ||
| state: 'state', | ||
| states: 'state', | ||
| linearstate: 'state', | ||
| team: 'team', | ||
| teams: 'team', | ||
| linearteam: 'team', | ||
| user: 'user', | ||
| users: 'user', | ||
| linearuser: 'user', | ||
| }; | ||
|
|
||
| /** Nango Linear model names that require an explicit canonical mapping. */ | ||
| const NANGO_MODEL_MAP: Readonly<Record<string, LinearPathObjectType>> = { | ||
| LinearComment: 'comment', | ||
| LinearCycle: 'cycle', | ||
| LinearIssue: 'issue', | ||
| LinearIssueLabel: 'label', | ||
| LinearLabel: 'label', | ||
| LinearMilestone: 'milestone', | ||
| LinearProject: 'project', | ||
| LinearRoadmap: 'roadmap', | ||
| LinearState: 'state', | ||
| LinearTeam: 'team', | ||
| LinearUser: 'user', | ||
| }; | ||
|
|
||
| function assertNonEmptySegment(value: string, label: string): string { | ||
| const trimmed = value.trim(); | ||
| if (!trimmed) { | ||
| throw new Error(`Linear ${label} must be a non-empty string`); | ||
| } | ||
| return trimmed; | ||
| } | ||
|
|
||
| /** Normalize a Nango model name or supported Linear object-type alias. */ | ||
| export function normalizeNangoLinearModel(model: string): LinearPathObjectType { | ||
| const direct = Object.hasOwn(NANGO_MODEL_MAP, model) | ||
| ? NANGO_MODEL_MAP[model] | ||
| : undefined; | ||
| if (direct) return direct; | ||
|
|
||
| const normalized = model.trim().toLowerCase(); | ||
| const mapped = Object.hasOwn(OBJECT_TYPE_ALIASES, normalized) | ||
| ? OBJECT_TYPE_ALIASES[normalized] | ||
| : undefined; | ||
| if (!mapped) { | ||
| throw new Error(`Unsupported Linear object type: ${model}`); | ||
| } | ||
| return mapped; | ||
| } | ||
|
|
||
| /** Canonical flat-record path for a Linear workflow state. */ | ||
| export function linearStatePath(stateId: string): string { | ||
| const encodedStateId = encodeURIComponent(assertNonEmptySegment(stateId, 'path segment')); | ||
| return `${LINEAR_PATH_ROOT}/states/${encodedStateId}.json`; | ||
| } | ||
|
|
||
| /** Canonical resource index path for Linear workflow states. */ | ||
| export function linearStatesIndexPath(): string { | ||
| return `${LINEAR_PATH_ROOT}/states/_index.json`; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure cross-platform compatibility (especially on Windows), we should use
fileURLToPathfromnode:urlinstead of accessing.pathnameon anew URL(...)object. On Windows,.pathnamecan return paths starting with a leading slash (e.g.,/C:/path/to/project), which can cause issues withesbuildand other path resolution tools.