GeezerVison is showing github.com — feat: fetch for default branch (#1031) · googleapis/synthtool@898b38a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: synthtool/gcp/common.py +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | from copy import deepcopy | 21 | 21 | from pathlib import Path | 22 | 22 | from typing import Dict, List, Optional | | 23 | +import requests | 23 | 24 | import jinja2 | 24 | 25 |
| 25 | 26 | from synthtool import shell, _tracked_paths | @@ -59,6 +60,11 @@ def _generic_library(self, directory: str, **kwargs) -> Path: | 59 | 60 |
| 60 | 61 | t = templates.TemplateGroup(self._template_root / directory, self.excludes) | 61 | 62 |
| | 63 | + if "repository" in kwargs["metadata"] and "repo" in kwargs["metadata"]: | | 64 | + kwargs["metadata"]["repo"]["default_branch"] = _get_default_branch_name( | | 65 | + kwargs["metadata"]["repository"] | | 66 | + ) | | 67 | + | 62 | 68 | # TODO: migrate to python.py once old sample gen is deprecated | 63 | 69 | if directory == "python_samples": | 64 | 70 | t.env.globals["get_help"] = lambda filename: shell.run( | @@ -361,3 +367,10 @@ def _load_repo_metadata(metadata_file: str = "./.repo-metadata.json") -> Dict: | 361 | 367 | with open(metadata_file) as f: | 362 | 368 | return json.load(f) | 363 | 369 | return {} | | 370 | + | | 371 | + | | 372 | +def _get_default_branch_name(repository_name: str) -> str: | | 373 | + github_req = requests.get(f"https://api.github.com/repos/{repository_name}") | | 374 | + github_req.raise_for_status() | | 375 | + | | 376 | + return github_req.json()["default_branch"] |
Collapse file Copy file name to clipboardExpand all lines: synthtool/gcp/templates/node_library/.github/workflows/ci.yaml +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | on: | 2 | 2 | push: | 3 | 3 | branches: | 4 | | - - master | | 4 | + - default-branch | 5 | 5 | pull_request: | 6 | 6 | name: ci | 7 | 7 | jobs: | |
Collapse file Copy file name to clipboardExpand all lines: synthtool/gcp/templates/node_library/README.md +5-5Lines changed: 5 additions & 5 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 25 | 25 | {% endif %} | 26 | 26 |
| 27 | 27 | A comprehensive list of changes in each version may be found in | 28 | | -[the CHANGELOG](https://github.com/{{ metadata['repo']['repo'] }}/blob/master/CHANGELOG.md). | | 28 | +[the CHANGELOG](https://github.com/{{ metadata['repo']['repo'] }}/blob/{{metadata['repo']['default_branch']}}/CHANGELOG.md). | 29 | 29 |
| 30 | 30 | {% if metadata['repo']['client_documentation'] %}* [{{ metadata['repo']['name_pretty'] }} {{ metadata['repo']['language']|language_pretty }} Client API Reference][client-docs]{% endif %} | 31 | 31 | {% if metadata['repo']['product_documentation'] %}* [{{ metadata['repo']['name_pretty'] }} Documentation][product-docs]{% endif %} | @@ -75,11 +75,11 @@ Google APIs Client Libraries, in [Client Libraries Explained][explained]. | 75 | 75 | {% if metadata['samples']|length %} | 76 | 76 | ## Samples | 77 | 77 |
| 78 | | -Samples are in the [`samples/`](https://github.com/{{ metadata['repo']['repo'] }}/tree/master/samples) directory. Each sample's `README.md` has instructions for running its sample. | | 78 | +Samples are in the [`samples/`](https://github.com/{{ metadata['repo']['repo'] }}/tree/{{ metadata['repo']['default_branch'] }}/samples) directory. Each sample's `README.md` has instructions for running its sample. | 79 | 79 |
| 80 | 80 | | Sample | Source Code | Try it | | 81 | 81 | | --------------------------- | --------------------------------- | ------ | | 82 | | -{% for sample in metadata['samples'] %}| {{ sample.title }} | [source code](https://github.com/{{ metadata['repo']['repo'] }}/blob/master/{{ sample.file }}) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/{{ metadata['repo']['repo'] }}&page=editor&open_in_editor={{ sample.file }},samples/README.md) | | | 82 | +{% for sample in metadata['samples'] %}| {{ sample.title }} | [source code](https://github.com/{{ metadata['repo']['repo'] }}/blob/{{ metadata['repo']['default_branch'] }}/{{ sample.file }}) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/{{ metadata['repo']['repo'] }}&page=editor&open_in_editor={{ sample.file }},samples/README.md) | | 83 | 83 | {% endfor %} | 84 | 84 | {% endif %} | 85 | 85 | {% if metadata['repo']['client_documentation'] %} | @@ -141,7 +141,7 @@ More Information: [Google Cloud Platform Launch Stages][launch_stages] | 141 | 141 |
| 142 | 142 | ## Contributing | 143 | 143 |
| 144 | | -Contributions welcome! See the [Contributing Guide](https://github.com/{{ metadata['repo']['repo'] }}/blob/master/CONTRIBUTING.md). | | 144 | +Contributions welcome! See the [Contributing Guide](https://github.com/{{ metadata['repo']['repo'] }}/blob/{{ metadata['repo']['default_branch'] }}/CONTRIBUTING.md). | 145 | 145 |
| 146 | 146 | Please note that this `README.md`, the `samples/README.md`, | 147 | 147 | and a variety of configuration files in this repository (including `.nycrc` and `tsconfig.json`) | @@ -153,7 +153,7 @@ to its template in this | 153 | 153 |
| 154 | 154 | Apache Version 2.0 | 155 | 155 |
| 156 | | -See [LICENSE](https://github.com/{{ metadata['repo']['repo'] }}/blob/master/LICENSE) | | 156 | +See [LICENSE](https://github.com/{{ metadata['repo']['repo'] }}/blob/{{ metadata['repo']['default_branch'] }}/LICENSE) | 157 | 157 |
| 158 | 158 | {% if metadata['repo']['client_documentation'] %}[client-docs]: {{ metadata['repo']['client_documentation'] }}{% endif %} | 159 | 159 | {% if metadata['repo']['product_documentation'] %}[product-docs]: {{ metadata['repo']['product_documentation'] }}{% endif %} | |
Collapse file Copy file name to clipboardExpand all lines: synthtool/gcp/templates/node_library/samples/README.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -45,7 +45,7 @@ Before running the samples, make sure you've followed the steps outlined in | 45 | 45 |
| 46 | 46 | {%- endif %} | 47 | 47 |
| 48 | | -View the [source code](https://github.com/{{ metadata['repo']['repo'] }}/blob/master/{{ sample.file }}). | | 48 | +View the [source code](https://github.com/{{ metadata['repo']['repo'] }}/blob/{{ metadata['repo']['default_branch'] }}/{{ sample.file }}). | 49 | 49 |
| 50 | 50 | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/{{ metadata['repo']['repo'] }}&page=editor&open_in_editor={{ sample.file }},samples/README.md) | 51 | 51 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/test_common.py +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | # See the License for the specific language governing permissions and | 13 | 13 | # limitations under the License. | 14 | 14 |
| 15 | | -from synthtool.gcp.common import decamelize | | 15 | +from synthtool.gcp.common import decamelize, _get_default_branch_name | 16 | 16 | from pathlib import Path | 17 | 17 | from pytest import raises | 18 | 18 | import os | | 19 | +import requests_mock | 19 | 20 | import synthtool as s | 20 | 21 | import tempfile | 21 | 22 | import shutil | 22 | 23 |
| | 24 | + | 23 | 25 | MOCK = Path(__file__).parent / "generationmock" | 24 | 26 | template_dir = Path(__file__).parent.parent / "synthtool/gcp/templates" | 25 | 27 | common = s.gcp.CommonTemplates(template_path=template_dir) | @@ -41,6 +43,14 @@ def test_handles_empty_string(): | 41 | 43 | assert decamelize("") == "" | 42 | 44 |
| 43 | 45 |
| | 46 | +def test_get_default_branch(): | | 47 | + with requests_mock.Mocker() as m: | | 48 | + m.get( | | 49 | + "https://api.github.com/repos/repo_name", text='{"default_branch": "main"}', | | 50 | + ) | | 51 | + assert _get_default_branch_name("repo_name") == "main" | | 52 | + | | 53 | + | 44 | 54 | def test_py_samples_clientlib(): | 45 | 55 | path_to_gen = MOCK / "client_library" | 46 | 56 | with tempfile.TemporaryDirectory() as tempdir: | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_transforms.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -281,4 +281,6 @@ def change_test_dir(): | 281 | 281 | def test_get_staging_dirs(change_test_dir): | 282 | 282 | assert [path.name for path in transforms.get_staging_dirs("v1")] == ["v2", "v1"] | 283 | 283 | assert [path.name for path in transforms.get_staging_dirs("v2")] == ["v1", "v2"] | | 284 | + paths = [path.name for path in transforms.get_staging_dirs()] | | 285 | + paths.sort() | 284 | 286 | assert [path.name for path in transforms.get_staging_dirs()] == ["v1", "v2"] |
|