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.

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