Skip to content

Commit 784c896

Browse files
MosheEichlermaimorag
authored andcommitted
Gitlab added cancel pipeline command (demisto#29897)
* add new command to cancel a gitlab pipeline * RN and docs * RN * docker * RN * format * doc review * mypy fixes * UT
1 parent f5e0680 commit 784c896

File tree

7 files changed

+216
-12
lines changed

7 files changed

+216
-12
lines changed

Packs/GitLab/Integrations/GitLabv2/GitLabv2.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,19 @@ def gitlab_trigger_pipeline(self, project_id: str, data: dict) -> dict:
328328
suffix = f'projects/{project_id}/trigger/pipeline'
329329
return self._http_request('POST', suffix, data=data)
330330

331+
def gitlab_cancel_pipeline(self, project_id: str, pipeline_id: str) -> dict:
332+
"""Cancel a pipeline on GitLab.
333+
334+
Args:
335+
project_id: Project ID on which to cancel the pipeline.
336+
pipeline_id: Pipeline ID to cancel.
337+
338+
Returns:
339+
dict: The response in JSON format.
340+
"""
341+
suffix = f'/projects/{project_id}/pipelines/{pipeline_id}/cancel'
342+
return self._http_request('POST', suffix)
343+
331344

332345
''' HELPER FUNCTIONS '''
333346

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

17471760

1761+
def gitlab_cancel_pipeline_command(client: Client, args: dict[str, str]) -> CommandResults:
1762+
"""
1763+
Cancels a GitLab pipeline.
1764+
Args:
1765+
client (Client): Client to perform calls to GitLab services.
1766+
args (dict) XSOAR arguments:
1767+
- 'project_id': Project ID on which to cancel the pipeline.
1768+
- 'pipeline_id': The pipline ID to cancel.
1769+
1770+
Returns:
1771+
(CommandResults).
1772+
"""
1773+
project_id = args.get('project_id') or client.project_id
1774+
if not (pipeline_id := args.get('pipeline_id', '')):
1775+
return_error("The pipline id is required in order to cancel it")
1776+
1777+
response = client.gitlab_cancel_pipeline(project_id, pipeline_id)
1778+
1779+
outputs = {k: v for k, v in response.items() if k in PIPELINE_FIELDS_TO_EXTRACT}
1780+
human_readable = tableToMarkdown('GitLab Pipeline', outputs, removeNull=True)
1781+
1782+
return CommandResults(
1783+
outputs_prefix='GitLab.Pipeline',
1784+
outputs_key_field='id',
1785+
outputs=outputs,
1786+
raw_response=response,
1787+
readable_output=human_readable
1788+
)
1789+
1790+
17481791
def check_for_html_in_error(e: str):
17491792
"""
17501793
Args:
@@ -1807,6 +1850,7 @@ def main() -> None: # pragma: no cover
18071850
'gitlab-jobs-list': gitlab_jobs_list_command,
18081851
'gitlab-artifact-get': gitlab_artifact_get_command,
18091852
'gitlab-trigger-pipeline': gitlab_trigger_pipeline_command,
1853+
'gitlab-cancel-pipeline': gitlab_cancel_pipeline_command,
18101854
}
18111855

18121856
try:

Packs/GitLab/Integrations/GitLabv2/GitLabv2.yml

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ configuration:
3232
name: proxy
3333
type: 8
3434
required: false
35-
description: Integration to GitLab API
35+
description: Integration to GitLab API.
3636
display: GitLab v2
3737
name: GitLabv2
3838
script:
@@ -194,9 +194,9 @@ script:
194194
description: Whether to filter confidential or public issues.
195195
type: Boolean
196196
- name: created_after
197-
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
197+
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.
198198
- name: created_before
199-
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
199+
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.
200200
- auto: PREDEFINED
201201
name: partial_response
202202
defaultValue: 'true'
@@ -263,14 +263,14 @@ script:
263263
- asc
264264
- auto: PREDEFINED
265265
name: state
266-
description: Return all issues or just those that are opened or closed
266+
description: Return all issues or just those that are opened or closed.
267267
predefined:
268268
- opened
269269
- closed
270270
- name: updated_after
271-
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
271+
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.
272272
- name: updated_before
273-
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
273+
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.
274274
- defaultValue: 50
275275
name: limit
276276
description: Maximum number of results to return.
@@ -586,13 +586,13 @@ script:
586586
- name: labels
587587
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.
588588
- name: created_after
589-
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
589+
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.
590590
- name: created_before
591-
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
591+
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.
592592
- name: updated_after
593-
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
593+
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.
594594
- name: updated_before
595-
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
595+
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.
596596
- auto: PREDEFINED
597597
name: scope
598598
description: Return merge requests for the given scope.
@@ -3327,11 +3327,71 @@ script:
33273327
- contextPath: GitLab.Pipeline.user.web_url
33283328
description: Web URL of the user who triggered the pipeline.
33293329
type: String
3330+
- name: gitlab-cancel-pipeline
3331+
arguments:
3332+
- description: Project ID on which to cancel the pipeline.
3333+
name: project_id
3334+
- description: The pipline ID to cancel.
3335+
name: pipeline_id
3336+
required: true
3337+
description: Cancels a GitLab pipeline.
3338+
outputs:
3339+
- contextPath: GitLab.Pipeline.id
3340+
description: Pipeline ID.
3341+
type: Number
3342+
- contextPath: GitLab.Pipeline.project_id
3343+
description: Project ID that the pipeline belongs to.
3344+
type: Number
3345+
- contextPath: GitLab.Pipeline.status
3346+
description: Status of the pipeline.
3347+
type: String
3348+
- contextPath: GitLab.Pipeline.ref
3349+
description: Reference of the pipeline.
3350+
type: String
3351+
- contextPath: GitLab.Pipeline.sha
3352+
description: SHA of the pipeline.
3353+
type: String
3354+
- contextPath: GitLab.Pipeline.created_at
3355+
description: Time when the pipeline was created.
3356+
type: Date
3357+
- contextPath: GitLab.Pipeline.updated_at
3358+
description: Time when the pipeline was last updated.
3359+
type: Date
3360+
- contextPath: GitLab.Pipeline.started_at
3361+
description: Time when the pipeline was started.
3362+
type: Date
3363+
- contextPath: GitLab.Pipeline.finished_at
3364+
description: Time when the pipeline was finished.
3365+
type: Date
3366+
- contextPath: GitLab.Pipeline.duration
3367+
description: Duration of the pipeline in seconds.
3368+
type: Number
3369+
- contextPath: GitLab.Pipeline.web_url
3370+
description: Web URL of the pipeline.
3371+
type: String
3372+
- contextPath: GitLab.Pipeline.user.name
3373+
description: Name of the user who triggered the pipeline.
3374+
type: String
3375+
- contextPath: GitLab.Pipeline.user.username
3376+
description: Username that triggered the pipeline.
3377+
type: String
3378+
- contextPath: GitLab.Pipeline.user.id
3379+
description: ID of the user who triggered the pipeline.
3380+
type: Number
3381+
- contextPath: GitLab.Pipeline.user.state
3382+
description: State of the user who triggered the pipeline.
3383+
type: String
3384+
- contextPath: GitLab.Pipeline.user.avatar_url
3385+
description: Avatar URL of the user who triggered the pipeline.
3386+
type: String
3387+
- contextPath: GitLab.Pipeline.user.web_url
3388+
description: Web URL of the user who triggered the pipeline.
3389+
type: String
33303390
runonce: false
33313391
script: '-'
33323392
type: python
33333393
subtype: python3
3334-
dockerimage: demisto/python3:3.10.13.72123
3394+
dockerimage: demisto/python3:3.10.13.74666
33353395
fromversion: 6.5.0
33363396
tests:
33373397
- Test-GitLab-v2

Packs/GitLab/Integrations/GitLabv2/GitLabv2_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,3 +1027,34 @@ def test_trigger_pipeline(mocker, trigger_token, args, expected_result):
10271027
assert command_result.outputs == expected_outputs
10281028
assert command_result.outputs.get('ref') == args.get('ref_branch')
10291029
assert command_result.outputs.get('project_id') == args.get('project_id')
1030+
1031+
1032+
def test_cancel_pipeline(mocker):
1033+
"""
1034+
Given:
1035+
- GitLab client and demisto args
1036+
When:
1037+
- gitlab_cancel_pipeline_command
1038+
Then:
1039+
- The response is as expected
1040+
"""
1041+
from GitLabv2 import Client, gitlab_cancel_pipeline_command
1042+
args = {'project_id': 2222, 'pipeline_id': 1}
1043+
expected_result = util_load_json('test_data/commands_test_data.json').get('cancel_pipeline')
1044+
client = Client(project_id=1234,
1045+
base_url="server_url",
1046+
verify=False,
1047+
proxy=False,
1048+
headers={'PRIVATE-TOKEN': 'api_key'})
1049+
expected_outputs: list[dict] = expected_result['expected_outputs']
1050+
expected_prefix: str = expected_result['expected_prefix']
1051+
expected_key_field: str = expected_result['expected_key_field']
1052+
mocker.patch.object(Client, '_http_request', return_value=expected_result['mock_response'])
1053+
1054+
command_result = gitlab_cancel_pipeline_command(client, args)
1055+
1056+
assert command_result.outputs_prefix == expected_prefix
1057+
assert command_result.outputs_key_field == expected_key_field
1058+
assert command_result.outputs == expected_outputs
1059+
assert command_result.outputs.get('project_id') == int(args.get('project_id'))
1060+
assert command_result.outputs.get('status') == 'canceled'

Packs/GitLab/Integrations/GitLabv2/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,3 +2201,41 @@ Triggers a GitLab pipeline on a selected project and branch.
22012201
| GitLab.Pipeline.user.state | String | State of the user who triggered the pipeline. |
22022202
| GitLab.Pipeline.user.avatar_url | String | Avatar URL of the user who trigerred the pipeline. |
22032203
| GitLab.Pipeline.user.web_url | String | Web URL of the user who triggered the pipeline. |
2204+
2205+
### gitlab-cancel-pipeline
2206+
2207+
***
2208+
Cancels a GitLab pipeline.
2209+
2210+
#### Base Command
2211+
2212+
`gitlab-cancel-pipeline`
2213+
2214+
#### Input
2215+
2216+
| **Argument Name** | **Description** | **Required** |
2217+
| --- | --- | --- |
2218+
| project_id | Project ID on which to run the pipeline. | Optional |
2219+
| pipeline_id | The pipline ID to cancel. | Optional |
2220+
2221+
#### Context Output
2222+
2223+
| **Path** | **Type** | **Description** |
2224+
| --- | --- | --- |
2225+
| GitLab.Pipeline.id | Number | Pipeline ID. |
2226+
| GitLab.Pipeline.project_id | Number | Project ID that the pipeline belongs to. |
2227+
| GitLab.Pipeline.status | String | Status of the pipeline. |
2228+
| GitLab.Pipeline.ref | String | Reference of the pipeline. |
2229+
| GitLab.Pipeline.sha | String | SHA of the pipeline. |
2230+
| GitLab.Pipeline.created_at | Date | Time when the pipeline was created. |
2231+
| GitLab.Pipeline.updated_at | Date | Time when the pipeline was last updated. |
2232+
| GitLab.Pipeline.started_at | Date | Time when the pipeline was started. |
2233+
| GitLab.Pipeline.finished_at | Date | Time when the pipeline was finished. |
2234+
| GitLab.Pipeline.duration | Number | Duration of the pipeline in seconds. |
2235+
| GitLab.Pipeline.web_url | String | Web URL of the pipeline. |
2236+
| GitLab.Pipeline.user.name | String | Name of the user who triggered the pipeline. |
2237+
| GitLab.Pipeline.user.username | String | Username that triggered the pipeline. |
2238+
| GitLab.Pipeline.user.id | Number | ID of the user who triggered the pipeline. |
2239+
| GitLab.Pipeline.user.state | String | State of the user who triggered the pipeline. |
2240+
| GitLab.Pipeline.user.avatar_url | String | Avatar URL of the user who trigerred the pipeline. |
2241+
| GitLab.Pipeline.user.web_url | String | Web URL of the user who triggered the pipeline. |

Packs/GitLab/Integrations/GitLabv2/test_data/commands_test_data.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,5 +1243,31 @@
12431243
"expected_prefix": "GitLab.Pipeline",
12441244
"expected_url_mock_suffix": "projects/2222/pipelines",
12451245
"expected_key_field": "id"
1246+
},
1247+
"cancel_pipeline": {
1248+
"mock_response": {
1249+
"id": 1,
1250+
"project_id": 2222,
1251+
"sha": "skd5h31245ljkasl4kj45l324",
1252+
"ref": "master",
1253+
"status": "canceled",
1254+
"source": "trigger",
1255+
"created_at": "2021-06-15T14:31:29.607Z",
1256+
"updated_at": "2021-06-15T14:31:32.964Z",
1257+
"web_url": "https://server_url/project/-/pipelines/1"
1258+
},
1259+
"expected_outputs": {
1260+
"id": 1,
1261+
"project_id": 2222,
1262+
"sha": "skd5h31245ljkasl4kj45l324",
1263+
"ref": "master",
1264+
"status": "canceled",
1265+
"created_at": "2021-06-15T14:31:29.607Z",
1266+
"updated_at": "2021-06-15T14:31:32.964Z",
1267+
"web_url": "https://server_url/project/-/pipelines/1"
1268+
},
1269+
"expected_prefix": "GitLab.Pipeline",
1270+
"expected_url_mock_suffix": "projects/2222/pipelines",
1271+
"expected_key_field": "id"
12461272
}
12471273
}

Packs/GitLab/ReleaseNotes/2_2_18.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
#### Integrations
3+
##### GitLab v2
4+
- Updated the Docker image to: *demisto/python3:3.10.13.74666*.
5+
- Added a new command ***gitlab-cancel-pipeline*** that enables canceling a GitLab pipeline.

Packs/GitLab/pack_metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "GitLab",
33
"description": "Pack for handling gitlab operations",
44
"support": "xsoar",
5-
"currentVersion": "2.2.17",
5+
"currentVersion": "2.2.18",
66
"author": "Cortex XSOAR",
77
"url": "https://www.paloaltonetworks.com/cortex",
88
"email": "",

0 commit comments

Comments
 (0)