【问题标题】:How to create Wiki Subpages in Azure Devops thru Python?如何通过 Python 在 Azure Devops 中创建 Wiki 子页面?
【发布时间】:2021-04-13 18:12:58
【问题描述】:

我的 Azure devops 页面如下所示:

我有 4 个熊猫数据框。 我需要从每个数据框在 Azure devops wiki 中创建 4 个子页面。 比如说,来自第一个数据帧的 Sub1,来自第二个数据帧的 Sub2,依此类推。

我的结果应该在标签中。结果应如下所示:

是否可以通过 API 创建子页面? 我参考了以下文档。但我无法理解。任何输入都会有所帮助。谢谢。

https://github.com/microsoft/azure-devops-python-samples/blob/main/API%20Samples.ipynb https://docs.microsoft.com/en-us/rest/api/azure/devops/wiki/pages/create%20or%20update?view=azure-devops-rest-6.0

【问题讨论】:

    标签: azure api azure-devops


    【解决方案1】:

    要创建 wiki 子页面,您应该使用 Pages - Create Or Update api,并指定 pathpagename/subpagename。关于如何在 Python 中使用 api,您可以使用Azure DevOps Python API 并参考以下示例:

    def create_or_update_page(self, parameters, project, wiki_identifier, path, version, comment=None, version_descriptor=None):
            """CreateOrUpdatePage.
            [Preview API] Creates or edits a wiki page.
            :param :class:`<WikiPageCreateOrUpdateParameters> <azure.devops.v6_0.wiki.models.WikiPageCreateOrUpdateParameters>` parameters: Wiki create or update operation parameters.
            :param str project: Project ID or project name
            :param str wiki_identifier: Wiki ID or wiki name.
            :param str path: Wiki page path.
            :param String version: Version of the page on which the change is to be made. Mandatory for `Edit` scenario. To be populated in the If-Match header of the request.
            :param str comment: Comment to be associated with the page operation.
            :param :class:`<GitVersionDescriptor> <azure.devops.v6_0.wiki.models.GitVersionDescriptor>` version_descriptor: GitVersionDescriptor for the page. (Optional in case of ProjectWiki).
            :rtype: :class:`<WikiPageResponse> <azure.devops.v6_0.wiki.models.WikiPageResponse>`
            """
            route_values = {}
            if project is not None:
                route_values['project'] = self._serialize.url('project', project, 'str')
            if wiki_identifier is not None:
                route_values['wikiIdentifier'] = self._serialize.url('wiki_identifier', wiki_identifier, 'str')
            query_parameters = {}
            if path is not None:
                query_parameters['path'] = self._serialize.query('path', path, 'str')
            if comment is not None:
                query_parameters['comment'] = self._serialize.query('comment', comment, 'str')
            if version_descriptor is not None:
                if version_descriptor.version_type is not None:
                    query_parameters['versionDescriptor.versionType'] = version_descriptor.version_type
                if version_descriptor.version is not None:
                    query_parameters['versionDescriptor.version'] = version_descriptor.version
                if version_descriptor.version_options is not None:
                    query_parameters['versionDescriptor.versionOptions'] = version_descriptor.version_options
            additional_headers = {}
            if version is not None:
                additional_headers['If-Match'] = version
            content = self._serialize.body(parameters, 'WikiPageCreateOrUpdateParameters')
            response = self._send(http_method='PUT',
                                  location_id='25d3fbc7-fe3d-46cb-b5a5-0b6f79caf27b',
                                  version='6.0-preview.1',
                                  route_values=route_values,
                                  query_parameters=query_parameters,
                                  additional_headers=additional_headers,
                                  content=content)
            response_object = models.WikiPageResponse()
            response_object.page = self._deserialize('WikiPage', response)
            response_object.eTag = response.headers.get('ETag')
            return response_object
    

    更多详情,您可以参考以下链接:

    https://github.com/microsoft/azure-devops-python-api/blob/451cade4c475482792cbe9e522c1fee32393139e/azure-devops/azure/devops/v6_0/wiki/wiki_client.py#L107

    【讨论】:

    • 是的。我已经通过了这个。但我无法获得该方法所需的完整流程 - 身份验证、参数(示例会很有帮助)。
    【解决方案2】:

    可以用rest api实现

    import requests
    import base64
    import pandas as pd
    
    pat = 'TO BE FILLED BY YOU'  #CONFIDENTIAL
    authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')
    
    headers = {
        'Accept': 'application/json',
        'Authorization': 'Basic '+authorization
    }
    
    df = pd.read_csv('sf_metadata.csv')  #METADATA OF 3 TABLES 
    df.set_index('TABLE_NAME', inplace=True,drop=True)
    df_test1 = df.loc['CURRENCY'] 
    
    
    x1 = df_test1.to_html()  # CONVERTING TO HTML TO PRESERVE THE TABULAR STRUCTURE
    
    #JSON FOR PUT REQUEST
    SamplePage1 = {
      "content": x1
    }
    
    #API CALLS TO AZURE DEVOPS WIKI 
    response = requests.put(
        url="https://dev.azure.com/xxx/yyy/_apis/wiki/wikis/yyy.wiki/pages?path=SamplePag2&api-version=6.0", headers=headers,json=SamplePage1)
    print(response.text)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多