-
-
Notifications
You must be signed in to change notification settings - Fork 471
[DNM][Do not review] Test GitHub workflow for SDK names #5588
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
Closed
+462
−0
Closed
Changes from all commits
Commits
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
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,158 @@ | ||
| # When sdk.name values are added, removed, or updated in Config.kt, posts a PR comment | ||
| # reminding authors to update the sdk_map spreadsheet for Looker/Hex reporting. | ||
| # Warn-only: does not block merge. See scripts/detect_sdk_name_changes.py. | ||
| # Fork PRs cannot post comments (read-only GITHUB_TOKEN on pull_request). | ||
| name: 'SDK Name Check' | ||
| run-name: sdk.name change check (Config.kt) | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| files-changed: | ||
| name: Detect changed files | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| config_kt: ${{ steps.changes.outputs.config_kt }} | ||
| sdk_name_check_scripts: ${{ steps.changes.outputs.sdk_name_check_scripts }} | ||
| steps: | ||
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | ||
| - name: Get changed files | ||
| id: changes | ||
| uses: dorny/paths-filter@fbd0ab8f3e69293af611ebaee6363fc25e6d187d # v4.0.1 | ||
| with: | ||
| token: ${{ github.token }} | ||
| filters: .github/file-filters.yml | ||
|
|
||
| sdk-name-check: | ||
| name: SDK name check | ||
| if: needs.files-changed.outputs.config_kt == 'true' || needs.files-changed.outputs.sdk_name_check_scripts == 'true' | ||
| needs: files-changed | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| pull-requests: write | ||
| steps: | ||
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Run detector unit tests | ||
| run: python3 -m unittest discover -s scripts -p 'test_detect_sdk_name_changes.py' | ||
|
|
||
| - name: Detect SDK name changes | ||
| if: needs.files-changed.outputs.config_kt == 'true' | ||
| continue-on-error: true | ||
| id: detect | ||
| run: | | ||
| git fetch --no-tags origin \ | ||
| "${{ github.event.pull_request.base.sha }}" \ | ||
| "${{ github.event.pull_request.head.sha }}" | ||
| changes=$(python3 scripts/detect_sdk_name_changes.py \ | ||
| --base "${{ github.event.pull_request.base.sha }}" \ | ||
| --head "${{ github.event.pull_request.head.sha }}") | ||
| echo "changes=${changes}" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Update PR comment | ||
| if: needs.files-changed.outputs.config_kt == 'true' && (steps.detect.outcome == 'success' || steps.detect.outcome == 'failure') | ||
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | ||
| env: | ||
| SDK_NAME_CHANGES: ${{ steps.detect.outputs.changes }} | ||
| DETECT_OUTCOME: ${{ steps.detect.outcome }} | ||
| with: | ||
| script: | | ||
| const marker = '<!-- sdk-name-check -->'; | ||
|
|
||
| const comments = await github.paginate(github.rest.issues.listComments, { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| }); | ||
|
|
||
| const existing = comments.find( | ||
| (comment) => | ||
| comment.body?.includes(marker) && | ||
| comment.user?.type === 'Bot', | ||
| ); | ||
|
|
||
| const upsertComment = async (body) => { | ||
| if (existing) { | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: existing.id, | ||
| body, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| body, | ||
| }); | ||
| }; | ||
|
|
||
| if (process.env.DETECT_OUTCOME === 'failure') { | ||
| await upsertComment( | ||
| [ | ||
| '### ⚠️ SDK name check failed', | ||
| '', | ||
| 'Could not compare `*SDK_NAME` declarations in `Config.kt`. Use `val FOO_SDK_NAME = "..."` with an optional `$OTHER_SDK_NAME` prefix.', | ||
| '', | ||
| 'See the **Detect SDK name changes** step logs for details.', | ||
| '', | ||
| marker, | ||
| ].join('\n'), | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const changes = JSON.parse(process.env.SDK_NAME_CHANGES || '{}'); | ||
| const added = changes.added || []; | ||
| const removed = changes.removed || []; | ||
| const sheetUrl = changes.sdk_map_sheet_url; | ||
|
|
||
| if (added.length === 0 && removed.length === 0) { | ||
| if (existing) { | ||
| await github.rest.issues.deleteComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: existing.id, | ||
| }); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const formatList = (names) => names.map((name) => `- \`${name}\``).join('\n'); | ||
| const sections = ['### ⚠️ SDK name changes detected', '']; | ||
|
|
||
| if (added.length > 0) { | ||
| sections.push( | ||
| 'This PR adds new `sdk.name` value(s) that will appear in production telemetry:', | ||
| '', | ||
| formatList(added), | ||
| '', | ||
| `Please add them to the [sdk_map spreadsheet](${sheetUrl}) so Looker/Hex reporting stays accurate.`, | ||
| '', | ||
| ); | ||
| } | ||
|
|
||
| if (removed.length > 0) { | ||
| sections.push( | ||
| 'This PR removes `sdk.name` value(s) from production telemetry:', | ||
| '', | ||
| formatList(removed), | ||
| '', | ||
| `Please remove them from the [sdk_map spreadsheet](${sheetUrl}).`, | ||
| '', | ||
| ); | ||
| } | ||
|
|
||
| sections.push(marker); | ||
| await upsertComment(sections.join('\n')); | ||
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,132 @@ | ||
| #!/usr/bin/env python3 | ||
| """Detect added and removed sdk.name values in Config.kt between two revisions.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| import re | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| CONFIG_PATH = "buildSrc/src/main/java/Config.kt" | ||
| SDK_MAP_SHEET_URL = ( | ||
| "https://docs.google.com/spreadsheets/d/1hqFhytQuHMvuOz1XD0kCXg6x0ViflHrpjW7nNhvzYmU/" | ||
| "edit?gid=334165604" | ||
| ) | ||
| SDK_NAME_PATTERN = re.compile(r"^\s*val (\w+SDK_NAME) = (.+)$") | ||
| STRING_LITERAL = re.compile(r'^"((?:[^"\\]|\\.)*)"$') | ||
| INTERPOLATION_PATTERN = re.compile(r"\$([A-Za-z0-9_]+)") | ||
|
|
||
|
|
||
| def parse_sdk_constants(content: str) -> dict[str, str]: | ||
| """Return mapping of constant name to resolved sdk.name string.""" | ||
| resolved: dict[str, str] = {} | ||
| for line in content.splitlines(): | ||
| match = SDK_NAME_PATTERN.match(line) | ||
| if not match: | ||
| continue | ||
| name, rhs = match.group(1), match.group(2).strip() | ||
| resolved[name] = resolve_rhs(rhs, resolved) | ||
| return resolved | ||
|
|
||
|
|
||
| def resolve_rhs(rhs: str, resolved: dict[str, str]) -> str: | ||
| literal = STRING_LITERAL.match(rhs) | ||
| if not literal: | ||
| raise ValueError(f"Cannot parse SDK name assignment: {rhs}") | ||
| return expand_interpolation(literal.group(1), resolved) | ||
|
|
||
|
|
||
| def expand_interpolation(value: str, resolved: dict[str, str]) -> str: | ||
| if "${" in value: | ||
| raise ValueError(f"Unsupported brace interpolation in SDK name value: {value}") | ||
|
|
||
| def replace(match: re.Match[str]) -> str: | ||
| name = match.group(1) | ||
| if name not in resolved: | ||
| raise ValueError(f"Unknown SDK name constant: {name}") | ||
| return resolved[name] | ||
|
|
||
| return INTERPOLATION_PATTERN.sub(replace, value) | ||
|
|
||
|
|
||
| def find_sdk_name_changes( | ||
| base: dict[str, str], head: dict[str, str] | ||
| ) -> tuple[list[str], list[str]]: | ||
| base_values = set(base.values()) | ||
| head_values = set(head.values()) | ||
| added = sorted(head_values - base_values) | ||
| removed = sorted(base_values - head_values) | ||
| return added, removed | ||
|
|
||
|
|
||
| def git_show(ref: str, path: str) -> str: | ||
| try: | ||
| return subprocess.check_output( | ||
| ["git", "show", f"{ref}:{path}"], | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| text=True, | ||
| stderr=subprocess.PIPE, | ||
| ) | ||
| except subprocess.CalledProcessError as error: | ||
| if error.returncode == 128 and "exists on disk, but not in" in error.stderr: | ||
| return "" | ||
| raise | ||
|
|
||
|
|
||
| def read_config_source(base: str | None, head: str | None, config_path: str) -> tuple[str, str]: | ||
| if base is None or head is None: | ||
| raise ValueError("Both base and head refs are required") | ||
| return git_show(base, config_path), git_show(head, config_path) | ||
|
|
||
|
|
||
| def format_changes(added: list[str], removed: list[str]) -> str: | ||
| return json.dumps( | ||
| { | ||
| "added": added, | ||
| "removed": removed, | ||
| "sdk_map_sheet_url": SDK_MAP_SHEET_URL, | ||
| }, | ||
| separators=(",", ":"), | ||
| ) | ||
|
|
||
|
|
||
| def main(argv: list[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser( | ||
| description="Print added and removed sdk.name values as JSON." | ||
| ) | ||
| parser.add_argument("--base", help="Git ref for the PR base revision") | ||
| parser.add_argument("--head", help="Git ref for the PR head revision") | ||
| parser.add_argument("--base-file", type=Path, help="Base Config.kt file") | ||
| parser.add_argument("--head-file", type=Path, help="Head Config.kt file") | ||
| parser.add_argument("--config-path", default=CONFIG_PATH) | ||
| args = parser.parse_args(argv) | ||
|
|
||
| try: | ||
| if args.base_file and args.head_file: | ||
| base_content = args.base_file.read_text(encoding="utf-8") | ||
| head_content = args.head_file.read_text(encoding="utf-8") | ||
| elif args.base and args.head: | ||
| base_content, head_content = read_config_source( | ||
| args.base, args.head, args.config_path | ||
| ) | ||
| else: | ||
| parser.error("Provide --base/--head or --base-file/--head-file") | ||
|
|
||
| base_sdk = parse_sdk_constants(base_content) | ||
| head_sdk = parse_sdk_constants(head_content) | ||
| except ValueError as error: | ||
| print(f"error: {error}", file=sys.stderr) | ||
| return 1 | ||
| except subprocess.CalledProcessError as error: | ||
| print(f"error: git command failed: {' '.join(error.cmd)}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| added, removed = find_sdk_name_changes(base_sdk, head_sdk) | ||
| print(format_changes(added, removed)) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.