0

We have several wiki pages created in our Azure Devops which will help developers to onboard their applications with certain information.

Since we need to manage a huge team, its challenging to share the wiki details to new new teams which are onboarding.

As a solution we are looking for some enforcement policy in Azure Devops which will help us.

  • A common readme file with those wiki links are onboarded to all the new repos going to be created.
  • The common readme file with those wiki links are imported to all the existing repos
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
Vowneee
  • 956
  • 10
  • 33

1 Answers1

1

As a solution we are looking for some enforcement policy in Azure Devops which will help us.

I'm afraid there's no such enforcement policy can help meet your requirements.

The common readme file with those wiki links are imported to all the existing repos

However, adding the file to your existing repositories can be achieved by using this API:

POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pushes?api-version=5.1 

For example, I have file: MD_Folder/wiki_links.md in repo testfile1. And I want to add this file to other repos. Here's a sample script I used in pipeline to add the file to different repos.

trigger:
- none

pool:
  vmImage: ubuntu-latest

steps:
- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      import os
      import base64
      import requests
      import json
      
      #get file content from file_path and return it as base64
      
      def get_file_content(file_path):
          with open(file_path, 'rb') as f:
              content = f.read()
          #base64 encode the content
          content = str(base64.b64encode(content))
          #remove the b' and ' from the content
          content = content[2:-1]
          # print(content)
          return content
      
      def get_branch_object_id(organization_name, project_name, personal_access_token, repo_name, branch_name):
          url = "https://dev.azure.com/"+organization_name+"/"+project_name+"/_apis/git/repositories/"+repo_name+"/refs?api-version=7.0"
      
          payload={}
          headers = {
          'Authorization': 'Basic '+personal_access_token,
          }
      
          response = requests.request("GET", url, headers=headers, data=payload)
          json_response = response.json()
          branches = json_response['value']
          for branch in branches:
              if branch['name'] == 'refs/heads/'+branch_name:
                  # print(branch['objectId'])
                  return branch['objectId']
              else:
                  print("Branch not found")
                  return None
          # print(response.text)
      def push_MD_file_to_repo(organization_name, project_name, personal_access_token, repo_name, branch_name, branch_object_id, remote_path, base64_content):
          url = "https://dev.azure.com/"+organization_name+"/"+project_name+"/_apis/git/repositories/"+repo_name+"/pushes?api-version=5.1"
      
          payload = json.dumps({
          "refUpdates": [
              {
              "name": "refs/heads/"+branch_name+"",
              "oldObjectId": ""+branch_object_id+""
              }
          ],
          "commits": [
              {
              "comment": "Added new wiki MD file.",
              "changes": [
                  {
                  "changeType": "add",
                  "item": {
                      "path": ""+remote_path+""
                  },
                  "newContent": {
                      "content": ""+base64_content+"",
                      "contentType": "base64encoded"
                  }
                  }
              ]
              }
          ]
          })
          headers = {
          'Authorization': 'Basic '+personal_access_token,
          'Content-Type': 'application/json',
          }
      
          response = requests.request("POST", url, headers=headers, data=payload)
      
          print(response.text)
      
      def push(repo_name):
          organization_name = "{YourOrgName}"
          project_name = "{YourProjectName}"
          personal_access_token = "{YourEncodedPAT}"
          branch_name = "main"
      
          LocalFile = "MD_Folder/wiki_links.md"
          RemoteFile = "Wiki_Folder/wiki_links.md"
      
          base64_content = get_file_content(LocalFile)
          branch_object_id = get_branch_object_id(organization_name, project_name, personal_access_token, repo_name, branch_name)
          push_MD_file_to_repo(organization_name, project_name, personal_access_token, repo_name, branch_name, branch_object_id, RemoteFile, base64_content)
      
      Repositories = "{TargetRepo1},{TargetRepo2},{TargetRepo3}"
      Repositories = Repositories.split(',')
      for repo in Repositories:
          push(repo)

Run the pipeline and you will find the file in the path: Wiki_Folder/wiki_links.md in the target repos.

enter image description here enter image description here

You can manually enter the target repo and use the comma to separate. Or you could use another script to get the target repo.

  • Great. I will checkout this. Also it it possible for the reverse flow like, from all existing repos, I can scan for a directory called "Architecture" and push that directory to a central repo where we are storing all the "architectures" alone? – Vowneee Dec 15 '22 at 08:55
  • Hi @Vowneee, sorry for the delay response. I'm afraid there's no built-in feature or API can help scan the directories. But we have API that can help get or download a certain file in certain branch. You could refer to this [ticket](https://stackoverflow.com/questions/54228312/how-to-download-a-file-in-a-branch-from-a-git-repo-using-azure-devops-rest-api) for more details. – Evelyn Chen-MSFT Dec 21 '22 at 06:52