-
Notifications
You must be signed in to change notification settings - Fork 37
feat(treescript): Bug 2015581 Add github create branch action #1341
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,58 @@ async def __aenter__(self): | |
| async def __aexit__(self, *excinfo): | ||
| await self.close() | ||
|
|
||
| async def create_branch(self, branch_name: str, from_branch: Optional[str] = None, dry_run: bool = False) -> None: | ||
| """Create a new branch in the repository. | ||
|
|
||
| Args: | ||
| branch_name (str): The name of the new branch to create. | ||
| from_branch (str): The branch to create the new branch from. Uses the | ||
| repository's default branch if unspecified (optional). | ||
| dry_run (bool): If it's a dry run | ||
| """ | ||
| # Get the repository ID and source OID in one query | ||
| source_branch = from_branch or "HEAD" | ||
| info_query = Template( | ||
| dedent(""" | ||
| query getRepoInfo { | ||
| repository(owner: "$owner", name: "$repo") { | ||
| id | ||
| object(expression: "$branch") { | ||
| oid | ||
| } | ||
| } | ||
| }""") | ||
| ) | ||
| str_info_query = info_query.substitute(owner=self.owner, repo=self.repo, branch=source_branch) | ||
| repo = (await self._client.execute(str_info_query))["repository"] | ||
|
|
||
| if repo.get("object") is None: | ||
| raise UnknownBranchError(f"branch '{source_branch}' not found in repo!") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should also check if the branch already exists and bail early? I'm seeing the "branch creation passed but version bump got 500s" already... Although there's an argument for saying that if the branch already exists it could've been created by mistake by a human and shouldn't be used and thus failing here is the right thing to do... |
||
|
|
||
| repo_id = repo["id"] | ||
| source_oid = repo["object"]["oid"] | ||
|
|
||
| create_branch_mutation = dedent(""" | ||
| mutation ($input: CreateRefInput!) { | ||
| createRef(input: $input) { | ||
| ref { | ||
| name | ||
| } | ||
| } | ||
| }""") | ||
| variables = { | ||
| "input": { | ||
| "repositoryId": repo_id, | ||
| "name": f"refs/heads/{branch_name}", | ||
| "oid": source_oid, | ||
| } | ||
| } | ||
|
|
||
| verb = "Would create" if dry_run else "Creating" | ||
| log.debug(f"{verb} {branch_name} on repo {self.repo}[{repo_id}] at {source_branch}@{source_oid}") | ||
| if not dry_run: | ||
| await self._client.execute(create_branch_mutation, variables=variables) | ||
|
|
||
| async def commit(self, branch: str, message: str, additions: Optional[Dict[str, str]] = None, deletions: Optional[List[str]] = None) -> None: | ||
| """Commit changes to the given repository and branch. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #!/usr/bin/env python | ||
| """Treescript branch methods.""" | ||
|
|
||
| from typing import Dict | ||
|
|
||
| from scriptworker_client.github_client import GithubClient | ||
|
|
||
| from treescript.exceptions import TreeScriptError | ||
| from treescript.util.task import get_branch, get_create_branch_info, should_push | ||
|
|
||
|
|
||
| def get_branch_name(task: Dict) -> str: | ||
hneiva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Get the branch_name from a task's create_branch_info. | ||
|
|
||
| Args: | ||
| task (Dict): The task definition containing create_branch_info. | ||
|
|
||
| Returns: | ||
| str: The name of the target branch. | ||
|
|
||
| Raises: | ||
| TreeScriptError: If branch_name is not specified in the task. | ||
| """ | ||
| create_branch_info = get_create_branch_info(task) | ||
| if "branch_name" not in create_branch_info: | ||
| raise TreeScriptError("branch_name is required in task") | ||
| return create_branch_info["branch_name"] | ||
|
|
||
|
|
||
| async def create_branch(client: GithubClient, task: Dict) -> None: | ||
| """Create a new branch in the repository based on task configuration. | ||
|
|
||
| Args: | ||
| client (GithubClient): GithubClient instance for associated repo. | ||
| task (Dict): The task definition containing branch configuration. | ||
| """ | ||
| await client.create_branch( | ||
| branch_name=get_branch_name(task), | ||
| from_branch=get_branch(task), | ||
| dry_run=not should_push(task), | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| #!/usr/bin/env python | ||
| """Tests for treescript github branch methods.""" | ||
|
|
||
| import pytest | ||
|
|
||
| import treescript.github.branch as branch | ||
| from treescript.exceptions import TreeScriptError | ||
|
|
||
|
|
||
| def test_get_branch_name(): | ||
| task = { | ||
| "payload": { | ||
| "create_branch_info": { | ||
| "branch_name": "release-v1.0", | ||
| } | ||
| } | ||
| } | ||
| assert branch.get_branch_name(task) == "release-v1.0" | ||
|
|
||
|
|
||
| def test_get_branch_name_not_specified(): | ||
| task = { | ||
| "payload": { | ||
| "create_branch_info": { | ||
| "from_branch": "main", | ||
| } | ||
| } | ||
| } | ||
| with pytest.raises(TreeScriptError): | ||
| branch.get_branch_name(task) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_create_branch(mocker, github_client): | ||
| task = { | ||
| "payload": { | ||
| "branch": "main", | ||
| "create_branch_info": { | ||
| "branch_name": "release-v1.0", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| mock_create_branch = mocker.patch.object(github_client, "create_branch") | ||
|
|
||
| await branch.create_branch(github_client, task) | ||
|
|
||
| mock_create_branch.assert_called_once_with( | ||
| branch_name="release-v1.0", | ||
| from_branch="main", | ||
| dry_run=False, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_create_branch_dry_run(mocker, github_client): | ||
| task = { | ||
| "payload": { | ||
| "branch": "main", | ||
| "create_branch_info": { | ||
| "branch_name": "release-v1.0", | ||
| }, | ||
| "dry_run": True, | ||
| } | ||
| } | ||
|
|
||
| mock_create_branch = mocker.patch.object(github_client, "create_branch") | ||
|
|
||
| await branch.create_branch(github_client, task) | ||
|
|
||
| mock_create_branch.assert_called_once_with( | ||
| branch_name="release-v1.0", | ||
| from_branch="main", | ||
| dry_run=True, | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.