DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mastering High-Risk GitHub Pull Requests: Review, Rollout Strategies, and Lessons Learned
  • Why GitOps Is Gaining Popularity in DevOps: A Deep Dive Into the Future of Infrastructure Management
  • Resilience Pattern: Circuit Breaker
  • JGit Library Examples in Java

Trending

  • Agentic Testing: Moving Quality From Checkpoint to Control Layer
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How To Handle Dependencies Between Pull-Requests

How To Handle Dependencies Between Pull-Requests

In this article, we will learn valuable techniques for managing dependencies between Pull Requests (PRs). We would understand how to strategically handle conflicts and optimize the order of merges.

By 
Hugo Escafit user avatar
Hugo Escafit
·
Jul. 23, 23 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
5.1K Views

Join the DZone community and get the full member experience.

Join For Free

In the dynamic environment of software development, effective management of dependencies between pull requests (PRs) is pivotal to enabling smooth collaboration and seamless code integration. 

But let’s face it, juggling dependencies manually can be a real challenge!

Why Managing Dependencies Between PRs Is a Challenge?

Here are some key aspects of the problem:

Order of Merging

If PRs have dependencies on one another, the order in which they are merged becomes crucial.

Merging PRs in the wrong order could lead to broken code or features not working as intended in the main branch.

This challenge becomes even more pronounced when there are a lot of PRs dependent on each other in large projects.

It can sometimes create challenging situations where it’s difficult to assign responsibility and find solutions.

He roped me into this meme
Meeseeks

Conflict Resolution

When multiple PRs modify the same codebase, they can lead to dependency issues.

Resolving these issues manually can be time-consuming and error-prone.

Code Review Complexity

PRs that depend on changes introduced in other PRs can complicate the code review process.

Reviewers may need to consider multiple PRs at once to understand the full context, which can slow down the review and merging process.

Solution: Defining PR Dependencies

In this tutorial, we will explore how Mergify helps improve the process and improve workflow efficiency by handling dependencies. 

Mergify is a robust solution for GitHub automation, engineered to streamline our workflow and adeptly manage the intricacies of dependency handling.

We could avoid the above cases by defining “Defining Pull Request Dependencies” using Mergify’s Depends-On header. 

It handles prioritization and streamlines the process by automatically arranging the merging sequence of our pull requests.

We need to simply add a Depends-On header into the body of a pull request to indicate its dependencies.

It will then wait to merge until all linked requests are successfully merged. 

Now let’s see this in action with a simple example:

Step 1: Project Setup

For this example, we would start out by setting up a basic React project.

Shell
 
mkdir mergify-demo
cd mergify-demo
git init
npx create-react-app ./


For this example, we would be making pull requests creating different branches for our changes and opening pull requests into the main branch with dependencies.

Setting up the project

Step 2: Setting up Rules

We will go ahead and create our .mergify.yml file.

The configuration file contains a pull_request_rules key, which includes a list of rules.

Each rule has a name, conditions, and actions.

We would need to define the conditions that must match for the rule to be applied and specify the actions that will be executed when the conditions are met.

Here’s what our file would look like 

.mergify.yml

YAML
 
queue_rules:
  - name: default
    merge_conditions:
      - label=merge
pull_request_rules:
  - name: Alert on dependency updates
    conditions:
      - files~=package.json
    actions:
      label:
        add:
          - dependency update
      queue:
        name: default


The above rule will check if the package.json file has been changed - if yes, then it will add a label dependency update and put the PR in the queue named default.

It will then merge the PR when the label merge is applied to the PR.

We can use the Mergify config editor to validate our rules before using them.

Here’s how our folder structure will look after adding .mergify.yml file 

Folder Structure

Step 3: Pushing the Repo

Now we would create a new repository in GitHub and push our project to it.

Shell
 
git add .
git commit -m "first commit"
git branch -M main
git remote add origin <Repo Link>
git push -u origin main


Step 4: Setting up Mergify With Our Repository

Now we will go ahead and set up Mergify with our repository. 

We can install Mergify in our GitHub account by visiting the dashboard or GitHub site and logging in with GitHub.

Then we could select the repositories we want Mergify to give access to (for demo purposes, we will give access to all the repos in this case)

We could now use Mergify's Config Editor to verify our rules. 

The Set-Up

Step 5: Creating the First PR

Let’s go ahead and create a new branch named pr-1.

Shell
 
git checkout -b pr-1


In this branch, we would change the version of our project in the package.json file from

JSON
 
 "version": "0.1.0"


to

JSON
 
"version": "0.1.1"


Now, we will push our changes.

Shell
 
git add .
git commit -m "chore: version upgrade"
git push --set-upstream origin pr-1


and create a new Pull Request to the main branch without any header.

Making the first PR

In our rules, we have defined a merge condition, i.e., label=merge.

We did not put the merge label in the first PR, so Mergify will not merge automatically, and we would need to merge it manually.

Step 6: Creating the Second PR

Let’s go ahead and create a new branch named pr-2. In this branch we would change the version of our project in package.json file and also that of react-scripts.

Shell
 
git checkout maingit checkout -b pr-2


In this branch, we would change the version of our project as well as of react-scripts in the package.json file, from

JSON
 
{
    "name": "mergify-demo",
    "version": "0.1.0",
    "dependencies": {
        "@testing-library/jest-dom": "^5.16.5",
        "@testing-library/react": "^13.4.0",
        "@testing-library/user-event": "^13.5.0",
        "react": "^18.2.0",
        "react-dom": "^18.2.0",
        "react-scripts": "5.0.1",
        "web-vitals": "^2.1.4"
    }
}


to

JSON
 
{
    "name": "mergify-demo",
    "version": "0.1.1",
    "dependencies": {
        "@testing-library/jest-dom": "^5.16.5",
        "@testing-library/react": "^13.4.0",
        "@testing-library/user-event": "^13.5.0",
        "react": "^18.2.0",
        "react-dom": "^18.2.0",
        "react-scripts": "5.0.0",
        "web-vitals": "^2.1.4"
    }
}


Now let’s push this one too.

Shell
 
git add .
git commit -m "chore: change react-scripts version"
git push --set-upstream origin pr-2


Now we would go ahead and open a pull request with the following header.

Shell
 
Updating Version.
You may look at these pull requests for the full picture.
Depends-On: #1


Mergify looks for the Depends-On: keyword in the PR header for dependency information.

Here we have added the merge label, so it will try to merge this PR automatically if all the other conditions are met.

Step 7: Merging the First PR

Now we will go ahead and merge our first PR. Our second PR still has some merge conflicts, we will resolve them as shown 

Mergify will automatically check if the conditions of the rules are met for the pull request. When conditions are satisfied, it will apply the specified actions. As soon as we resolve our merge conflict, our PR gets merged automatically.  We can go to the dashboard and check our event logs 

You may check out both  Pull Requests here for reference.

That’s it! Now enjoy automated dependency management and smoother PR workflows. Let automation tools handle the complexities while you focus on building amazing software!

Conclusion

In our exploration of dependency management with Mergify, we’ve discovered powerful tools that streamline workflows and conquer challenges in handling dependencies between pull requests by automating tasks and ensuring code stability.

Git Dependency pull requests Requests pull request push

Published at DZone with permission of Hugo Escafit. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Mastering High-Risk GitHub Pull Requests: Review, Rollout Strategies, and Lessons Learned
  • Why GitOps Is Gaining Popularity in DevOps: A Deep Dive Into the Future of Infrastructure Management
  • Resilience Pattern: Circuit Breaker
  • JGit Library Examples in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook