Skip to content

Commit

Permalink
Gitlab added cancel pipeline command (demisto#29897)
Browse files Browse the repository at this point in the history
* add new command to cancel a gitlab pipeline

* RN and docs

* RN

* docker

* RN

* format

* doc review

* mypy fixes

* UT
  • Loading branch information
MosheEichler authored Sep 27, 2023
1 parent c71058a commit 37d7c4c
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 12 deletions.
44 changes: 44 additions & 0 deletions Packs/GitLab/Integrations/GitLabv2/GitLabv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,19 @@ def gitlab_trigger_pipeline(self, project_id: str, data: dict) -> dict:
suffix = f'projects/{project_id}/trigger/pipeline'
return self._http_request('POST', suffix, data=data)

def gitlab_cancel_pipeline(self, project_id: str, pipeline_id: str) -> dict:
"""Cancel a pipeline on GitLab.
Args:
project_id: Project ID on which to cancel the pipeline.
pipeline_id: Pipeline ID to cancel.
Returns:
dict: The response in JSON format.
"""
suffix = f'/projects/{project_id}/pipelines/{pipeline_id}/cancel'
return self._http_request('POST', suffix)


''' HELPER FUNCTIONS '''

Expand Down Expand Up @@ -1745,6 +1758,36 @@ def gitlab_trigger_pipeline_command(client: Client, args: dict[str, str]) -> Com
)


def gitlab_cancel_pipeline_command(client: Client, args: dict[str, str]) -> CommandResults:
"""
Cancels a GitLab pipeline.
Args:
client (Client): Client to perform calls to GitLab services.
args (dict) XSOAR arguments:
- 'project_id': Project ID on which to cancel the pipeline.
- 'pipeline_id': The pipline ID to cancel.
Returns:
(CommandResults).
"""
project_id = args.get('project_id') or client.project_id
if not (pipeline_id := args.get('pipeline_id', '')):
return_error("The pipline id is required in order to cancel it")

response = client.gitlab_cancel_pipeline(project_id, pipeline_id)

outputs = {k: v for k, v in response.items() if k in PIPELINE_FIELDS_TO_EXTRACT}
human_readable = tableToMarkdown('GitLab Pipeline', outputs, removeNull=True)

return CommandResults(
outputs_prefix='GitLab.Pipeline',
outputs_key_field='id',
outputs=outputs,
raw_response=response,
readable_output=human_readable
)


def check_for_html_in_error(e: str):
"""
Args:
Expand Down Expand Up @@ -1807,6 +1850,7 @@ def main() -> None: # pragma: no cover
'gitlab-jobs-list': gitlab_jobs_list_command,
'gitlab-artifact-get': gitlab_artifact_get_command,
'gitlab-trigger-pipeline': gitlab_trigger_pipeline_command,
'gitlab-cancel-pipeline': gitlab_cancel_pipeline_command,
}

try:
Expand Down
82 changes: 71 additions & 11 deletions Packs/GitLab/Integrations/GitLabv2/GitLabv2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ configuration:
name: proxy
type: 8
required: false
description: Integration to GitLab API
description: Integration to GitLab API.
display: GitLab v2
name: GitLabv2
script:
Expand Down Expand Up @@ -194,9 +194,9 @@ script:
description: Whether to filter confidential or public issues.
type: Boolean
- name: created_after
description: Return issues created on or after the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z
description: Return issues created on or after the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z.
- name: created_before
description: Return issues created on or before the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z
description: Return issues created on or before the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z.
- auto: PREDEFINED
name: partial_response
defaultValue: 'true'
Expand Down Expand Up @@ -263,14 +263,14 @@ script:
- asc
- auto: PREDEFINED
name: state
description: Return all issues or just those that are opened or closed
description: Return all issues or just those that are opened or closed.
predefined:
- opened
- closed
- name: updated_after
description: Return issues updated on or after the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z
description: Return issues updated on or after the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z.
- name: updated_before
description: Return issues updated on or before the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z
description: Return issues updated on or before the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z.
- defaultValue: 50
name: limit
description: Maximum number of results to return.
Expand Down Expand Up @@ -586,13 +586,13 @@ script:
- name: labels
description: Return merge requests matching a comma-separated list of labels. None lists all merge requests with no labels. Any lists all merge requests with at least one label. Predefined names are case-insensitive.
- name: created_after
description: Return merge requests created on or after the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z
description: Return merge requests created on or after the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z.
- name: created_before
description: Return merge requests created on or before the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z
description: Return merge requests created on or before the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z.
- name: updated_after
description: Return merge requests updated on or after the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z
description: Return merge requests updated on or after the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z.
- name: updated_before
description: Return merge requests updated on or before the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z
description: Return merge requests updated on or before the given time in timestamp format (<number> <time unit>, e.g., 12 hours, 7 days), or Format - YYYY-MM-DDThh:mm:ssZ. Example - 2020-02-02T01:02:03Z.
- auto: PREDEFINED
name: scope
description: Return merge requests for the given scope.
Expand Down Expand Up @@ -3327,11 +3327,71 @@ script:
- contextPath: GitLab.Pipeline.user.web_url
description: Web URL of the user who triggered the pipeline.
type: String
- name: gitlab-cancel-pipeline
arguments:
- description: Project ID on which to cancel the pipeline.
name: project_id
- description: The pipline ID to cancel.
name: pipeline_id
required: true
description: Cancels a GitLab pipeline.
outputs:
- contextPath: GitLab.Pipeline.id
description: Pipeline ID.
type: Number
- contextPath: GitLab.Pipeline.project_id
description: Project ID that the pipeline belongs to.
type: Number
- contextPath: GitLab.Pipeline.status
description: Status of the pipeline.
type: String
- contextPath: GitLab.Pipeline.ref
description: Reference of the pipeline.
type: String
- contextPath: GitLab.Pipeline.sha
description: SHA of the pipeline.
type: String
- contextPath: GitLab.Pipeline.created_at
description: Time when the pipeline was created.
type: Date
- contextPath: GitLab.Pipeline.updated_at
description: Time when the pipeline was last updated.
type: Date
- contextPath: GitLab.Pipeline.started_at
description: Time when the pipeline was started.
type: Date
- contextPath: GitLab.Pipeline.finished_at
description: Time when the pipeline was finished.
type: Date
- contextPath: GitLab.Pipeline.duration
description: Duration of the pipeline in seconds.
type: Number
- contextPath: GitLab.Pipeline.web_url
description: Web URL of the pipeline.
type: String
- contextPath: GitLab.Pipeline.user.name
description: Name of the user who triggered the pipeline.
type: String
- contextPath: GitLab.Pipeline.user.username
description: Username that triggered the pipeline.
type: String
- contextPath: GitLab.Pipeline.user.id
description: ID of the user who triggered the pipeline.
type: Number
- contextPath: GitLab.Pipeline.user.state
description: State of the user who triggered the pipeline.
type: String
- contextPath: GitLab.Pipeline.user.avatar_url
description: Avatar URL of the user who triggered the pipeline.
type: String
- contextPath: GitLab.Pipeline.user.web_url
description: Web URL of the user who triggered the pipeline.
type: String
runonce: false
script: '-'
type: python
subtype: python3
dockerimage: demisto/python3:3.10.13.72123
dockerimage: demisto/python3:3.10.13.74666
fromversion: 6.5.0
tests:
- Test-GitLab-v2
31 changes: 31 additions & 0 deletions Packs/GitLab/Integrations/GitLabv2/GitLabv2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,3 +1027,34 @@ def test_trigger_pipeline(mocker, trigger_token, args, expected_result):
assert command_result.outputs == expected_outputs
assert command_result.outputs.get('ref') == args.get('ref_branch')
assert command_result.outputs.get('project_id') == args.get('project_id')


def test_cancel_pipeline(mocker):
"""
Given:
- GitLab client and demisto args
When:
- gitlab_cancel_pipeline_command
Then:
- The response is as expected
"""
from GitLabv2 import Client, gitlab_cancel_pipeline_command
args = {'project_id': 2222, 'pipeline_id': 1}
expected_result = util_load_json('test_data/commands_test_data.json').get('cancel_pipeline')
client = Client(project_id=1234,
base_url="server_url",
verify=False,
proxy=False,
headers={'PRIVATE-TOKEN': 'api_key'})
expected_outputs: list[dict] = expected_result['expected_outputs']
expected_prefix: str = expected_result['expected_prefix']
expected_key_field: str = expected_result['expected_key_field']
mocker.patch.object(Client, '_http_request', return_value=expected_result['mock_response'])

command_result = gitlab_cancel_pipeline_command(client, args)

assert command_result.outputs_prefix == expected_prefix
assert command_result.outputs_key_field == expected_key_field
assert command_result.outputs == expected_outputs
assert command_result.outputs.get('project_id') == int(args.get('project_id'))
assert command_result.outputs.get('status') == 'canceled'
38 changes: 38 additions & 0 deletions Packs/GitLab/Integrations/GitLabv2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2201,3 +2201,41 @@ Triggers a GitLab pipeline on a selected project and branch.
| GitLab.Pipeline.user.state | String | State of the user who triggered the pipeline. |
| GitLab.Pipeline.user.avatar_url | String | Avatar URL of the user who trigerred the pipeline. |
| GitLab.Pipeline.user.web_url | String | Web URL of the user who triggered the pipeline. |

### gitlab-cancel-pipeline

***
Cancels a GitLab pipeline.

#### Base Command

`gitlab-cancel-pipeline`

#### Input

| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| project_id | Project ID on which to run the pipeline. | Optional |
| pipeline_id | The pipline ID to cancel. | Optional |

#### Context Output

| **Path** | **Type** | **Description** |
| --- | --- | --- |
| GitLab.Pipeline.id | Number | Pipeline ID. |
| GitLab.Pipeline.project_id | Number | Project ID that the pipeline belongs to. |
| GitLab.Pipeline.status | String | Status of the pipeline. |
| GitLab.Pipeline.ref | String | Reference of the pipeline. |
| GitLab.Pipeline.sha | String | SHA of the pipeline. |
| GitLab.Pipeline.created_at | Date | Time when the pipeline was created. |
| GitLab.Pipeline.updated_at | Date | Time when the pipeline was last updated. |
| GitLab.Pipeline.started_at | Date | Time when the pipeline was started. |
| GitLab.Pipeline.finished_at | Date | Time when the pipeline was finished. |
| GitLab.Pipeline.duration | Number | Duration of the pipeline in seconds. |
| GitLab.Pipeline.web_url | String | Web URL of the pipeline. |
| GitLab.Pipeline.user.name | String | Name of the user who triggered the pipeline. |
| GitLab.Pipeline.user.username | String | Username that triggered the pipeline. |
| GitLab.Pipeline.user.id | Number | ID of the user who triggered the pipeline. |
| GitLab.Pipeline.user.state | String | State of the user who triggered the pipeline. |
| GitLab.Pipeline.user.avatar_url | String | Avatar URL of the user who trigerred the pipeline. |
| GitLab.Pipeline.user.web_url | String | Web URL of the user who triggered the pipeline. |
Original file line number Diff line number Diff line change
Expand Up @@ -1243,5 +1243,31 @@
"expected_prefix": "GitLab.Pipeline",
"expected_url_mock_suffix": "projects/2222/pipelines",
"expected_key_field": "id"
},
"cancel_pipeline": {
"mock_response": {
"id": 1,
"project_id": 2222,
"sha": "skd5h31245ljkasl4kj45l324",
"ref": "master",
"status": "canceled",
"source": "trigger",
"created_at": "2021-06-15T14:31:29.607Z",
"updated_at": "2021-06-15T14:31:32.964Z",
"web_url": "https://server_url/project/-/pipelines/1"
},
"expected_outputs": {
"id": 1,
"project_id": 2222,
"sha": "skd5h31245ljkasl4kj45l324",
"ref": "master",
"status": "canceled",
"created_at": "2021-06-15T14:31:29.607Z",
"updated_at": "2021-06-15T14:31:32.964Z",
"web_url": "https://server_url/project/-/pipelines/1"
},
"expected_prefix": "GitLab.Pipeline",
"expected_url_mock_suffix": "projects/2222/pipelines",
"expected_key_field": "id"
}
}
5 changes: 5 additions & 0 deletions Packs/GitLab/ReleaseNotes/2_2_18.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#### Integrations
##### GitLab v2
- Updated the Docker image to: *demisto/python3:3.10.13.74666*.
- Added a new command ***gitlab-cancel-pipeline*** that enables canceling a GitLab pipeline.
2 changes: 1 addition & 1 deletion Packs/GitLab/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "GitLab",
"description": "Pack for handling gitlab operations",
"support": "xsoar",
"currentVersion": "2.2.17",
"currentVersion": "2.2.18",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down

0 comments on commit 37d7c4c

Please sign in to comment.