Created
July 22, 2020 16:34
-
-
Save a11smiles/771129a43f03f09d9bee16651e920ee7 to your computer and use it in GitHub Desktop.
Takes a newly created project, uploads a README.md, then sets it as the project's description. This uses the AzDO Python library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from azure.devops.connection import Connection | |
from azure.devops.v5_1.core import TeamProject | |
from azure.devops.v5_1.git import GitPush, GitRefUpdate, GitCommit | |
from msrest.authentication import BasicAuthentication | |
from msrest.universal_http import ClientRequest | |
# Authenticate | |
credentials = BasicAuthentication('', token) | |
connection = Connection(base_url=f'https://dev.azure.com/{org}', creds=credentials) | |
# Find the project (ex. 'My_Project') | |
core_client = self.clients.get_core_client() | |
projects = core_client.get_projects() | |
for p in projects.value: | |
if p.name == 'My_Project': | |
project = p | |
# Read README.md contents | |
fs = helpers.FileSystem() | |
content = fs.read_file('./README.md') | |
# Create the initial push/commit | |
git_client = self.clients.get_git_client() | |
repo = git_client.get_repositories(project.id)[0] | |
ref_update = GitRefUpdate(name="refs/heads/master", old_object_id="0000000000000000000000000000000000000000", repository_id=repo.id) | |
changes = [ | |
{ | |
"changeType": "add", | |
"item": { | |
"path": "/README.md" | |
}, | |
"newContent": { | |
"content": content, | |
"contentType": "rawtext" | |
} | |
} | |
] | |
commit = GitCommit(comment="Initial commit.", changes=changes) | |
push = GitPush(commits=[commit], ref_updates=[ref_update], repository=repo) | |
git_client.create_push(push, repo.id) | |
# Manually set the project description to the README.md of the newly created project/repo | |
headers = {"content-type": "application/json; charset=utf-8'"} | |
client_request = ClientRequest("PATCH", f'https://dev.azure.com/{org}/_apis/Settings/project/{project.id}/Entries/host?api-version=5.1-preview.1') # <----- Non-publicized URL | |
payload = { "VersionControl/ProjectOverview/DefaultRepository": repo.id } # <----- NOTE: Changing this payload is how to change the description to a WIKI page | |
response = self.connection._client.send(request=client_request, headers=headers, content=payload) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment