Azure DevOps Crash Course (Part 3): Azure Repos, Branches and Your First Pull Request
DEV Community

Azure DevOps Crash Course (Part 3): Azure Repos, Branches and Your First Pull Request

Early in my career I pushed a "tiny fix" straight to main on a Friday afternoon. It broke the login page for everyone. I spent my weekend fixing it, and on Monday my lead turned on branch protection. Nobody said a word, but I got the message.

Today we make sure that never happens to you. In Part 2 we planned our work in Azure Boards. Now we finally write some code.

By the end of this post you will have:

  • A small ASP.NET Core Todo API in Azure Repos
  • A branch created straight from a work item
  • Your first pull request, linked to a user story
  • A main branch that nobody (including you) can push to directly

Let us go.

Step 1: Create the Todo API

You need the .NET SDK installed. I am using .NET 10, the current long-term support version. Open a terminal and run:

mkdir TodoApi && cd TodoApi
dotnet new sln -n TodoApi
dotnet new webapi -n TodoApi.Api -o src/TodoApi.Api
dotnet new xunit -n TodoApi.Tests -o tests/TodoApi.Tests
dotnet sln add src/TodoApi.Api tests/TodoApi.Tests
dotnet add tests/TodoApi.Tests reference src/TodoApi.Api
dotnet new gitignore

Quick check that everything works:

dotnet build
dotnet test

We now have an API project and a test project. The tests do not do much yet, that is Part 4.

Step 2: Push it to Azure Repos

When we created the TodoApi project in Part 1, Azure DevOps also created an empty Git repo with the same name. Go to Repos > Files and you will see a page with instructions to push an existing repository. Copy the URL from there. It looks like this:

git init -b main
git add .
git commit -m "Initial Todo API"
git remote add origin https://dev.azure.com/yourname-learning/TodoApi/_git/TodoApi
git push -u origin main

If Git asks for a password, you have two easy options:

  • If you have Git for Windows, Git Credential Manager is already installed and will open a browser window to sign you in. This is the easiest way.
  • Otherwise, click Generate Git Credentials on that same Repos page and use the username and password it gives you.

Refresh Repos > Files and there is your code. That little moment never gets old.

Step 3: Protect the main branch

Before we write any new code, let us lock the front door.

  • Go to Repos > Branches.
  • Find main, click the three dots, and choose Branch policies.
  • Turn on Require a minimum number of reviewers and set it to 1.
  • Since you are probably learning alone, also tick Allow requestors to approve their own changes. In a real team, leave this off.
  • Turn on Check for linked work items and set it to Required.
  • Turn on Check for comment resolution and set it to Required.

From now on, every change to main must come through a pull request that is reviewed, linked to a work item, and has no open comments. Microsoft explains every option here: Set and manage branch policies.

There is one more policy called Build validation. It runs your tests on every pull request. We will turn that on in Part 4, once we have a pipeline.

Step 4: Create a branch from a work item

This is one of my favorite little features in Azure DevOps.

  • Go to Boards > Sprints and open the story "As a user, I want to add a todo". Note its ID, for example #3 (yours may be different).
  • In the Development section on the right, click Create a branch.
  • Name it feature/3-add-todo and base it on main.

The branch is now automatically linked to the story. Pull it locally:

git fetch
git checkout feature/3-add-todo

Step 5: Write a little code

Create a file src/TodoApi.Api/TodoItem.cs:

namespace TodoApi.Api;
public record TodoItem(int Id, string Title, bool IsDone = false)
{
  public static bool IsValidTitle(string? title) => !string.IsNullOrWhiteSpace(title) && title.Length <= 100;
}

Remember the acceptance criteria from Part 2? "Title is required. Title is 100 characters or less." That rule now lives in code.

Now open Program.cs and add these endpoints just before app.Run();:

var todos = new List<TodoApi.Api.TodoItem>();
app.MapGet("/todos", () => todos);
app.MapPost("/todos", (TodoApi.Api.TodoItem input) =>
{
  if (!TodoApi.Api.TodoItem.IsValidTitle(input.Title))
    return Results.BadRequest("Title is required and must be 100 characters or less.");
  var item = input with { Id = todos.Count + 1 };
  todos.Add(item);
  return Results.Created($"/todos/{item.Id}", item);
});

Yes, it is an in-memory list. That is fine for learning. We care about the DevOps part, not the database.

Commit and push:

git add .
git commit -m "Add todo endpoints with title validation #3"
git push -u origin feature/3-add-todo

Notice the #3 in the commit message. Azure Repos sees that and links the commit to the work item automatically.

Step 6: Open your first pull request

  • Go to Repos > Pull requests. You will probably see a banner offering to create a pull request for your new branch. Click it.
  • Target branch: main.
  • Write a short title and description. Pretend someone else will read it next year, because they will.
  • Check that your work item shows up under Work items to link. If not, add it.
  • Click Create.

Now look at the right side of the pull request. You will see the policies we set up: reviewers, linked work items, comments. Each one turns green when it is satisfied.

Leave yourself a comment on one line of code, then resolve it. Approve the pull request. Then click Complete. In the dialog, tick Complete associated work items after merging and Delete feature/3-add-todo after merging.

Go back to Boards. The story moved to Closed on its own. That connection between planning and code is the whole point of Azure DevOps. Microsoft's full guide to pull requests is here: Collaborate on code with Azure Repos.

Bonus: look at your pull request as raw data

Everything you just clicked through is also available through the REST API. While you are signed in, open this in a new tab (swap in your own organization name):

https://dev.azure.com/yourname-learning/TodoApi/_apis/git/repositories/TodoApi/pullrequests?searchCriteria.status=all&api-version=7.1

You will get a long JSON response with your pull request: who created it, the source and target branches, the merge status and more. It is hard to read in the browser, so I paste it into a JSON parser online to format it and click through the fields.

It is a really good way to understand what Azure DevOps stores behind the scenes, and it will help later when we start automating things. The official reference for this endpoint is here: Pull Requests - Get Pull Requests.

Your homework

  • Push the Todo API to Azure Repos
  • Add the branch policies on main
  • Create a branch from a work item, make a change, and complete a pull request
  • Try to push directly to main and watch it get rejected (it feels good, trust me)

Coming up next

In Part 4 we build our first YAML pipeline. Every push will restore, build and test the app automatically, and we will hook it into the pull request so broken code can never reach main. If you have not sent the free parallelism request from Part 1 yet, do it today.

What is your team's rule for pull requests? One reviewer, two reviewers, or "just merge it"? Tell me in the comments.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.