0
votes

I'm puzzled with the Azure DevOps API POST. I've successfully created a policy for "Minimum number of reviewers" but it's not being created against the branch I'm specifying. I've done this in Fiddler for the moment, here's my request (private values obfuscated):

POST http://our-tfs-server:8080/tfs/TheCollection/TheProject/_apis/policy/configurations?api-version=5.1 HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: our-tfs-server:8080
Authorization: Basic MyBase64EncodedToken

{
  "IsBlocking": true,
  "IsEnabled": true,
  "Settings": {
    "MinimumApproverCount": 2,
    "Scope": [
      {
        "RefName": "refs/heads/master",
        "MatchKind": "exact",

        "RepositoryId": "7718333c-044e-423a-baa1-45e6a1f0ff41"
      }
    ]
  },
  "Type": {
    "Id": "fa4e907d-c16b-4a4c-9dfa-4906e5d171dd"
  }

}

and here's the response body:

{
  "createdBy": {
    "displayName": "Surname, Firstname",
    "url": "http://our-tfs-server:8080/tfs/TheCollection/_apis/Identities/SomeString",
    "_links": { "avatar": { "href": "http://our-tfs-server:8080/tfs/TheCollection/_apis/GraphProfile/MemberAvatars/win.SomeString" } },
    "id": "7b40f8ab-a933-4b43-bdf8-bf0b179d28e6",
    "uniqueName": "MyUsername",
    "imageUrl": "http://our-tfs-server:8080/tfs/TheCollection/_api/_common/identityImage?id=SomeString",
    "descriptor": "win.SomeOtherString"
  },
  "createdDate": "2020-01-23T02:09:34.4738854",
  "isEnabled": true,
  "isBlocking": true,
  "isDeleted": false,
  "settings": {
    "minimumApproverCount": 2,
    "creatorVoteCounts": false,
    "allowDownvotes": false,
    "resetOnSourcePush": false,
    "scope": [ { "repositoryId": "7718333c-044e-423a-baa1-45e6a1f0ff41" } ]
  },
  "_links": {
    "self": { "href": "http://our-tfs-server:8080/tfs/TheCollection/8b7e65ed-1136-4b0f-9780-de2a3860447a/_apis/policy/configurations/254" },
    "policyType": { "href": "http://our-tfs-server:8080/tfs/TheCollection/8b7e65ed-1136-4b0f-9780-de2a3860447a/_apis/policy/types/fa4e907d-c16b-4a4c-9dfa-4906e5d171dd" }
  },
  "revision": 1,
  "id": 254,
  "url": "http://our-tfs-server:8080/tfs/TheCollection/8b7e65ed-1136-4b0f-9780-de2a3860447a/_apis/policy/configurations/254",
  "type": {
    "id": "fa4e907d-c16b-4a4c-9dfa-4906e5d171dd",
    "url": "http://our-tfs-server:8080/tfs/TheCollection/8b7e65ed-1136-4b0f-9780-de2a3860447a/_apis/policy/types/fa4e907d-c16b-4a4c-9dfa-4906e5d171dd",
    "displayName": "Minimum number of reviewers"
  }
}

Note that "scope": [ { "repositoryId": "7718333c-044e-423a-baa1-45e6a1f0ff41" } ] only has the repositoryId.

In the UI on the policies for the branch the Minimum Number of reviewers option is not ticked and there's no version of the question at a full Repository level. If I call /_apis/policy/configurations then I see the new policy so it has been created. Perhaps this will now guard all branches with the policy, but we don't want it at a global level and given there's no tooling in the DevOps web ui for it I'm pretty sure it's not intended to be like that anyway.

So, is this an oversight in the way the API functions or is the branch to apply the policy configuration to set using another technique?

1
Why can’t you apply the policy manually to the brach level using the UI, then do a GET to see what it looks like, then reference the result for what the POST body is supposed to look like? - jbooker
Since posting I have actually done that with Fiddler running and taken it's request. I'm not entirely clear which property is causing the issue. I'm stepping through removing default valued properties on at a time from the request then deleting to see what comes back. What's bugging me is I need to drive this from a C# app to apply rules in bulk and the call looks right to me but from the app where I've made the request the same as what Fiddler caught, it's failing there. will keep plugging away at it. doesn't seem like I'm missing anything other than something on the request body. Thanks - Stephen York

1 Answers

1
votes

So...the answer lay in the capitalisation of property names on the json request body. Whilst json is not case sensitive and the request I was making was returning 200 and a valid body internally, the server was falling apart on the setting of the branch to apply the Policy Configuration to. NewtonSoft was serialising property names with capital letters at the beginning and I found that using lowercase fixed the original issue and the returned object which was created then had the refName and matchKind assigned and the MinimumNumberOfReviewers check box was ticked and the number set to 2. Why MS have failed to serialise only one or 2 properties is a little weird, at any rate I applied JsonSerializationSettings globally to avoid the need for adding JsonPropertyAttribute everywhere as well as ignoring null properties.