Skip to content

Commit fbf0d12

Browse files
committed
Fixes
1 parent 46df1e5 commit fbf0d12

5 files changed

Lines changed: 47 additions & 28 deletions

File tree

Binary file not shown.

.github/scripts/check-wordpress-tested-up-to.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33

44
from __future__ import annotations
55

6+
import http.client
67
import json
78
import os
89
import re
9-
import subprocess
1010
import sys
11-
import urllib.request
1211
from pathlib import Path
1312

1413

15-
WORDPRESS_VERSION_CHECK_URL = os.environ.get(
16-
"WORDPRESS_VERSION_CHECK_URL",
17-
"https://api.wordpress.org/core/version-check/1.7/",
18-
)
14+
WORDPRESS_API_HOST = "api.wordpress.org"
15+
WORDPRESS_VERSION_CHECK_PATH = "/core/version-check/1.7/"
1916
WORDPRESS_LATEST_VERSION = os.environ.get("WORDPRESS_LATEST_VERSION")
2017
SCAN_EXTENSIONS = {".php", ".md", ".txt"}
2118
DEFAULT_EXCLUDED_DIRS = {
@@ -25,6 +22,7 @@
2522
"dist",
2623
"node_modules",
2724
"plugin-check-build",
25+
"__pycache__",
2826
"vendor",
2927
}
3028
TESTED_UP_TO_PATTERN = re.compile(
@@ -41,7 +39,7 @@ def main() -> int:
4139
findings = find_tested_up_to_entries(excluded_dirs)
4240

4341
if not findings:
44-
message = "No Tested up to metadata was found in tracked PHP, Markdown, or text files."
42+
message = "No Tested up to metadata was found in PHP, Markdown, or text files."
4543
print_github_error(message)
4644
write_summary(latest_version, [], [message])
4745
return 1
@@ -85,13 +83,26 @@ def get_latest_wordpress_major_minor() -> str:
8583
if WORDPRESS_LATEST_VERSION:
8684
return normalize_major_minor(WORDPRESS_LATEST_VERSION)
8785

88-
request = urllib.request.Request(
89-
WORDPRESS_VERSION_CHECK_URL,
90-
headers={"User-Agent": "wordpress-tested-up-to-check/1.0"},
86+
connection = http.client.HTTPSConnection(WORDPRESS_API_HOST, timeout=20)
87+
connection.request(
88+
"GET",
89+
WORDPRESS_VERSION_CHECK_PATH,
90+
headers={
91+
"Accept": "application/json",
92+
"User-Agent": "wordpress-tested-up-to-check/1.0",
93+
},
9194
)
9295

93-
with urllib.request.urlopen(request, timeout=20) as response:
94-
payload = json.load(response)
96+
response = connection.getresponse()
97+
try:
98+
if response.status != http.client.OK:
99+
raise RuntimeError(
100+
f"WordPress version-check API returned HTTP {response.status}."
101+
)
102+
103+
payload = json.loads(response.read().decode("utf-8"))
104+
finally:
105+
connection.close()
95106

96107
versions = []
97108
for offer in payload.get("offers", []):
@@ -130,7 +141,7 @@ def find_tested_up_to_entries(
130141
) -> list[dict[str, str | int | None]]:
131142
findings = []
132143

133-
for path in get_tracked_files():
144+
for path in get_scanned_files(excluded_dirs):
134145
if not should_scan(path, excluded_dirs):
135146
continue
136147

@@ -152,16 +163,24 @@ def find_tested_up_to_entries(
152163
return findings
153164

154165

155-
def get_tracked_files() -> list[Path]:
156-
result = subprocess.run(
157-
["git", "ls-files", "*.php", "*.md", "*.txt"],
158-
check=True,
159-
stdout=subprocess.PIPE,
160-
stderr=subprocess.PIPE,
161-
text=True,
162-
)
166+
def get_scanned_files(excluded_dirs: set[str]) -> list[Path]:
167+
root = Path.cwd()
168+
paths = []
169+
170+
for current_dir, dirnames, filenames in os.walk(root):
171+
dirnames[:] = [
172+
dirname for dirname in dirnames if dirname not in excluded_dirs
173+
]
174+
175+
current_path = Path(current_dir)
176+
for filename in filenames:
177+
path = current_path / filename
178+
relative_path = path.relative_to(root)
179+
180+
if should_scan(relative_path, excluded_dirs):
181+
paths.append(relative_path)
163182

164-
return [Path(line) for line in result.stdout.splitlines() if line.strip()]
183+
return sorted(paths)
165184

166185

167186
def should_scan(path: Path, excluded_dirs: set[str]) -> bool:

.github/workflows/wordpress-tested-up-to.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
name: WordPress Tested Up To
66

77
on:
8-
push:
9-
branches: [ main ]
10-
pull_request:
118
schedule:
129
- cron: '23 12 * * *'
13-
workflow_dispatch:
1410

1511
concurrency:
1612
group: ${{ github.workflow }}-${{ github.ref }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414
/.phpcs-cache
1515
/.phpstan.cache/
1616
/.psalm/cache/
17+
18+
# Python tooling caches
19+
__pycache__/
20+
*.py[cod]

tests/unit/DomainValidationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function test_resource_hints_use_wordpress_filter_contract(): void {
192192

193193
$preconnect_hints = es_optimizer_add_preconnect_resource_hints( array(), 'preconnect' );
194194
$dns_prefetch_urls = es_optimizer_add_dns_prefetch_resource_hints( array(), 'dns-prefetch' );
195-
$cdn_hint = $this->findResourceHintByHref( $preconnect_hints, 'https://cdn.example.com' );
195+
$cdn_hint = $this->find_resource_hint_by_href( $preconnect_hints, 'https://cdn.example.com' );
196196

197197
$this->assertContains( array( 'href' => 'https://fonts.gstatic.com', 'crossorigin' => 'anonymous' ), $preconnect_hints );
198198
$this->assertIsArray( $cdn_hint );
@@ -235,7 +235,7 @@ public function test_resource_hints_are_not_added_when_feature_flags_are_disable
235235
* @param string $href Hint href to locate.
236236
* @return array<string, mixed>|null Matching hint, if present.
237237
*/
238-
private function findResourceHintByHref( array $hints, string $href ): ?array {
238+
private function find_resource_hint_by_href( array $hints, string $href ): ?array {
239239
foreach ( $hints as $hint ) {
240240
if ( is_array( $hint ) && $href === ( $hint['href'] ?? null ) ) {
241241
return $hint;

0 commit comments

Comments
 (0)