Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions lib/gitlab/client/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -715,5 +715,154 @@ def project_deploy_tokens(project, options = {})
def project_languages(project)
get("/projects/#{url_encode project}/languages")
end

# List all project access tokens.
#
# @example
# Gitlab.project_access_tokens(42)
#
# @param [Integer, String] project The ID or path of a project.
# @option options [String] :state Limit by active/inactive state. Optional.
#
# @return [Array<Gitlab::ObjectifiedHash>]
def project_access_tokens(project, options = {})
get("/projects/#{url_encode project}/access_tokens", query: options)
end

# Get a specific project access token.
#
# @example
# Gitlab.project_access_token(42, 1234)
#
# @param [Integer, String] project The ID or path of a project.
# @param [Integer] token_id The ID of the project access token.
#
# @return [Gitlab::ObjectifiedHash] Information about the specified project access token.
def project_access_token(project, token_id, options = {})
get("/projects/#{url_encode project}/access_tokens/#{token_id}", query: options)
end

# Creates a new project access token.
#
# @example
# Gitlab.create_project_access_token(42, 'My Token', ['api'], '2024-12-12', access_level: 40)
#
# @param [Integer, String] project The ID or path of a project.
# @param [String] name The name of the project access token.
# @param [Array] scopes List of scopes of the project access token.
# @param [String] expires_at A date string in the format YYYY-MM-DD.
# @option options [Integer] :access_level Access level. Optional. Defaults to 40.
#
# @return [Gitlab::ObjectifiedHash] Information about the created project access token.
def create_project_access_token(project, name, scopes, expires_at, options = {})
post("/projects/#{url_encode project}/access_tokens", body: { name: name, scopes: scopes, expires_at: expires_at }.merge(options))
end

# Rotate a project access token.
#
# @example
# Gitlab.rotate_project_access_token(42, 1234)
#
# @param [Integer, String] project The ID or path of a project.
# @param [Integer] token_id The ID of the project access token.
# @option options [String] :expires_at A date string in the format YEAR-MONTH-DAY.
#
# @return [Gitlab::ObjectifiedHash] Information about the specified project access token.
def rotate_project_access_token(project, token_id, options = {})
post("/projects/#{url_encode project}/access_tokens/#{token_id}/rotate", query: options)
end

# Revoke a project access token.
#
# @example
# Gitlab.revoke_project_access_token(42, 1234)
#
# @param [Integer, String] project The ID or path of a project.
# @param [Integer] token_id The ID of the project access token.
#
# @return [Gitlab::ObjectifiedHash]
def revoke_project_access_token(project, token_id)
delete("/projects/#{url_encode project}/access_tokens/#{token_id}")
end

# List all project variables.
#
# @example
# Gitlab.project_variables(42)
#
# @param [Integer, String] project The ID or path of a project.
#
# @return [Array<Gitlab::ObjectifiedHash>]
def project_variables(project, options = {})
get("/projects/#{url_encode project}/variables", query: options)
end

# Get a specific project variable.
#
# @example
# Gitlab.project_variable(42, 'MY_VAR')
#
# @param [Integer, String] project The ID or path of a project.
# @param [Integer] key The key of the variable.
#
# @return [Gitlab::ObjectifiedHash] Information about the specified project variable.
def project_variable(project, key, options = {})
get("/projects/#{url_encode project}/variables/#{key}", query: options)
end

# Creates a new project variable.
#
# @example
# Gitlab.create_project_variable(42, 'MY_VAR', 'Value of the variable')
#
# @param [Integer, String] project The ID or path of a project.
# @param [String] key The key of the project variable.
# @param [String] value The value of the project variable.
# @option options [String] :description Description for the project variable.
# @option options [String] :environment_scope The environment scope for the project variable.
# @option options [Boolean] :masked Whether the variable is masked.
# @option options [Boolean] :protected Whether the variable is protected.
# @option options [Boolean] :raw Whether the variable is raw.
# @option options [String] :variable_type The type of the project variable.
#
# @return [Gitlab::ObjectifiedHash] Information about the created project variable.
def create_project_variable(project, key, value, options = {})
post("/projects/#{url_encode project}/variables", body: { key: key, value: value }.merge(options))
end

# Updates an existing project variable.
#
# @example
# Gitlab.update_project_variable(42, 'MY_VAR', 'Value of the variable')
#
# @param [Integer, String] project The ID or path of a project.
# @param [String] key The key of the project variable.
# @param [String] value The value of the project variable.
# @option options [String] :description Description for the project variable.
# @option options [String] :environment_scope The environment scope for the project variable.
# @option options [Boolean] :masked Whether the variable is masked.
# @option options [Boolean] :protected Whether the variable is protected.
# @option options [Boolean] :raw Whether the variable is raw.
# @option options [String] :variable_type The type of the project variable.
# @option options [Hash] :filter Filter the project variables considered.
#
# @return [Gitlab::ObjectifiedHash] Information about the updated project variable.
def update_project_variable(project, key, value, options = {})
put("/projects/#{url_encode project}/variables", body: { key: key, value: value }.merge(options))
end

# Delete a project variable.
#
# @example
# Gitlab.delete_project_variable(42, 'MY_VAR')
#
# @param [Integer, String] project The ID or path of a project.
# @param [Integer] key The key of the project variable.
# @option options [Hash] :filter Filter the project variables considered.
#
# @return [Gitlab::ObjectifiedHash]
def delete_project_variable(project, key, options = {})
delete("/projects/#{url_encode project}/variables/#{key}", query: options)
end
end
end
16 changes: 16 additions & 0 deletions spec/fixtures/project_access_token_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": 2,
"name": "Reporter Token",
"revoked": false,
"created_at": "2024-09-13T03:05:43.075Z",
"scopes": [
"api"
],
"user_id": 2,
"last_used_at": null,
"last_used_at": "2024-09-13T04:10:23.075Z",
"active": true,
"expires_at": "2024-09-20",
"access_level": 20,
"token": "glpat-aRandomStringAsToken"
}
15 changes: 15 additions & 0 deletions spec/fixtures/project_access_token_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": 2,
"name": "Reporter Token",
"revoked": false,
"created_at": "2024-09-13T03:05:43.075Z",
"scopes": [
"api"
],
"user_id": 2,
"last_used_at": null,
"last_used_at": "2024-09-13T04:10:23.075Z",
"active": true,
"expires_at": "2024-09-20",
"access_level": 20
}
33 changes: 33 additions & 0 deletions spec/fixtures/project_access_tokens_get_all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"id": 2,
"name": "Reporter Token",
"revoked": false,
"created_at": "2024-09-13T03:05:43.075Z",
"scopes": [
"api"
],
"user_id": 2,
"last_used_at": null,
"last_used_at": "2024-09-13T04:10:23.075Z",
"active": true,
"expires_at": "2024-09-20",
"access_level": 20
},
{
"id": 3,
"name": "Developer Token",
"revoked": false,
"created_at": "2024-09-13T03:06:20.075Z",
"scopes": [
"read_api",
"read_repository"
],
"user_id": 2,
"last_used_at": null,
"last_used_at": "2024-09-13T06:15:23.075Z",
"active": true,
"expires_at": "2024-09-25",
"access_level": 40
}
]
11 changes: 11 additions & 0 deletions spec/fixtures/project_variable_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"variable_type": "env_var",
"key": "MY_VAR",
"value": "Custom Value",
"hidden": false,
"protected": false,
"masked": false,
"raw": true,
"environment_scope": "*",
"description": "A sample variable"
}
11 changes: 11 additions & 0 deletions spec/fixtures/project_variable_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"variable_type": "env_var",
"key": "MY_VAR",
"value": "Custom Value",
"hidden": false,
"protected": false,
"masked": false,
"raw": true,
"environment_scope": "*",
"description": "A sample variable"
}
24 changes: 24 additions & 0 deletions spec/fixtures/project_variables_get_all.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"variable_type": "env_var",
"key": "MY_VAR",
"value": "Custom Value",
"hidden": false,
"protected": false,
"masked": false,
"raw": true,
"environment_scope": "*",
"description": "A sample variable"
},
{
"variable_type": "env_var",
"key": "MY_VAR_2",
"value": "Another Value",
"hidden": false,
"protected": false,
"masked": false,
"raw": true,
"environment_scope": "*",
"description": "Another sample variable"
}
]
Loading