-
Notifications
You must be signed in to change notification settings - Fork 7
Add preview and publication URL support to DAK LM and JSON generation #174
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
Merged
Merged
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
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
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,128 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| DAK URL Utilities | ||
|
|
||
| Shared utilities for generating publication and preview URLs for DAK-enabled repositories. | ||
| These utilities are used by PR comment scripts and other build tools to determine | ||
| the appropriate URLs based on repository configuration and branch context. | ||
| """ | ||
|
|
||
| import json | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Dict, Any, Optional, Tuple | ||
|
|
||
|
|
||
| def load_dak_config(dak_path: Path = None) -> Optional[Dict[str, Any]]: | ||
| """Load dak.json configuration if it exists.""" | ||
| if dak_path is None: | ||
| dak_path = Path("dak.json") | ||
|
|
||
| if not dak_path.exists(): | ||
| return None | ||
|
|
||
| try: | ||
| with open(dak_path, 'r', encoding='utf-8') as file: | ||
| return json.load(file) | ||
| except (json.JSONDecodeError, IOError): | ||
| return None | ||
|
|
||
|
|
||
| def generate_dak_publication_url(repo_name: str, canonical_url: str = "") -> str: | ||
| """Generate publication URL based on repository ownership and name.""" | ||
| # Check if this is a WorldHealthOrganization repository | ||
| github_repo = os.getenv('GITHUB_REPOSITORY', '') | ||
| if github_repo.startswith('WorldHealthOrganization/'): | ||
| # Extract stub by removing 'smart-' prefix if present | ||
| stub = repo_name | ||
| if stub.startswith('smart-'): | ||
| stub = stub[6:] # Remove 'smart-' prefix | ||
| return f"https://smart.who.int/{stub}" | ||
| else: | ||
| # For non-WHO repositories, use canonical URL or default pattern | ||
| if canonical_url: | ||
| return canonical_url | ||
| # Fallback to GitHub Pages pattern | ||
| if github_repo: | ||
| profile, repo = github_repo.split('/') | ||
| return f"https://{profile}.github.io/{repo}" | ||
| return canonical_url or "" | ||
|
|
||
|
|
||
| def generate_dak_preview_url(repo_name: str = "") -> str: | ||
| """Generate preview URL for current CI build.""" | ||
| github_repo = os.getenv('GITHUB_REPOSITORY', '') | ||
| if github_repo: | ||
| profile, repo = github_repo.split('/') | ||
| return f"https://{profile}.github.io/{repo}" | ||
| # Fallback for local development | ||
| return f"https://worldhealthorganization.github.io/{repo_name}" | ||
|
|
||
|
|
||
| def is_release_branch() -> bool: | ||
| """Check if current branch is a release branch (prefixed with 'release-').""" | ||
| branch_name = os.getenv('GITHUB_REF_NAME', os.getenv('BRANCH_NAME', '')) | ||
| return branch_name.startswith('release-') | ||
|
|
||
|
|
||
| def get_deployment_urls(branch: str, repository: str = "") -> Tuple[str, str]: | ||
| """ | ||
| Get appropriate deployment URLs based on DAK configuration and branch context. | ||
|
|
||
| Returns: | ||
| Tuple[str, str]: (deployment_url, base_url) where: | ||
| - deployment_url: The URL for the specific branch deployment | ||
| - base_url: The base URL for the repository | ||
| """ | ||
| # Load DAK configuration if available | ||
| dak_config = load_dak_config() | ||
|
|
||
| if dak_config: | ||
| # Use DAK-specific URL logic | ||
| repo_name = repository.split('/')[-1] if repository else "" | ||
|
|
||
| if is_release_branch(): | ||
| # For release branches, use publication URL as base | ||
| base_url = dak_config.get('publicationUrl', generate_dak_publication_url(repo_name)) | ||
| else: | ||
| # For non-release branches, use preview URL as base | ||
| base_url = dak_config.get('previewUrl', generate_dak_preview_url(repo_name)) | ||
|
|
||
| # Generate branch-specific URL | ||
| if branch == 'main': | ||
| deployment_url = base_url | ||
| else: | ||
| # Extract branch suffix for URL | ||
| branch_for_url = branch.split('/')[-1] if '/' in branch else branch | ||
| deployment_url = f"{base_url.rstrip('/')}/branches/{branch_for_url}" | ||
| else: | ||
| # Fallback to GitHub Pages pattern for non-DAK repositories | ||
| github_repo = repository or os.getenv('GITHUB_REPOSITORY', '') | ||
| if github_repo: | ||
| profile, repo = github_repo.split('/') | ||
| base_url = f"https://{profile}.github.io/{repo}" | ||
| else: | ||
| base_url = "https://worldhealthorganization.github.io/smart-base" | ||
|
|
||
| if branch == 'main': | ||
| deployment_url = f"{base_url}/" | ||
| else: | ||
| branch_for_url = branch.split('/')[-1] if '/' in branch else branch | ||
| deployment_url = f"{base_url}/branches/{branch_for_url}/" | ||
|
|
||
| return deployment_url, base_url | ||
|
|
||
|
|
||
| def get_canonical_url_for_branch(branch: str, repository: str = "") -> str: | ||
| """Get the canonical URL that should be used for the given branch.""" | ||
| dak_config = load_dak_config() | ||
|
|
||
| if dak_config: | ||
| if is_release_branch(): | ||
| return dak_config.get('publicationUrl', dak_config.get('canonicalUrl', '')) | ||
| else: | ||
| return dak_config.get('previewUrl', dak_config.get('canonicalUrl', '')) | ||
| else: | ||
| # Fallback for non-DAK repositories | ||
| deployment_url, _ = get_deployment_urls(branch, repository) | ||
| return deployment_url | ||
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 |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| """ | ||
|
|
||
| import json | ||
| import os | ||
| import sys | ||
| import yaml | ||
| from pathlib import Path | ||
|
|
@@ -48,20 +49,77 @@ def convert_publisher(sushi_publisher: Any) -> Dict[str, str]: | |
| return {"name": ""} | ||
|
|
||
|
|
||
| def generate_publication_url(repo_name: str, canonical_url: str) -> str: | ||
| """Generate publication URL based on repository ownership and name.""" | ||
| # Check if this is a WorldHealthOrganization repository | ||
| if os.getenv('GITHUB_REPOSITORY', '').startswith('WorldHealthOrganization/'): | ||
|
||
| # Extract stub by removing 'smart-' prefix if present | ||
| stub = repo_name | ||
| if stub.startswith('smart-'): | ||
| stub = stub[6:] # Remove 'smart-' prefix | ||
| return f"https://smart.who.int/{stub}" | ||
| else: | ||
| # For non-WHO repositories, use canonical URL or default pattern | ||
| if canonical_url: | ||
| return canonical_url | ||
| # Fallback to GitHub Pages pattern | ||
| github_repo = os.getenv('GITHUB_REPOSITORY', '') | ||
| if github_repo: | ||
| return f"https://{github_repo.split('/')[0]}.github.io/{github_repo.split('/')[1]}" | ||
| return canonical_url or "" | ||
|
|
||
|
|
||
| def generate_preview_url(repo_name: str) -> str: | ||
| """Generate preview URL for current CI build.""" | ||
| github_repo = os.getenv('GITHUB_REPOSITORY', '') | ||
| if github_repo: | ||
| profile, repo = github_repo.split('/') | ||
| return f"https://{profile}.github.io/{repo}" | ||
| # Fallback for local development | ||
| return f"https://worldhealthorganization.github.io/{repo_name}" | ||
|
|
||
|
|
||
| def is_release_branch() -> bool: | ||
| """Check if current branch is a release branch (prefixed with 'release-').""" | ||
| branch_name = os.getenv('GITHUB_REF_NAME', os.getenv('BRANCH_NAME', '')) | ||
| return branch_name.startswith('release-') | ||
|
|
||
|
|
||
| def generate_dak_json(sushi_config: Dict[str, Any]) -> Dict[str, Any]: | ||
| """Generate dak.json structure from sushi-config.yaml.""" | ||
|
|
||
| # Extract repository information | ||
| repo_id = sushi_config.get("id", "") | ||
| repo_name = repo_id.split('.')[-1] if '.' in repo_id else repo_id | ||
| canonical_url = sushi_config.get("canonical", "") | ||
|
|
||
| # Generate URLs based on branch type and repository ownership | ||
| if is_release_branch(): | ||
| # For release branches, use publication URL for canonical references | ||
| publication_url = generate_publication_url(repo_name, canonical_url) | ||
| preview_url = generate_preview_url(repo_name) | ||
| # Use publication URL as canonical URL for release branches | ||
| effective_canonical = publication_url | ||
| else: | ||
| # For non-release branches, use preview URL | ||
| publication_url = generate_publication_url(repo_name, canonical_url) | ||
| preview_url = generate_preview_url(repo_name) | ||
| # Use preview URL as canonical URL for development branches | ||
| effective_canonical = preview_url | ||
|
|
||
| # Core DAK identity (mapped from sushi config) | ||
| dak = { | ||
| "resourceType": "DAK", | ||
| "resourceDefinition": "http://smart.who.int/base/StructureDefinition/DAK", | ||
| "id": sushi_config.get("id", ""), | ||
| "id": repo_id, | ||
| "name": sushi_config.get("name", ""), | ||
| "title": sushi_config.get("title", ""), | ||
| "description": sushi_config.get("description", ""), | ||
| "version": sushi_config.get("version", "0.1.0"), | ||
| "status": sushi_config.get("status", "draft"), | ||
| "publicationUrl": sushi_config.get("canonical", ""), | ||
| "publicationUrl": publication_url, | ||
| "previewUrl": preview_url, | ||
| "canonicalUrl": effective_canonical, | ||
| "license": sushi_config.get("license", "CC0-1.0"), | ||
| "copyrightYear": sushi_config.get("copyrightYear", str(datetime.now().year)), | ||
| "publisher": convert_publisher(sushi_config.get("publisher", {})) | ||
|
|
@@ -103,6 +161,9 @@ def main(): | |
| print(f"DAK ID: {dak_config['id']}") | ||
| print(f"DAK Title: {dak_config['title']}") | ||
| print(f"Publication URL: {dak_config['publicationUrl']}") | ||
| print(f"Preview URL: {dak_config['previewUrl']}") | ||
| print(f"Canonical URL: {dak_config['canonicalUrl']}") | ||
| print(f"Is Release Branch: {is_release_branch()}") | ||
| except IOError as e: | ||
| print(f"Error writing output file: {e}") | ||
| sys.exit(1) | ||
|
|
||
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
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.
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.
The hardcoded fallback URL should be extracted to a constant or made configurable to avoid magic strings in the code.