Skip to content

Commit 725417f

Browse files
committed
feat: add test-e2e job to ci workflow and example script
1 parent 27e0a92 commit 725417f

4 files changed

Lines changed: 211 additions & 1 deletion

File tree

.github/scripts/example.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import {
2+
createStickyComment,
3+
findCommentByIdentifier,
4+
addLabelsToPullRequest,
5+
pullRequestHasLabels,
6+
getCurrentPullRequestNumber,
7+
getRepoInfo,
8+
sanitizeInput,
9+
escapeMarkdown,
10+
formatDate,
11+
checkBranchExists,
12+
listDeployments,
13+
} from "github-typescript-utils";
14+
15+
type Ctx = {
16+
core: typeof import("@actions/core");
17+
github: ReturnType<typeof import("@actions/github").getOctokit>;
18+
context: typeof import("@actions/github").context;
19+
args: {
20+
testMessage: string;
21+
testLabels: string[];
22+
testBranch: string;
23+
};
24+
};
25+
26+
/**
27+
* E2E test script that exercises various utility functions
28+
* to verify they can be imported and bundled correctly
29+
*/
30+
export default async function run({ core, github, context, args }: Ctx) {
31+
core.info("🚀 Starting E2E test of github-typescript-utils");
32+
33+
try {
34+
// Test 1: Input sanitization
35+
const sanitizedMessage = sanitizeInput(args.testMessage);
36+
core.info(
37+
`✅ Input sanitization: "${args.testMessage}" → "${sanitizedMessage}"`
38+
);
39+
40+
// Test 2: Text formatting utilities
41+
const escapedText = escapeMarkdown("**Bold** _italic_ `code`");
42+
const formattedDate = formatDate(new Date());
43+
core.info(
44+
`✅ Text formatting: escaped="${escapedText}", date="${formattedDate}"`
45+
);
46+
47+
// Test 3: Context utilities
48+
const repoInfo = getRepoInfo(context);
49+
core.info(`✅ Repo info: ${repoInfo.owner}/${repoInfo.repo}`);
50+
51+
// Test 4: PR context (if available)
52+
const prNumber = getCurrentPullRequestNumber(context);
53+
if (prNumber) {
54+
core.info(`✅ PR context: Found PR #${prNumber}`);
55+
56+
// Test PR utilities
57+
const hasLabels = await pullRequestHasLabels({
58+
ctx: { core, github, context },
59+
repo: repoInfo,
60+
pullNumber: prNumber,
61+
labels: args.testLabels,
62+
});
63+
core.info(
64+
`✅ PR label check: has labels [${args.testLabels.join(
65+
", "
66+
)}] = ${hasLabels}`
67+
);
68+
} else {
69+
core.info("ℹ️ No PR context available, skipping PR-specific tests");
70+
}
71+
72+
// Test 5: Branch utilities
73+
const branchExists = await checkBranchExists({
74+
ctx: { core, github, context },
75+
repo: repoInfo,
76+
branchName: args.testBranch,
77+
});
78+
core.info(`✅ Branch check: "${args.testBranch}" exists = ${branchExists}`);
79+
80+
core.info("🎉 E2E test completed successfully!");
81+
82+
return {
83+
success: true,
84+
testsRun: 7,
85+
repoInfo,
86+
prNumber,
87+
branchExists,
88+
};
89+
} catch (error) {
90+
core.error(`❌ E2E test failed: ${error}`);
91+
core.setFailed(`E2E test failed: ${error}`);
92+
return { success: false, error: String(error) };
93+
}
94+
}

.github/scripts/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@tkstang/ci-scripts",
3+
"private": true,
4+
"type": "module",
5+
"dependencies": {
6+
"github-typescript-utils": "file:../../"
7+
}
8+
}

.github/workflows/ci.yml

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636

3737
test:
3838
name: Test Suite
39+
needs: lint
3940
runs-on: ubuntu-latest
4041
steps:
4142
- name: Checkout
@@ -90,10 +91,53 @@ jobs:
9091
echo "" >> $GITHUB_STEP_SUMMARY
9192
echo "✅ **${TEST_COUNT} tests passed** with **${OVERALL_COVERAGE} overall coverage**" >> $GITHUB_STEP_SUMMARY
9293
94+
test-e2e:
95+
name: E2E Test with github-typescript
96+
needs: lint
97+
runs-on: ubuntu-latest
98+
permissions:
99+
contents: read
100+
pull-requests: write
101+
deployments: read
102+
steps:
103+
- name: Checkout
104+
uses: actions/checkout@v4
105+
106+
- name: Setup pnpm
107+
uses: pnpm/action-setup@v4
108+
with:
109+
version: 10
110+
111+
- name: Setup Node.js
112+
uses: actions/setup-node@v4
113+
with:
114+
node-version: 22
115+
cache: pnpm
116+
117+
# Install scripts dependencies (including local utils via file:../../)
118+
- name: Setup scripts dependencies
119+
run: |
120+
cd .github/scripts
121+
pnpm install
122+
123+
# Run the example script through the published wrapper
124+
- name: Run E2E test via github-typescript
125+
uses: tkstang/github-typescript@v1
126+
with:
127+
working-directory: .github/scripts
128+
ts-file: example.ts
129+
node-version: "22"
130+
args: |
131+
{
132+
"testMessage": "Hello E2E Test!",
133+
"testLabels": ["e2e-test", "automated"],
134+
"testBranch": "main"
135+
}
136+
93137
build:
94138
name: Build
95139
runs-on: ubuntu-latest
96-
needs: [lint, test]
140+
needs: [lint, test, test-e2e]
97141
steps:
98142
- name: Checkout
99143
uses: actions/checkout@v4

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,70 @@ yarn add github-typescript-utils
5252

5353
## Usage with github-typescript
5454

55+
### Dependency Setup
56+
57+
For `esbuild` to resolve `github-typescript-utils`, you need the package available in `node_modules`. Choose one approach:
58+
59+
#### Option A: Root Dependencies (Simplest)
60+
61+
Add to your repository's root `package.json`:
62+
63+
```json
64+
{
65+
"dependencies": {
66+
"github-typescript-utils": "^0.2.0"
67+
}
68+
}
69+
```
70+
71+
Workflow setup:
72+
73+
```yaml
74+
- uses: actions/setup-node@v4
75+
with:
76+
node-version: 22
77+
cache: pnpm
78+
- run: pnpm install
79+
80+
- uses: tkstang/github-typescript@v1
81+
with:
82+
ts-file: .github/scripts/manage-pr.ts
83+
```
84+
85+
#### Option B: Isolated CI Dependencies
86+
87+
Create `.github/scripts/package.json`:
88+
89+
```json
90+
{
91+
"name": "ci-scripts",
92+
"private": true,
93+
"type": "module",
94+
"dependencies": {
95+
"github-typescript-utils": "^0.2.0"
96+
}
97+
}
98+
```
99+
100+
Workflow setup:
101+
102+
```yaml
103+
- uses: actions/setup-node@v4
104+
with:
105+
node-version: 22
106+
cache: pnpm
107+
cache-dependency-path: .github/scripts/pnpm-lock.yaml
108+
- run: pnpm install
109+
working-directory: .github/scripts
110+
111+
- uses: tkstang/github-typescript@v1
112+
with:
113+
working-directory: .github/scripts
114+
ts-file: manage-pr.ts
115+
```
116+
117+
### Script Example
118+
55119
Create a TypeScript script that imports the utilities:
56120

57121
```ts

0 commit comments

Comments
 (0)