|
| 1 | +import logging |
| 2 | + |
| 3 | +from sync2jira.api.gitlab_client import GitlabClient |
| 4 | +import sync2jira.downstream_issue as d_issue |
| 5 | +import sync2jira.downstream_pr as d_pr |
| 6 | + |
| 7 | +# Local Modules |
| 8 | +import sync2jira.intermediary as i |
| 9 | + |
| 10 | +log = logging.getLogger("sync2jira") |
| 11 | + |
| 12 | + |
| 13 | +def should_sync(upstream, labels, config, event_type): |
| 14 | + mapped_repos = config["sync2jira"]["map"]["gitlab"] |
| 15 | + if upstream not in mapped_repos: |
| 16 | + log.debug("%r not in Gitlab map: %r", upstream, mapped_repos.keys()) |
| 17 | + return None |
| 18 | + if event_type not in mapped_repos[upstream].get("sync", []): |
| 19 | + log.debug( |
| 20 | + "%r not in Gitlab sync map: %r", |
| 21 | + event_type, |
| 22 | + mapped_repos[upstream].get("sync", []), |
| 23 | + ) |
| 24 | + return None |
| 25 | + |
| 26 | + _filter = config["sync2jira"].get("filters", {}).get("gitlab", {}).get(upstream, {}) |
| 27 | + for key, expected in _filter.items(): |
| 28 | + if key == "labels": |
| 29 | + if labels.isdisjoint(expected): |
| 30 | + log.debug("Labels %s not found on issue: %s", expected, upstream) |
| 31 | + return None |
| 32 | + |
| 33 | + |
| 34 | +def handle_gitlab_issue(body, headers, config, suffix): |
| 35 | + """ |
| 36 | + Handle GitLab issue from FedMsg. |
| 37 | +
|
| 38 | + :param Dict body: FedMsg Message body |
| 39 | + :param Dict body: FedMsg Message headers |
| 40 | + :param Dict config: Config File |
| 41 | + :param Bool is_pr: msg refers to a pull request |
| 42 | + """ |
| 43 | + upstream = body["project"]["path_with_namespace"] |
| 44 | + url = headers["x-gitlab-instance"] |
| 45 | + token = config["sync2jira"].get("github_token") |
| 46 | + labels = {label["title"] for label in body.get("labels", [])} |
| 47 | + iid = body.get("object_attributes").get("iid") |
| 48 | + |
| 49 | + if should_sync(upstream, labels, config, "issue"): |
| 50 | + sync_gitlab_issue(GitlabClient(url, token, upstream), iid, upstream, config) |
| 51 | + |
| 52 | + |
| 53 | +def handle_gitlab_note(body, headers, config, suffix): |
| 54 | + """ |
| 55 | + Handle Gitlab note from FedMsg. |
| 56 | +
|
| 57 | + :param Dict body: FedMsg Message body |
| 58 | + :param Dict body: FedMsg Message headers |
| 59 | + :param Dict config: Config File |
| 60 | + :param String suffix: FedMsg suffix |
| 61 | + """ |
| 62 | + upstream = body["project"]["path_with_namespace"] |
| 63 | + url = headers["x-gitlab-instance"] |
| 64 | + token = config["sync2jira"].get("github_token") |
| 65 | + |
| 66 | + if "merge_request" in body: |
| 67 | + labels = { |
| 68 | + label["title"] for label in body.get("merge_request").get("labels", []) |
| 69 | + } |
| 70 | + iid = body.get("merge_request").get("iid") |
| 71 | + |
| 72 | + if should_sync(upstream, labels, config, "issue"): |
| 73 | + sync_gitlab_mr(GitlabClient(url, token, upstream), iid, upstream) |
| 74 | + if "issue" in body: |
| 75 | + labels = {label["title"] for label in body.get("issue").get("labels", [])} |
| 76 | + iid = body.get("issue").get("iid") |
| 77 | + |
| 78 | + if should_sync(upstream, labels, config, "pullrequest"): |
| 79 | + sync_gitlab_issue(GitlabClient(url, token, upstream), iid, upstream) |
| 80 | + log.info("Note was not added to an issue or merge request. Skipping note event.") |
| 81 | + |
| 82 | + |
| 83 | +def handle_gitlab_mr(body, headers, config, suffix): |
| 84 | + """ |
| 85 | + Handle Gitlab merge request from FedMsg. |
| 86 | +
|
| 87 | + :param Dict body: FedMsg Message body |
| 88 | + :param Dict body: FedMsg Message headers |
| 89 | + :param Dict config: Config File |
| 90 | + :param String suffix: FedMsg suffix |
| 91 | + """ |
| 92 | + upstream = body["project"]["path_with_namespace"] |
| 93 | + url = headers["x-gitlab-instance"] |
| 94 | + token = config["sync2jira"].get("github_token") |
| 95 | + labels = {label["title"] for label in body.get("labels", [])} |
| 96 | + iid = body.get("object_attributes").get("iid") |
| 97 | + |
| 98 | + if should_sync(upstream, labels, config, "pullrequest"): |
| 99 | + sync_gitlab_mr(GitlabClient(url, token, upstream), iid, upstream, config) |
| 100 | + |
| 101 | + |
| 102 | +def sync_gitlab_issue(client, iid, upstream, config): |
| 103 | + gitlab_issue = client.fetch_issue(iid) |
| 104 | + comments = gitlab_issue.notes.list(all=True) |
| 105 | + |
| 106 | + issue = i.Issue.from_gitlab(gitlab_issue, comments, upstream, config) |
| 107 | + d_issue.sync_with_jira(issue, config) |
| 108 | + |
| 109 | + |
| 110 | +def sync_gitlab_mr(client, iid, upstream, config): |
| 111 | + gitlab_mr = client.fetch_mr(iid) |
| 112 | + comments = gitlab_mr.notes.list(all=True) |
| 113 | + |
| 114 | + mr = i.PR.from_gitlab(gitlab_mr, comments, upstream, config) |
| 115 | + d_pr.sync_with_jira(mr, config) |
| 116 | + |
| 117 | + |
| 118 | +handlers = { |
| 119 | + "gitlab.issues": handle_gitlab_issue, |
| 120 | + "gitlab.issue_comment": handle_gitlab_mr, |
| 121 | + "gitlab.note": handle_gitlab_note, |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +def get_handler_for(suffix, topic, idx): |
| 126 | + """ |
| 127 | + Function to check if a handler for given suffix is configured |
| 128 | + :param String suffix: Incoming suffix |
| 129 | + :param String topic: Topic of incoming message |
| 130 | + :param String idx: Id of incoming message |
| 131 | + :returns: Handler function if configured for suffix. Otherwise None. |
| 132 | + """ |
| 133 | + if suffix in handlers: |
| 134 | + return handlers.get(suffix) |
| 135 | + log.info("No gitlab handler for %r %r %r", suffix, topic, idx) |
| 136 | + return None |
0 commit comments