megha
megha

Reputation: 833

Create GitHub service connection using azure rest api giving error

I am trying to create GitHub service connection using azure Devops rest api -

In postman I put following Json Body-

{
   "name": "release-1",
   "type": "github",
   "url": "https://github.com",
   "authorization": {
   "scheme": "PersonalAccessToken",
   "parameters": {
   "accessToken": "<Github_Personal_Access_Token>"
  }
 }
}

This Creates service connection but It gives error when I open service connection in UI and try to verify but if I edit and replace with same Github token In UI then it works. seems like its not taking token I am providing in Json body -

enter image description here

I also see information here on this ongoing issue - https://ljvmiranda921.github.io/notebook/2019/12/28/workaround-azure-github-pat/

I want to automate GitHub service connection creation.

How to create GitHub service connection using Azure Devops rest api ?

Upvotes: 1

Views: 795

Answers (1)

LoLance
LoLance

Reputation: 28086

How to create GitHub service connection using Azure Devops rest api ?

Are you creating this GitHub service connection?

enter image description here

If so, you should use EndPoints-Create API and your Json Body can follow this format:

{
    "authorization": {
        "scheme": "Token",
        "parameters": {"AccessToken": "YourGitHubPAT"}
    },
    "data": {},
    "description": "",
    "name": "YourServiceConnectionName",
    "serviceEndpointProjectReferences": [{
        "description": "",
        "name": "YourServiceConnectionName",
        "projectReference": {
            "id": "YourProjectID",
            "name": "YourProjectName"
        }
    }],
    "type": "github",
    "url": "https://github.com",
    "isShared": false
}

We should enter the value of YourGitHubPAT, YourServiceConnectionName(twice in two points), YourProjectID and YourProjectName with your own values to make the body work. We can get YourProjectID via Projects-List API.

After running that in PostMan, I can verify it successfully without issues. I guess your issue may have something to do with your parameters. Use

"authorization": {
    "scheme": "Token",
    "parameters": {"AccessToken": "<Github_Personal_Access_Token>"}
},

Instead of

"authorization": {
   "scheme": "PersonalAccessToken",
   "parameters": {"accessToken": "<Github_Personal_Access_Token>"}

Update about how to enable grant access to all pipelines:

Dislike other elements like Token, Name and Description, Grant Access Permissions to all pipelines option is managed by another API.

API to enable this option:

https://dev.azure.com/{YourOrganizationName}/{YourProjectName}/_apis/pipelines/pipelinePermissions/endpoint/{YourEndPointID}?api-version=5.1-preview.1

Body:

{
    "resource": {
        "id": "YourEndPointID",
        "type": "endpoint",
        "name": ""
    },
    "pipelines": [],
    "allPipelines": {
        "authorized": true,
        "authorizedBy": null,
        "authorizedOn": null
    }
}

Note: We need to enter EnterPointID in URL and Body. (Twice!)

In PostMan, you should use PATCH method and application/json type. The YourEndPointID is ServiceConnectionID, you can get this value from EndPoints-Create API's response.

So normally to create a service connection with "Grant Access to All..." enabled, we should run Endpoints-Create first and then run the second API to enable this option.

(And this is the same behavior in Web Portal. When we click Verify and Save button in web browser, it actually also calls these two APIs to complete the process.)

Upvotes: 1

Related Questions