Skip to content

Commit 40d6b85

Browse files
committed
add tests to version re-write
1 parent b6f19a4 commit 40d6b85

13 files changed

Lines changed: 403 additions & 123 deletions

File tree

actions/release-tag-creation/action.yml

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ outputs:
2828
value: ${{ steps.create-release-tag.outputs.tag_pushed }}
2929
readme_stable_tag:
3030
description: 'The Stable tag value written to readme.txt.'
31-
value: ${{ steps.update-version-in-tag-files.outputs.readme_stable_tag }}
31+
value: ${{ steps.capture-readme.outputs.readme_stable_tag }}
3232
readme_beta_tag:
3333
description: 'The Beta tag value written to readme.txt.'
34-
value: ${{ steps.update-version-in-tag-files.outputs.readme_beta_tag }}
34+
value: ${{ steps.capture-readme.outputs.readme_beta_tag }}
3535

3636
runs:
3737
using: composite
@@ -55,47 +55,13 @@ runs:
5555
git config --global user.name "${{ inputs.maintain_username }}"
5656
git config --global user.email "${{ inputs.maintain_email }}"
5757
58-
- name: Update version in elementor.php
59-
id: capture-php
60-
shell: bash
61-
run: |
62-
VERSION="${{ inputs.version }}"
63-
sed -i -E "s/( \* Version: ).*/\1${VERSION}/" elementor.php
64-
sed -i -E "s/(define\( 'ELEMENTOR_VERSION', ')[^']*'/\1${VERSION}'/" elementor.php
65-
echo "elementor.php after version update:"
66-
grep -E "Version:|ELEMENTOR_VERSION" elementor.php
67-
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
68-
69-
- name: Update Stable tag in readme.txt
70-
shell: bash
71-
if: ${{ steps.handle-version-input.outputs.channel == 'stable' }}
72-
run: |
73-
VERSION="${{ inputs.version }}"
74-
LAST_BETA_TAG=$(git ls-remote --tags | awk '{print $2}' | sed 's/^refs\/tags\/v//' | sed 's/^refs\/tags\///' | grep -E '^[0-9]+\.[0-9]+\.[0-9]-beta[1-9]$' | sort -V | tail -n 1)
75-
sed -i -E "s/Stable tag: .*/Stable tag: ${VERSION}/" readme.txt
76-
sed -i -E "s/Beta tag: .*/Beta tag: ${LAST_BETA_TAG}/" readme.txt
77-
echo "readme.txt stable tag updated:"
78-
grep -E "Stable tag:|Beta tag:" readme.txt
79-
80-
- name: Update Beta tag in readme.txt
81-
shell: bash
82-
if: ${{ steps.handle-version-input.outputs.channel == 'beta' }}
83-
run: |
84-
VERSION="${{ inputs.version }}"
85-
LAST_STABLE_TAG=$(git ls-remote --tags | awk '{print $2}' | sed 's/^refs\/tags\/v//' | sed 's/^refs\/tags\///' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1)
86-
sed -i -E "s/Stable tag: .*/Stable tag: ${LAST_STABLE_TAG}/" readme.txt
87-
sed -i -E "s/Beta tag: .*/Beta tag: ${VERSION}/" readme.txt
88-
echo "readme.txt beta tag updated:"
89-
grep -E "Stable tag:|Beta tag:" readme.txt
90-
91-
- name: Capture readme.txt values
58+
- name: Update version files
9259
id: capture-readme
9360
shell: bash
94-
run: |
95-
STABLE_TAG=$(grep -E "^Stable tag:" readme.txt | sed 's/Stable tag: //')
96-
BETA_TAG=$(grep -E "^Beta tag:" readme.txt | sed 's/Beta tag: //')
97-
echo "stable_tag=${STABLE_TAG}" >> "$GITHUB_OUTPUT"
98-
echo "beta_tag=${BETA_TAG}" >> "$GITHUB_OUTPUT"
61+
env:
62+
INPUT_VERSION: ${{ inputs.version }}
63+
INPUT_CHANNEL: ${{ steps.handle-version-input.outputs.channel }}
64+
run: node "${{ github.action_path }}/dist/update-version-files.js"
9965

10066
- name: Commit version bump
10167
shell: bash
@@ -114,13 +80,6 @@ runs:
11480
git commit -m "Bump version to ${VERSION}"
11581
echo "Committed version bump to ${VERSION}."
11682
117-
- name: Set update-version-in-tag-files outputs
118-
id: update-version-in-tag-files
119-
shell: bash
120-
run: |
121-
echo "readme_stable_tag=${{ steps.capture-readme.outputs.stable_tag }}" >> "$GITHUB_OUTPUT"
122-
echo "readme_beta_tag=${{ steps.capture-readme.outputs.beta_tag }}" >> "$GITHUB_OUTPUT"
123-
12483
- name: Create release tag
12584
id: create-release-tag
12685
shell: bash

actions/release-tag-creation/dist/index.js

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

actions/release-tag-creation/dist/update-version-files.js

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

actions/release-tag-creation/main.test.ts

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import { execSync } from 'node:child_process';
21
import { appendFileSync } from 'node:fs';
3-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { afterEach, describe, expect, it, vi } from 'vitest';
43

54
import {
6-
checkTagDoesNotExist,
75
deriveBranch,
86
extractChannel,
97
getVersion,
108
setOutput,
119
validateFormat,
1210
} from './main';
1311

14-
vi.mock('node:child_process');
1512
vi.mock('node:fs');
1613

17-
const mockExecSync = vi.mocked(execSync);
1814
const mockAppendFileSync = vi.mocked(appendFileSync);
1915

2016
// ─── getVersion ───────────────────────────────────────────────────────────────
@@ -88,42 +84,6 @@ describe('validateFormat', () => {
8884
});
8985
});
9086

91-
// ─── checkTagDoesNotExist ─────────────────────────────────────────────────────
92-
93-
describe('checkTagDoesNotExist', () => {
94-
beforeEach(() => {
95-
vi.clearAllMocks();
96-
});
97-
98-
it('does not throw when ls-remote returns empty (tag absent)', () => {
99-
mockExecSync.mockReturnValue('' as never);
100-
expect(() => { checkTagDoesNotExist('3.11.0'); }).not.toThrow();
101-
});
102-
103-
it('throws when ls-remote returns a line (tag exists)', () => {
104-
mockExecSync.mockReturnValue(
105-
'abc123\trefs/tags/3.11.0\n' as never,
106-
);
107-
expect(() => { checkTagDoesNotExist('3.11.0'); }).toThrow('already exists');
108-
});
109-
110-
it('throws when execSync itself fails', () => {
111-
mockExecSync.mockImplementation(() => {
112-
throw new Error('git: not found');
113-
});
114-
expect(() => { checkTagDoesNotExist('3.11.0'); }).toThrow('Failed to check remote tags');
115-
});
116-
117-
it('queries the exact ref for the given version', () => {
118-
mockExecSync.mockReturnValue('' as never);
119-
checkTagDoesNotExist('4.1.0-beta1');
120-
expect(mockExecSync).toHaveBeenCalledWith(
121-
expect.stringContaining('refs/tags/4.1.0-beta1'),
122-
expect.any(Object),
123-
);
124-
});
125-
});
126-
12787
// ─── extractChannel ───────────────────────────────────────────────────────────
12888

12989
describe('extractChannel', () => {

actions/release-tag-creation/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@actions/core": "^1.11.1",
14+
"@elementor/editor-github-actions-utils": "^1.0.0",
1415
"semver": "^7.7.2"
1516
},
1617
"devDependencies": {

actions/release-tag-creation/tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from 'tsup';
22

33
export default defineConfig({
4-
entry: ['index.ts'],
4+
entry: ['index.ts', 'update-version-files.ts'],
55
outDir: 'dist',
66
format: 'cjs',
77
noExternal: [/.+/],
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { execSync } from 'node:child_process';
2+
import { readFileSync, writeFileSync } from 'node:fs';
3+
import { appendFileSync } from 'node:fs';
4+
import {
5+
parseLatestTagFromLsRemote,
6+
patchPhpVersion,
7+
patchReadmeTxt,
8+
} from '@elementor/editor-github-actions-utils';
9+
10+
const STABLE_TAG_PATTERN = /^\d+\.\d+\.\d+$/;
11+
const BETA_TAG_PATTERN = /^\d+\.\d+\.\d+-beta\d+$/;
12+
13+
function getEnv(name: string): string {
14+
const value = process.env[name];
15+
if (!value) {
16+
throw new Error(`Missing required environment variable: ${name}`);
17+
}
18+
return value;
19+
}
20+
21+
function setOutput(name: string, value: string): void {
22+
const outputFile = process.env['GITHUB_OUTPUT'];
23+
if (outputFile) {
24+
appendFileSync(outputFile, `${name}=${value}\n`);
25+
} else {
26+
console.log(`OUTPUT ${name}=${value}`);
27+
}
28+
}
29+
30+
function fetchLsRemote(): string {
31+
return execSync('git ls-remote --tags', { encoding: 'utf8' });
32+
}
33+
34+
export function run(): void {
35+
try {
36+
const version = getEnv('INPUT_VERSION');
37+
const channel = getEnv('INPUT_CHANNEL');
38+
39+
// ── elementor.php ────────────────────────────────────────────────────────
40+
const phpPath = 'elementor.php';
41+
const patchedPhp = patchPhpVersion(readFileSync(phpPath, 'utf8'), version);
42+
writeFileSync(phpPath, patchedPhp, 'utf8');
43+
console.log(`✅ elementor.php patched to ${version}`);
44+
45+
// ── readme.txt ───────────────────────────────────────────────────────────
46+
const readmePath = 'readme.txt';
47+
const lsRemote = fetchLsRemote();
48+
49+
let readmeTags: { stable: string; beta: string };
50+
51+
if (channel === 'stable') {
52+
const lastBetaTag = parseLatestTagFromLsRemote(lsRemote, BETA_TAG_PATTERN);
53+
if (!lastBetaTag) {
54+
throw new Error('Could not find any existing beta tag in remote — cannot update readme.txt Beta tag.');
55+
}
56+
readmeTags = { stable: version, beta: lastBetaTag };
57+
} else {
58+
const lastStableTag = parseLatestTagFromLsRemote(lsRemote, STABLE_TAG_PATTERN);
59+
if (!lastStableTag) {
60+
throw new Error('Could not find any existing stable tag in remote — cannot update readme.txt Stable tag.');
61+
}
62+
readmeTags = { stable: lastStableTag, beta: version };
63+
}
64+
65+
const patchedReadme = patchReadmeTxt(readFileSync(readmePath, 'utf8'), readmeTags);
66+
writeFileSync(readmePath, patchedReadme, 'utf8');
67+
console.log(`✅ readme.txt patched — Stable: ${readmeTags.stable}, Beta: ${readmeTags.beta}`);
68+
69+
// ── outputs ──────────────────────────────────────────────────────────────
70+
setOutput('readme_stable_tag', readmeTags.stable);
71+
setOutput('readme_beta_tag', readmeTags.beta);
72+
} catch (err) {
73+
console.error(`\n::error::${(err as Error).message}\n`);
74+
process.exit(1);
75+
}
76+
}
77+
78+
run();

actions/setup-elementor-env/dist/index.js

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

package-lock.json

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

packages/editor-github-actions-utils/package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
"private": true,
99
"scripts": {
1010
"build": "tsup --config ./tsup.config.ts",
11-
"dev": "npm run build -- --watch"
11+
"dev": "npm run build -- --watch",
12+
"test": "vitest run",
13+
"test:watch": "vitest"
1214
},
1315
"dependencies": {
14-
"ansi-styles": "^6.2.1",
15-
"@actions/core": "^1.11.1"
16+
"@actions/core": "^1.11.1",
17+
"ansi-styles": "^6.2.1"
1618
},
1719
"devDependencies": {
18-
"tsup": "^8.5.0"
20+
"tsup": "^8.5.0",
21+
"vitest": "^4.1.9"
1922
}
2023
}

0 commit comments

Comments
 (0)