Skip to content
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

fix: Add fetch for default branch #1031

Merged
merged 5 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions synthtool/gcp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 @@ -58,6 +59,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 @@ -353,3 +359,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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really appreciate that you added a test for this 🆗

"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