Skip to content

Commit be9b35a

Browse files
authored
Merge pull request #5 from alalkamys/feat/default-pr-target-branch
Feat: Default PR Target Branch to Repo Default Branch
2 parents 96af9cb + e5d87a5 commit be9b35a

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

README.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
1010
[![GitHub Issues or Pull Requests](https://img.shields.io/github/issues/alalkamys/code-migration-assistant)](https://github.com/alalkamys/code-migration-assistant/issues)
11-
[![Version](https://img.shields.io/github/release/alalkamys/code-migration-assistant.svg)](https://github.com/alalkamys/code-migration-assistant/releases/)
11+
[![GitHub Release](https://img.shields.io/github/v/release/alalkamys/code-migration-assistant)](https://github.com/alalkamys/code-migration-assistant/releases/)
1212

1313
`Code Migration Assistant` is a powerful tool designed to streamline and automate the process of code migration at scale. With its intuitive interface and robust features, it enables users to effortlessly search for and replace patterns across multiple repositories, saving time and ensuring consistency in codebases.
1414

@@ -32,6 +32,8 @@ With `Code Migration Assistant` teams can accelerate the migration of code repos
3232
- [`replacements` (Optional)](#replacements-optional)
3333
- [`filesToExclude` (Optional)](#filestoexclude-optional)
3434
- [Environment Variables](#environment-variables)
35+
- [⚔️ Developed By](#️-developed-by)
36+
- [:book: Author](#book-author)
3537
<!--te-->
3638

3739
## Key Features
@@ -55,6 +57,7 @@ With `Code Migration Assistant`, developers and DevOps teams can efficiently man
5557
## Requriements
5658

5759
- [Git:][git] `Code Migration Assistant` relies on Git for cloning and interacting with repositories. Make sure Git is installed on your system and configured properly.
60+
- Python 3.6 or higher.
5861
- Access to Target Repositories: Ensure that you have appropriate access permissions to clone and modify the target repositories specified in the configuration file (`config.json`).
5962
- Environment Variables: Set up the required environment variables such as `AZURE_DEVOPS_PAT`, `GITHUB_TOKEN` and `GITHUB_ENTERPRISE_TOKEN` with the appropriate access permissions to authenticate with the respective SCM providers and raise pull requests.
6063

@@ -232,7 +235,7 @@ Below is an explanation of each field in the configuration file:
232235

233236
- For `azuredevops`:
234237

235-
- `targetRefName` (required): The name of the target branch of the pull request. (e.g `main` or `refs/heads/main`).
238+
- `targetRefName` (optional): The name of the target branch of the pull request. (e.g `main` or `refs/heads/main`). Defaults to the remote repository default branch.
236239
- `title` (required): The title of the pull request.
237240
- `description` (optional): The description of the pull request.
238241
- `labels` (optional): Labels to be applied to the pull request.
@@ -244,7 +247,7 @@ Below is an explanation of each field in the configuration file:
244247
245248
- For `github`:
246249

247-
- `base` (required): The name of the branch you want the changes pulled into. This should be an existing branch on the current repository. You cannot submit a pull request to one repository that requests a merge to a base of another repository (e.g `main` or `refs/heads/main`).
250+
- `base` (optional): The name of the branch you want the changes pulled into. This should be an existing branch on the current repository. You cannot submit a pull request to one repository that requests a merge to a base of another repository (e.g `main` or `refs/heads/main`). Defaults to the remote repository default branch.
248251
- `title` (required): The title of the pull request.
249252
- `body` (optional): The body content of the pull request.
250253
- `maintainer_can_modify` (optional): Indicates whether maintainers can modify the pull request.
@@ -294,6 +297,20 @@ Below is an explanation of each field in the configuration file:
294297
| `GITHUB_ENTERPRISE_TOKEN` | GitHub Enterprise Personal Access Token (PAT) | `None` |
295298
| `CODE_MIGRATION_ASSISTANT_USER_AGENT` | User agent used for HTTP requests by Code Migration Assistant | `alalkamys/code-migration-assistant` |
296299

300+
<br />
301+
302+
## ⚔️ Developed By
303+
304+
<a href="https://www.linkedin.com/in/shehab-el-deen/" target="_blank"><img alt="LinkedIn" align="right" title="LinkedIn" height="24" width="24" src="docs/assets/imgs/linkedin.png"></a>
305+
306+
Shehab El-Deen Alalkamy
307+
308+
<br />
309+
310+
## :book: Author
311+
312+
Shehab El-Deen Alalkamy
313+
297314
<!--********************* R E F E R E N C E S *********************-->
298315

299316
<!-- * Links * -->

app/helpers/scm_providers_operations.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from app.config import app_config
22
from app.helpers import azuredevops
33
from app.helpers import github
4+
from app.helpers.git import get_default_branch_name
45

56
from azure.devops.credentials import BasicAuthentication
67
from azure.devops.v7_0.git import GitPullRequest
@@ -51,7 +52,14 @@ def is_open_pull_requests(repo: Repo, pull_request_config: dict[str, dict[str, A
5152

5253
source_ref_name = f"refs/heads/{
5354
repo.active_branch.name}"
54-
target_ref_name: str = pull_request_payload['targetRefName']
55+
target_ref_name: str = pull_request_payload.get(
56+
'targetRefName', None)
57+
58+
if not target_ref_name:
59+
default_branch_name = get_default_branch_name(repo=repo)
60+
_logger.info(f"Setting targetRefName to the default branch '{
61+
default_branch_name}'")
62+
target_ref_name = default_branch_name
5563

5664
if not target_ref_name.startswith("refs/heads/"):
5765
_logger.debug(
@@ -104,7 +112,13 @@ def is_open_pull_requests(repo: Repo, pull_request_config: dict[str, dict[str, A
104112

105113
head_ref = f"refs/heads/{
106114
repo.active_branch.name}"
107-
base_ref: str = pull_request_payload['base']
115+
base_ref: str = pull_request_payload.get('base', None)
116+
117+
if not base_ref:
118+
default_branch_name = get_default_branch_name(repo=repo)
119+
_logger.info(f"Setting base to the default branch '{
120+
default_branch_name}'")
121+
base_ref = default_branch_name
108122

109123
if not base_ref.startswith("refs/heads/"):
110124
_logger.debug(
@@ -214,7 +228,14 @@ def raise_pull_request(repo: Repo, pull_request_config: dict[str, dict[str, Any]
214228
pull_request_payload.get('description', []))
215229
pull_request_payload['sourceRefName'] = f"refs/heads/{
216230
repo.active_branch.name}"
217-
target_ref_name: str = pull_request_payload['targetRefName']
231+
target_ref_name: str = pull_request_payload.get(
232+
'targetRefName', None)
233+
234+
if not target_ref_name:
235+
default_branch_name = get_default_branch_name(repo=repo)
236+
_logger.info(f"Setting targetRefName to the default branch '{
237+
default_branch_name}'")
238+
target_ref_name = default_branch_name
218239

219240
if not target_ref_name.startswith("refs/heads/"):
220241
_logger.debug(
@@ -257,7 +278,13 @@ def raise_pull_request(repo: Repo, pull_request_config: dict[str, dict[str, Any]
257278
pull_request_payload.get('body', []))
258279
pull_request_payload['head'] = f"refs/heads/{
259280
repo.active_branch.name}"
260-
base_ref: str = pull_request_payload['base']
281+
base_ref: str = pull_request_payload.get('base', None)
282+
283+
if not base_ref:
284+
default_branch_name = get_default_branch_name(repo=repo)
285+
_logger.info(f"Setting base to the default branch '{
286+
default_branch_name}'")
287+
base_ref = default_branch_name
261288

262289
if not base_ref.startswith("refs/heads/"):
263290
_logger.debug(

docs/assets/imgs/linkedin.png

913 Bytes
Loading

0 commit comments

Comments
 (0)