Skip to content

Commit 898b38a

Browse files
feat: fetch for default branch (#1031)
1 parent 1657dd8 commit 898b38a

6 files changed

Lines changed: 33 additions & 8 deletions

File tree

synthtool/gcp/common.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from copy import deepcopy
2121
from pathlib import Path
2222
from typing import Dict, List, Optional
23+
import requests
2324
import jinja2
2425

2526
from synthtool import shell, _tracked_paths
@@ -59,6 +60,11 @@ def _generic_library(self, directory: str, **kwargs) -> Path:
5960

6061
t = templates.TemplateGroup(self._template_root / directory, self.excludes)
6162

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+
6268
# TODO: migrate to python.py once old sample gen is deprecated
6369
if directory == "python_samples":
6470
t.env.globals["get_help"] = lambda filename: shell.run(
@@ -361,3 +367,10 @@ def _load_repo_metadata(metadata_file: str = "./.repo-metadata.json") -> Dict:
361367
with open(metadata_file) as f:
362368
return json.load(f)
363369
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"]

synthtool/gcp/templates/node_library/.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
on:
22
push:
33
branches:
4-
- master
4+
- default-branch
55
pull_request:
66
name: ci
77
jobs:

synthtool/gcp/templates/node_library/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
{% endif %}
2626

2727
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).
2929

3030
{% if metadata['repo']['client_documentation'] %}* [{{ metadata['repo']['name_pretty'] }} {{ metadata['repo']['language']|language_pretty }} Client API Reference][client-docs]{% endif %}
3131
{% 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].
7575
{% if metadata['samples']|length %}
7676
## Samples
7777

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.
7979

8080
| Sample | Source Code | Try it |
8181
| --------------------------- | --------------------------------- | ------ |
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) |
8383
{% endfor %}
8484
{% endif %}
8585
{% if metadata['repo']['client_documentation'] %}
@@ -141,7 +141,7 @@ More Information: [Google Cloud Platform Launch Stages][launch_stages]
141141

142142
## Contributing
143143

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).
145145

146146
Please note that this `README.md`, the `samples/README.md`,
147147
and a variety of configuration files in this repository (including `.nycrc` and `tsconfig.json`)
@@ -153,7 +153,7 @@ to its template in this
153153

154154
Apache Version 2.0
155155

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)
157157

158158
{% if metadata['repo']['client_documentation'] %}[client-docs]: {{ metadata['repo']['client_documentation'] }}{% endif %}
159159
{% if metadata['repo']['product_documentation'] %}[product-docs]: {{ metadata['repo']['product_documentation'] }}{% endif %}

synthtool/gcp/templates/node_library/samples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Before running the samples, make sure you've followed the steps outlined in
4545

4646
{%- endif %}
4747

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 }}).
4949

5050
[![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)
5151

tests/test_common.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from synthtool.gcp.common import decamelize
15+
from synthtool.gcp.common import decamelize, _get_default_branch_name
1616
from pathlib import Path
1717
from pytest import raises
1818
import os
19+
import requests_mock
1920
import synthtool as s
2021
import tempfile
2122
import shutil
2223

24+
2325
MOCK = Path(__file__).parent / "generationmock"
2426
template_dir = Path(__file__).parent.parent / "synthtool/gcp/templates"
2527
common = s.gcp.CommonTemplates(template_path=template_dir)
@@ -41,6 +43,14 @@ def test_handles_empty_string():
4143
assert decamelize("") == ""
4244

4345

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+
4454
def test_py_samples_clientlib():
4555
path_to_gen = MOCK / "client_library"
4656
with tempfile.TemporaryDirectory() as tempdir:

tests/test_transforms.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,6 @@ def change_test_dir():
281281
def test_get_staging_dirs(change_test_dir):
282282
assert [path.name for path in transforms.get_staging_dirs("v1")] == ["v2", "v1"]
283283
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()
284286
assert [path.name for path in transforms.get_staging_dirs()] == ["v1", "v2"]

0 commit comments

Comments
 (0)