Skip to content

Commit

Permalink
Automatically copy new github issues to gitlab using CI/CD
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon committed Jan 30, 2024
1 parent 3473090 commit 322dc07
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ test:
- master
- merge_requests
- tags
except:
- schedules

static_analysis:
stage: test
Expand All @@ -46,6 +48,8 @@ static_analysis:
- master
- merge_requests
- tags
except:
- schedules

shell_check:
stage: test
Expand All @@ -55,6 +59,8 @@ shell_check:
- master
- merge_requests
- tags
except:
- schedules

layer_properties:
stage: test
Expand All @@ -64,6 +70,8 @@ layer_properties:
- master
- merge_requests
- tags
except:
- schedules

verify_documentation:
stage: test
Expand All @@ -81,6 +89,8 @@ verify_documentation:
when: always
only:
- merge_requests
except:
- schedules

check_package_building:
stage: test
Expand All @@ -91,6 +101,8 @@ check_package_building:
- python3 -m twine check dist/*
only:
- merge_requests
except:
- schedules

check_confidential_strings:
stage: test
Expand All @@ -107,6 +119,8 @@ check_confidential_strings:
- merge_requests
variables:
- $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
except:
- schedules

check_copyright_headers:
stage: test
Expand All @@ -118,6 +132,8 @@ check_copyright_headers:
- merge_requests
variables:
- $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
except:
- schedules

check_license:
stage: test
Expand All @@ -128,6 +144,8 @@ check_license:
- merge_requests
variables:
- $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
except:
- schedules

pages:
stage: deploy
Expand All @@ -142,6 +160,8 @@ pages:
- public
only:
- master
except:
- schedules

.kaniko:
image:
Expand Down Expand Up @@ -172,6 +192,8 @@ deploy_image:
only:
- master
- tags
except:
- schedules

test_image:
extends: .kaniko
Expand All @@ -192,3 +214,18 @@ test_image:
- merge_requests
variables:
- $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
except:
- schedules

copy_github_issues:
stage: test
before_script:
- pip install requests
script:
- python ci/copy_github_issues.py $ACCESS_TOKEN ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/issues $LABELS
only:
- schedules
except:
- master
- tags
- merge_requests
97 changes: 97 additions & 0 deletions ci/copy_github_issues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# This code is part of KQCircuits
# Copyright (C) 2024 IQM Finland Oy
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program. If not, see
# https://www.gnu.org/licenses/gpl-3.0.html.
#
# The software distribution should follow IQM trademark policy for open-source software
# (meetiqm.com/developers/osstmpolicy). IQM welcomes contributions to the code. Please see our contribution agreements
# for individuals (meetiqm.com/developers/clas/individual) and organizations (meetiqm.com/developers/clas/organization).


"""
This file gets two files as input stream.
The first is a json with existing github issues,
the second is a json with existing gitlab issues. Preferably only the ones with the 'github' label.
It checks for every github issue if there is a corresponding gitlab issue.
If not it outputs the attributes to create a new gitlab issue.
"""

import sys
from http import HTTPStatus, client
import requests

if len(sys.argv) <= 1:
raise ValueError("Gitlab token not provided (first argument in python call)")
if len(sys.argv) == 2:
raise ValueError("Gitlab url to REST endpoint not provided (second argument in python call")

gitlab_token = sys.argv[1]
gitlab_url = sys.argv[2]
labels = len(sys.argv) > 3 and "github," + sys.argv[3] or "github"

github_url = "https://api.github.com/repos/iqm-finland/KQCircuits/issues"

# Get the issues from github
github_request = requests.get(github_url)
if github_request.status_code != HTTPStatus.OK:
raise requests.exceptions.HTTPError(
f"HTTP Error {github_request.status_code}: {client.responses[github_request.status_code]}")

# Get the open issues from gitlab
gitlab_header = {"PRIVATE-TOKEN": gitlab_token}
gitlab_request = requests.get(gitlab_url, headers=gitlab_header, params={"labels": "github"})
if gitlab_request.status_code != HTTPStatus.OK:
raise requests.exceptions.HTTPError(
f"HTTP Error {gitlab_request.status_code}: {client.responses[gitlab_request.status_code]}")

github_issues = github_request.json()
gitlab_issues = gitlab_request.json()

new_issue_found = False
for gh_issue in github_issues:
issue_number = gh_issue['number']
url = gh_issue['html_url']
for gl_issue in gitlab_issues:
if url in gl_issue['description']:
break
else: # URL not found in existing gitlab issues, so corresponding issue does not exist. Make it
new_issue_found = True
title = gh_issue['title']
user = gh_issue['user']
header = (f"This issue was automatically copied from GitHub by the CI/CD. \n"
f"Original {url} \n"
f"Original GitHub reporter: [{user['login']}]({user['html_url']}) \n"
f"Do not remove or edit this header or the `GitHub` label to avoid duplicating the issue. \n"
f"Original (unedited) description below. Feel free to edit anything below the following line.\n\n"
f"---\n\n")

description = f"{header}{gh_issue['body']}"

post_attributes = {"title": title,
"description": description,
"labels": labels}

print("New issue found, posting to gitlab")
print("Title: " + title)
gitlab_post = requests.post(gitlab_url, headers=gitlab_header, data=post_attributes)
if gitlab_post.status_code == HTTPStatus.CREATED:
print("Issue successfully created")
print(f"Issue can be found at {gitlab_post.json()['web_url']}")
print("---")
else:
print("Error creating the issue")
raise requests.exceptions.HTTPError(
f"HTTP Error {gitlab_post.status_code}: {client.responses[gitlab_post.status_code]}")

if not new_issue_found:
print("No new issues found")

0 comments on commit 322dc07

Please sign in to comment.