Gitlab V4 API学习

环境

gitlab 在线地址 https://gitlab.awbeci.com/ https://gitlab.com/gitlab-com
对应的v4版本的API文档 https://gitlab.awbeci.com/help/api/api_resources.md

Authentication

Most API requests require authentication, or only return public data when authentication isn’t provided. When authentication is not required, the documentation for each endpoint specifies this. For example, the /projects/:id endpoint does not require authentication.【还需要是public的】

Merge Request API

1、获取项目所有的MR
  1. Delete a merge request thread note
    DELETE /projects/:id/merge_requests/:merge_request_iid/discussions/:discussion_id/notes/:note_id
  2. Modify an existing merge request thread note
    PUT /projects/:id/merge_requests/:merge_request_iid/discussions/:discussion_id/notes/:note_id
  3. Add note to existing merge request thread
    POST /projects/:id/merge_requests/:merge_request_iid/discussions/:discussion_id/notes
  4. Resolve a merge request thread
    PUT /projects/:id/merge_requests/:merge_request_iid/discussions/:discussion_id
  5. Create new merge request thread
    POST /projects/:id/merge_requests/:merge_request_iid/discussions
  6. Get single merge request discussion item
    GET /projects/:id/merge_requests/:merge_request_iid/discussions/:discussion_id
curl --location --request GET 'https://gitlab.awbeci.com/api/v4/projects/62/merge_requests'
[
    {
        "id": 34,
        "iid": 1,
        "project_id": 62,
        "title": "WIP: Resolve \"issue01\"",
        "description": "Closes #1",
        "state": "opened",
        "created_at": "2021-05-23T06:25:22.054Z",
        "updated_at": "2021-05-23T07:48:13.435Z",
        "merged_by": null,
        "merged_at": null,
        "closed_by": null,
        "closed_at": null,
        "target_branch": "master",
        "source_branch": "1-issue01",
        "user_notes_count": 1,
        "upvotes": 0,
        "downvotes": 1,
        "assignee": null,
        "author": {
            "id": 25,
            "name": "xiaolixi",
            "username": "xiaolixi",
            "state": "active",
            "avatar_url": "https://gitlab.awbeci.com/uploads/-/system/user/avatar/25/avatar.png",
            "web_url": "https://gitlab.awbeci.com/xiaolixi"
        },
        "assignees": [],
        "source_project_id": 62,
        "target_project_id": 62,
        "labels": [],
        "work_in_progress": true,
        "milestone": null,
        "merge_when_pipeline_succeeds": false,
        "merge_status": "cannot_be_merged",
        "sha": "5d999e9db08c938db35e1aa126ecf3eb8fc61b3c",
        "merge_commit_sha": null,
        "squash_commit_sha": null,
        "discussion_locked": null,
        "should_remove_source_branch": null,
        "force_remove_source_branch": true,
        "reference": "!1",
        "references": {
            "short": "!1",
            "relative": "!1",
            "full": "tomgroup/tomproject01!1"
        },
        "web_url": "https://gitlab.awbeci.com/tomgroup/tomproject01/-/merge_requests/1",
        "time_stats": {
            "time_estimate": 0,
            "total_time_spent": 0,
            "human_time_estimate": null,
            "human_total_time_spent": null
        },
        "squash": false,
        "task_completion_status": {
            "count": 0,
            "completed_count": 0
        },
        "has_conflicts": true,
        "blocking_discussions_resolved": true,
        "approvals_before_merge": null
    }
]
2、获取MR ID详情
curl --location --request GET 'https://gitlab.awbeci.com/api/v4/projects/62/merge_requests/1'
{
    "id": 34,
    "iid": 1,
    "project_id": 62,
    "title": "WIP: Resolve \"issue01\"",
    "description": "Closes #1",
    "state": "opened",
    "created_at": "2021-05-23T06:25:22.054Z",
    "updated_at": "2021-05-23T07:48:13.435Z",
    "merged_by": null,
    "merged_at": null,
    "closed_by": null,
    "closed_at": null,
    "target_branch": "master",
    "source_branch": "1-issue01",
    "user_notes_count": 1,
    "upvotes": 0,
    "downvotes": 1,
    "assignee": null,
    "author": {
        "id": 25,
        "name": "xiaolixi",
        "username": "xiaolixi",
        "state": "active",
        "avatar_url": "https://gitlab.awbeci.com/uploads/-/system/user/avatar/25/avatar.png",
        "web_url": "https://gitlab.awbeci.com/xiaolixi"
    },
    "assignees": [],
    "source_project_id": 62,
    "target_project_id": 62,
    "labels": [],
    "work_in_progress": true,
    "milestone": null,
    "merge_when_pipeline_succeeds": false,
    "merge_status": "cannot_be_merged",
    "sha": "5d999e9db08c938db35e1aa126ecf3eb8fc61b3c",
    "merge_commit_sha": null,
    "squash_commit_sha": null,
    "discussion_locked": null,
    "should_remove_source_branch": null,
    "force_remove_source_branch": true,
    "reference": "!1",
    "references": {
        "short": "!1",
        "relative": "!1",
        "full": "tomgroup/tomproject01!1"
    },
    "web_url": "https://gitlab.awbeci.com/tomgroup/tomproject01/-/merge_requests/1",
    "time_stats": {
        "time_estimate": 0,
        "total_time_spent": 0,
        "human_time_estimate": null,
        "human_total_time_spent": null
    },
    "squash": false,
    "task_completion_status": {
        "count": 0,
        "completed_count": 0
    },
    "has_conflicts": true,
    "blocking_discussions_resolved": true,
    "approvals_before_merge": null,
    "subscribed": true,
    "changes_count": null,
    "latest_build_started_at": null,
    "latest_build_finished_at": null,
    "first_deployed_to_production_at": null,
    "pipeline": null,
    "head_pipeline": null,
    "diff_refs": {
        "base_sha": "5d999e9db08c938db35e1aa126ecf3eb8fc61b3c",
        "head_sha": "5d999e9db08c938db35e1aa126ecf3eb8fc61b3c",
        "start_sha": "5d999e9db08c938db35e1aa126ecf3eb8fc61b3c"
    },
    "merge_error": null,
    "first_contribution": false,
    "user": {
        "can_merge": true
    }
}
3、获取项目的MR ID的所有讨论
curl --location --request GET 'https://gitlab.awbeci.com/api/v4/projects/62/merge_requests/1/discussions' \
--header 'PRIVATE-TOKEN: EooWXSL3KHSneaZBpzuv'
[
    {
        "id": "268f991e79b1d7b9d480a44e2b04bfca9a98384d",
        "individual_note": true,
        "notes": [
            {
                "id": 89,
                "type": null,
                "body": "first diss 透明色",
                "attachment": null,
                "author": {
                    "id": 25,
                    "name": "xiaolixi",
                    "username": "xiaolixi",
                    "state": "active",
                    "avatar_url": "https://gitlab.awbeci.com/uploads/-/system/user/avatar/25/avatar.png",
                    "web_url": "https://gitlab.awbeci.com/xiaolixi"
                },
                "created_at": "2021-05-23T07:48:13.419Z",
                "updated_at": "2021-05-23T07:48:13.419Z",
                "system": false,
                "noteable_id": 34,
                "noteable_type": "MergeRequest",
                "resolvable": false,
                "confidential": null,
                "noteable_iid": 1,
                "commands_changes": {}
            }
        ]
    },
    {
        "id": "0d8cdc72d9c425e44be27a84219cd486ce858578",
        "individual_note": true,
        "notes": [
            {
                "id": 90,
                "type": null,
                "body": "第二个讨论",
                "attachment": null,
                "author": {
                    "id": 25,
                    "name": "xiaolixi",
                    "username": "xiaolixi",
                    "state": "active",
                    "avatar_url": "https://gitlab.awbeci.com/uploads/-/system/user/avatar/25/avatar.png",
                    "web_url": "https://gitlab.awbeci.com/xiaolixi"
                },
                "created_at": "2021-05-23T08:20:07.315Z",
                "updated_at": "2021-05-23T08:20:07.315Z",
                "system": false,
                "noteable_id": 34,
                "noteable_type": "MergeRequest",
                "resolvable": false,
                "confidential": null,
                "noteable_iid": 1,
                "commands_changes": {}
            }
        ]
    }
]
4、创建项目的MR的讨论
curl --location --request POST 'https://gitlab.awbeci.com/api/v4/projects/62/merge_requests/1/discussions?body=MR%E7%9A%84%E8%AF%84%E8%AE%BA%EF%BC%8C%E5%8F%AF%E4%BB%A5%E5%8A%A0notes%E7%9A%84%E8%AF%84%E8%AE%BA'
{
    "id": "147509142b2d7bd1b24ba1594d5f168ff4ee9b0f",
    "individual_note": false,
    "notes": [
        {
            "id": 91,
            "type": "DiscussionNote",
            "body": "MR的评论,可以加notes的评论",
            "attachment": null,
            "author": {
                "id": 25,
                "name": "xiaolixi",
                "username": "xiaolixi",
                "state": "active",
                "avatar_url": "https://gitlab.awbeci.com/uploads/-/system/user/avatar/25/avatar.png",
                "web_url": "https://gitlab.awbeci.com/xiaolixi"
            },
            "created_at": "2021-05-23T08:41:21.641Z",
            "updated_at": "2021-05-23T08:41:21.641Z",
            "system": false,
            "noteable_id": 34,
            "noteable_type": "MergeRequest",
            "resolvable": true,
            "resolved": false,
            "resolved_by": null,
            "confidential": null,
            "noteable_iid": 1,
            "commands_changes": {}
        }
    ]
}

在这里插入图片描述

Wiki Page API
  • 列出项目所有的wiki GET /projects/:id/wikis
    • curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" https://gitlab.example.com/api/v4/projects/1/wikis?with_content=1
  • 获取一个wiki page GET /projects/:id/wikis/:slug
    • curl --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" https://gitlab.example.com/api/v4/projects/1/wikis/home
  • 创建一个wiki page POST /projects/:id/wikis
    • curl --data "format=rdoc&title=Hello&content=Hello world" --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/projects/1/wikis"
  • 编辑一个wiki page PUT /projects/:id/wikis/:slug
    • curl --request PUT --data "format=rdoc&content=documentation&title=Docs" --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/projects/1/wikis/foo"
  • 删除一个wiki page DELETE /projects/:id/wikis/:slug
    • curl --request DELETE --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/projects/1/wikis/foo"
  • 上传一个附件到wiki仓库 POST /projects/:id/wikis/attachments,上传完之后就可以在MD中使用了。
    • curl --request POST --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" --form "file=@dk.png" https://gitlab.example.com/api/v4/projects/1/wikis/attachments
5、
5、
5、
5、
5、
5、
5、
5、
5、
5、
5、
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐