Skip to content

Commit

Permalink
Merge pull request #30 from uclahs-cds/nwiltsie-delete-branch
Browse files Browse the repository at this point in the history
Automatically delete release branch after merge
  • Loading branch information
nwiltsie authored Jan 28, 2025
2 parents e539242 + e7a6193 commit 2035ab6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed

- Remove hard-coded UCLA secrets, add explicit `secrets` inputs to workflows
- Automatically delete release branch after merging pull request

## [1.0.3] - 2024-11-01

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Usage of this tool requires adding three workflows to each calling repository (n
* By default the new release is a draft, so no public release or tag are created without user intervention.
1. Optionally, attach a source tarball including all submodules to the new release.
1. Comment on the release PR with a link to the new release.
1. Delete the merged release branch.

```mermaid
gitGraph
Expand Down Expand Up @@ -118,7 +119,7 @@ Parameters can be specified using the [`with`](https://docs.github.com/en/action
| `wf-alias-release.yaml` | `commit-user-name` | string | no | User name to use while tagging new commits (defaults to `github-actions[bot]`) |
| `wf-alias-release.yaml` | `commit-user-email` | string | no | User email to use while tagging new commits (defaults to `41898282+github-actions[bot]@users.noreply.github.com`) |

All four workflows also accept a `token` secret which is required for full functionality (e.g. CI/CD checks on the opened pull request). The provided [personal access token](https://github.com/settings/tokens/new) should be stored as a secret in the repository and have the following scopes:
All three workflows also accept a `token` secret which is required for full functionality (e.g. CI/CD checks on the opened pull request). The provided [personal access token](https://github.com/settings/tokens/new) should be stored as a secret in the repository and have the following scopes:

* `repo`
* `workflow`
Expand Down
4 changes: 4 additions & 0 deletions bumpchanges/finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .logging import setup_logging, NOTICE, LoggingMixin
from .utils import (
decode_branch_name,
delete_branch,
version_to_tag_str,
tag_to_semver,
str_to_bool,
Expand Down Expand Up @@ -206,3 +207,6 @@ def entrypoint():
except:
logging.getLogger(__name__).exception("Failed to create new release")
raise

# Delete the branch
delete_branch(new_release.owner_repo, os.environ["GITHUB_HEAD_REF"])
26 changes: 26 additions & 0 deletions bumpchanges/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ def decode_branch_name(branch: str) -> str:
return version


def delete_branch(owner_repo: str, branch: str):
"""Delete a branch from GitHub by name. Gracefully handles failure."""
logger = logging.getLogger(__name__)
try:
subprocess.run(
[
"gh",
"api",
"--method",
"DELETE",
"-H",
"Accept: application/vnd.github+json",
"-H",
"X-GitHub-Api-Version: 2022-11-28",
f"/repos/{owner_repo}/git/refs/heads/{branch}",
],
check=True,
capture_output=True,
)
logger.info("Deleted branch %s", branch)
except subprocess.CalledProcessError as err:
logger.info("Failed to delete branch %s", branch)
logger.info("Stdout: %s", err.stdout.decode("utf-8"))
logger.info("Stderr: %s", err.stderr.decode("utf-8"))


def tag_to_semver(tag: str) -> semver.version.Version:
"""
Return the Version associated with this git tag.
Expand Down

0 comments on commit 2035ab6

Please sign in to comment.