Skip to content

Commit

Permalink
feat: fetch for default branch (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
billyjacobson authored Apr 16, 2021
1 parent 1657dd8 commit 898b38a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
13 changes: 13 additions & 0 deletions synthtool/gcp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from copy import deepcopy
from pathlib import Path
from typing import Dict, List, Optional
import requests
import jinja2

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

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

if "repository" in kwargs["metadata"] and "repo" in kwargs["metadata"]:
kwargs["metadata"]["repo"]["default_branch"] = _get_default_branch_name(
kwargs["metadata"]["repository"]
)

# TODO: migrate to python.py once old sample gen is deprecated
if directory == "python_samples":
t.env.globals["get_help"] = lambda filename: shell.run(
Expand Down Expand Up @@ -361,3 +367,10 @@ def _load_repo_metadata(metadata_file: str = "./.repo-metadata.json") -> Dict:
with open(metadata_file) as f:
return json.load(f)
return {}


def _get_default_branch_name(repository_name: str) -> str:
github_req = requests.get(f"https://api.github.com/repos/{repository_name}")
github_req.raise_for_status()

return github_req.json()["default_branch"]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on:
push:
branches:
- master
- default-branch
pull_request:
name: ci
jobs:
Expand Down
10 changes: 5 additions & 5 deletions synthtool/gcp/templates/node_library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{% endif %}

A comprehensive list of changes in each version may be found in
[the CHANGELOG](https://github.com/{{ metadata['repo']['repo'] }}/blob/master/CHANGELOG.md).
[the CHANGELOG](https://github.com/{{ metadata['repo']['repo'] }}/blob/{{metadata['repo']['default_branch']}}/CHANGELOG.md).

{% if metadata['repo']['client_documentation'] %}* [{{ metadata['repo']['name_pretty'] }} {{ metadata['repo']['language']|language_pretty }} Client API Reference][client-docs]{% endif %}
{% if metadata['repo']['product_documentation'] %}* [{{ metadata['repo']['name_pretty'] }} Documentation][product-docs]{% endif %}
Expand Down Expand Up @@ -75,11 +75,11 @@ Google APIs Client Libraries, in [Client Libraries Explained][explained].
{% if metadata['samples']|length %}
## Samples

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

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

## Contributing

Contributions welcome! See the [Contributing Guide](https://github.com/{{ metadata['repo']['repo'] }}/blob/master/CONTRIBUTING.md).
Contributions welcome! See the [Contributing Guide](https://github.com/{{ metadata['repo']['repo'] }}/blob/{{ metadata['repo']['default_branch'] }}/CONTRIBUTING.md).

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

Apache Version 2.0

See [LICENSE](https://github.com/{{ metadata['repo']['repo'] }}/blob/master/LICENSE)
See [LICENSE](https://github.com/{{ metadata['repo']['repo'] }}/blob/{{ metadata['repo']['default_branch'] }}/LICENSE)

{% if metadata['repo']['client_documentation'] %}[client-docs]: {{ metadata['repo']['client_documentation'] }}{% endif %}
{% if metadata['repo']['product_documentation'] %}[product-docs]: {{ metadata['repo']['product_documentation'] }}{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion synthtool/gcp/templates/node_library/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Before running the samples, make sure you've followed the steps outlined in

{%- endif %}

View the [source code](https://github.com/{{ metadata['repo']['repo'] }}/blob/master/{{ sample.file }}).
View the [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)

Expand Down
12 changes: 11 additions & 1 deletion tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from synthtool.gcp.common import decamelize
from synthtool.gcp.common import decamelize, _get_default_branch_name
from pathlib import Path
from pytest import raises
import os
import requests_mock
import synthtool as s
import tempfile
import shutil


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


def test_get_default_branch():
with requests_mock.Mocker() as m:
m.get(
"https://api.github.com/repos/repo_name", text='{"default_branch": "main"}',
)
assert _get_default_branch_name("repo_name") == "main"


def test_py_samples_clientlib():
path_to_gen = MOCK / "client_library"
with tempfile.TemporaryDirectory() as tempdir:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,6 @@ def change_test_dir():
def test_get_staging_dirs(change_test_dir):
assert [path.name for path in transforms.get_staging_dirs("v1")] == ["v2", "v1"]
assert [path.name for path in transforms.get_staging_dirs("v2")] == ["v1", "v2"]
paths = [path.name for path in transforms.get_staging_dirs()]
paths.sort()
assert [path.name for path in transforms.get_staging_dirs()] == ["v1", "v2"]

0 comments on commit 898b38a

Please sign in to comment.