Commit e3c63ff2 authored by Guillaume Pasero's avatar Guillaume Pasero
Browse files

CI: detect WIP merge requests

No related merge requests found
Showing with 53 additions and 13 deletions
+53 -13
......@@ -22,24 +22,64 @@ import os
import urllib.request
import urllib.parse
import json
import re
import time
"""
Send a request to Gitlab and return the answer
The request parameter is added after `project/:id/`
"""
def GitlabRequest(request, project=53,data={}, token=''):
gitlab_url = "https://gitlab.orfeo-toolbox.org/api/v4/projects/"
gitlab_url+= str(project) + '/' + request
params = ''
if data:
params = urllib.parse.urlencode(data)
gitlab_request = urllib.request.Request(gitlab_url)
if token:
gitlab_request.add_header('PRIVATE-TOKEN' , token )
res = urllib.request.urlopen(gitlab_request, data=params.encode('ascii'))
return json.loads(res.read().decode())
"""
Check needed environment parameters
"""
def CheckEnvParameters(params):
for p in params:
if not p in os.environ.keys():
print("Missing environment variable '"+p+"'")
return False
return True
"""
Check for any duplicated twin pipeline and cancel it
"""
if __name__ == "__main__":
if not CheckEnvParameters(['CI_COMMIT_SHA']):
sys.exit(1)
env = os.environ
sha1 = env['CI_COMMIT_SHA']
# are we in a merge_request pipeline ?
if 'CI_MERGE_REQUEST_IID' in env.keys():
gitlab_url = "https://gitlab.orfeo-toolbox.org/api/v4/projects/"
gitlab_url+= env['CI_PROJECT_ID'] + '/pipelines?sha='+sha1
gitlab_request = urllib.request.Request(gitlab_url)
gitlab_request.add_header('PRIVATE-TOKEN' , env['K8S_SECRET_TWIN_PIPELINE'] )
res = urllib.request.urlopen(gitlab_request).read().decode()
jres = json.loads(res)
for item in jres:
if item["id"] < int(env['CI_PIPELINE_ID']) and item["status"] == "running":
gitlab_url = "https://gitlab.orfeo-toolbox.org/api/v4/projects/"
gitlab_url+= env['CI_PROJECT_ID'] + '/pipelines/'+str(item["id"])+'/cancel'
gitlab_request = urllib.request.Request(gitlab_url)
gitlab_request.add_header('PRIVATE-TOKEN' , env['K8S_SECRET_TWIN_PIPELINE'] )
res2 = urllib.request.urlopen(gitlab_request).read().decode()
if not CheckEnvParameters(['K8S_SECRET_TWIN_PIPELINE','CI_PROJECT_ID','CI_PIPELINE_ID']):
sys.exit(1)
mrInfo = GitlabRequest('merge_requests/'+env['CI_MERGE_REQUEST_IID'],token=env['K8S_SECRET_TWIN_PIPELINE'])
wip_regex = re.compile("^[Ww][Ii][Pp]:")
# is it a "WIP" merge request ?
if wip_regex.search(mrInfo["title"]):
# Yes: cancel the current pipeline
print("Cancel current pipeline "+env['CI_PIPELINE_ID'])
GitlabRequest('pipelines/'+env['CI_PIPELINE_ID']+'/cancel', \
project=env['CI_PROJECT_ID'], token=env['K8S_SECRET_TWIN_PIPELINE'])
time.sleep(180)
print("Error: this pipeline should have been canceled")
sys.exit(1)
else:
# No: cancel any previous "normal" pipeline on the same SHA1
jres = GitlabRequest('pipelines', project=env['CI_PROJECT_ID'], data={'sha':sha1}, token=env['K8S_SECRET_TWIN_PIPELINE'])
for item in jres:
if item["id"] < int(env['CI_PIPELINE_ID']) and item["status"] == "running":
print("Cancel pipeline "+str(item["id"]))
jres2 = GitlabRequest('pipelines/'+str(item["id"])+'/cancel', \
project=env['CI_PROJECT_ID'], token=env['K8S_SECRET_TWIN_PIPELINE'])
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment