Skip to content

DEVELOPMENT IS ON GITLAB: https://gitlab.kwant-project.org/qt/adaptive #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
basnijholt opened this issue Jun 11, 2018 · 3 comments
Closed
Labels

Comments

@basnijholt
Copy link
Member

Currently the development of adaptive happens on our GitLab instance: https://gitlab.kwant-project.org/qt/adaptive

That's where you'll find most issues.

@jbweston
Copy link
Contributor

You can use Github OAuth to sign in to the Gitlab instance

@basnijholt
Copy link
Member Author

We have moved the development to GitHub as of today!

@basnijholt
Copy link
Member Author

We used the following script:

import re
import gitlab
import github

gitlab_url = 'https://gitlab.kwant-project.org/'
repo_name_gl = 'qt/adaptive'
repo_name_gh = "python-adaptive/adaptive"

# gh = github.Github("...") # Joe
gh = github.Github("...") # Bas

gl = gitlab.Gitlab(gitlab_url, private_token=...', api_version=4)
gl.auth()

repo_gl = gl.projects.get(repo_name_gl)
mrs = repo_gl.mergerequests.list(state='opened')
issues = repo_gl.issues.list(all=True)

repo_gh = gh.get_repo(repo_name_gh)

def random_color():
    import random
    r = lambda: random.randint(0, 255)
    return '%02X%02X%02X' % (r(),r(),r())

def get_label(label):
    try:
        return repo_gh.create_label(label, color=random_color())
    except:
        return repo_gh.get_label(label)

def get_issue_body(issue, is_pr=False):
    attrs = issue.attributes
    author = attrs["author"]
    username_url = get_username_url(author["username"])
    url = attrs["web_url"]
    output = (f'## ([original {"merge request" if is_pr else "issue"} on GitLab]({url}))'
            '\n\n'
            f'_opened by {author["name"]} ({username_url}) at {attrs["created_at"]}_'
            '\n\n'
            f'{attrs["description"]}'
           )
    return parse(output)

def get_comment_body(discussion, url):
    attrs = discussion.attributes
    note = attrs['notes'][0]
    body = note['body']
    author = note['author']
    username_url = get_username_url(author["username"])
    output = (
        f'_originally posted by {author["name"]} ({username_url}) at {note["created_at"]} on [GitLab]({url})_'
        '\n\n'
        f'{body}'
       )
    return parse(output)

def human_posted(discussion):
    return not discussion.attributes['notes'][0]['system']

def get_username_url(username):
    url = gitlab_url + username
    return f'[@{username}]({url})'

def parse(text):
    repo_url = gitlab_url + repo_name_gl
    text = re.sub('!(\d{1,3})', rf'[gitlab:!\1]({repo_url}/merge_requests/\1)', text)
    text = re.sub('#(\d{1,3})', rf'[gitlab:#\1]({repo_url}/issues/\1)', text)
    return text

import time

for issue in issues:
    time.sleep(2)
    attrs = issue.attributes
    labels = [get_label(label) for label in attrs["labels"]]
    new_issue = repo_gh.create_issue(title=attrs['title'], body=get_issue_body(issue), labels=labels)
    for discussion in issue.discussions.list():
        if human_posted(discussion):
            time.sleep(0.5)
            comment_body = get_comment_body(discussion, attrs['web_url'])
            new_issue.create_comment(comment_body)
    if issue.attributes['state'] == 'closed':
        # Close the issue if closed on GitLab
        new_issue.edit(state="closed")

for mr in mrs:
    attrs = mr.attributes
    new_pr = repo_gh.create_pull(title=attrs['title'],
                                 body=get_issue_body(mr, is_pr=True),
                                 head=attrs['source_branch'],
                                 base=attrs['target_branch'],
                                 maintainer_can_modify=True)
    for discussion in mr.discussions.list():
        if human_posted(discussion):
            comment_body = get_comment_body(discussion, attrs['web_url'])
            new_pr.create_issue_comment(comment_body)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants