I have a shared branch in TFS (i.e. many people push to it). I want to prevent people from opening a Pull Request from that shared branch to any other branch.
I have full control over the server side. For example, I can add server side hooks or install server side extensions.
Motivation
Given #1
In TFS we associate work items with commits. However, we do not know how to enforce that individual commits be associated with work items. We do know how to enforce it for a Pull Request - through a branch policy.
So, we are not going to require the developers associate work items with commits, only with the Pull Request.
Given #2
In TFS if there is an outstanding Pull Request from some branch, then any new commits pushed to this branch or pulled into it (through another Pull Request) are automatically added to the outstanding Pull Request. See Prevent TFS from adding new commits to open pull request?
The result
Suppose X is a branch with an outstanding Pull Request PR1 onto the branch Y carrying 3 commits - c1
, c2
and c3
.
X --[PR1=<c1,c2,c3>]--> Y
If a new commit c4
is pulled into X through another Pull Request (say PR2)
--[PR2=<c4>]--> X
then c4
is automatically added to PR1:
X --[PR1=<c1,c2,c3,c4>]--> Y
Which means, when PR1 is completed, c4
will be associated with the work item of PR1. So, what do we have? In the branch X that commit is associated with PR2, but in the branch Y - with PR1. Both Pull Requests are associated with different work items.
What a mess.
Possible ways to prevent
- Enforce association of work items on the commit level. Besides the fact that we do not have a convenient way to enforce it (besides a server side hook that enforces every comment to end with
#XYZ
, see the first tip in https://blog.ehn.nu/2015/10/10-features-in-team-foundation-server-that-you-maybe-didnt-know-about/), it does not resolve the problem of the Pull Request scope creep. This may not be a problem for Pull Requests that are resolved automatically through a build, but it is for those requiring human approval. - Change the default behavior, i.e. Pull Request will not accept new commits as the default. And if the commits in the Pull Request are no longer in the source branch, then the Pull Request is automatically abandoned. Alas, TFS does not provide such flexibility and we do not know how this can be done, if at all.
- Prevent outgoing Pull Requests from a branch. We know that X is a shared branch, i.e. new commits can arrive there from different contributors at random, which generates the issue. If there was no way to open a Pull Request from X, then we would not have had this problem. But then how do you merge from X to Y? Create a private branch off X and merge it instead (through a Pull Request, of course). But that branch is private and so there is no issue.
Out of all the items, I feel we may have the most luck with the last one.