Skip to content

Commit

Permalink
[REF] project_git_gitlab: Black python code
Browse files Browse the repository at this point in the history
  • Loading branch information
mathben committed Dec 18, 2019
1 parent b909342 commit ec0b416
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 56 deletions.
13 changes: 3 additions & 10 deletions project_git_gitlab/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@
"license": "LGPL-3",
"author": "Odoo Community Association (OCA), Modoolar",
"website": "https://www.modoolar.com/",
"depends": [
"project_git"
],
"data": [
"views/project_git_gitlab_views.xml"
],

"depends": ["project_git"],
"data": ["views/project_git_gitlab_views.xml"],
"demo": [],
"qweb": [

],
"qweb": [],
"application": True,
}
22 changes: 13 additions & 9 deletions project_git_gitlab/controllers/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@
# License LGPLv3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.en.html).

from odoo import http
from odoo.addons.project_git.controller.controller \
import GitController, GitContext
from odoo.addons.project_git.controller.controller import (
GitController,
GitContext,
)


class GitLabContext(GitContext):
def __init__(self, token, payload):
GitContext.__init__(self, 'gitlab', token, payload)
GitContext.__init__(self, "gitlab", token, payload)

@property
def gitlab_token(self):
return self.header['token']
return self.header["token"]

@property
def has_gitlab_token(self):
return 'token' in self.header
return "token" in self.header


class GitLabController(GitController):

@http.route([
'/gitlab/payload/<string:token>'
], type='json', auth='public', website=True)
@http.route(
["/gitlab/payload/<string:token>"],
type="json",
auth="public",
website=True,
)
def process_request_gitlab(self, token, *args, **kw):
return self.process_request(
GitLabContext(token, http.request.jsonrequest)
Expand Down
62 changes: 25 additions & 37 deletions project_git_gitlab/models/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@

from dateutil.parser import parse

TYPE = [('gitlab', 'GitLab')]
TYPE = [("gitlab", "GitLab")]


class GitUser(models.Model):
_inherit = 'project.git.user'
_inherit = "project.git.user"

type = fields.Selection(
selection_add=TYPE,
)
type = fields.Selection(selection_add=TYPE,)


class GitRepository(models.Model):
_inherit = 'project.git.repository'
_inherit = "project.git.repository"

type = fields.Selection(
selection_add=TYPE,
)
type = fields.Selection(selection_add=TYPE,)

def _secret_visible_for_types(self):
types = super(GitRepository, self)._secret_visible_for_types()
Expand All @@ -32,23 +28,19 @@ def _secret_visible_for_types(self):


class GitCommit(models.Model):
_inherit = 'project.git.commit'
_inherit = "project.git.commit"

type = fields.Selection(
selection_add=TYPE,
)
type = fields.Selection(selection_add=TYPE,)


class GitBranch(models.Model):
_inherit = 'project.git.branch'
_inherit = "project.git.branch"

type = fields.Selection(
selection_add=TYPE,
)
type = fields.Selection(selection_add=TYPE,)


class GitPayloadParser(models.AbstractModel):
_inherit = 'project.git.payload.parser'
_inherit = "project.git.payload.parser"

# -------------------------------------------
# Header
Expand All @@ -58,8 +50,8 @@ def parse_gitlab_header(self, type, raw_payload):
action_type = self._map_gitlab_action_type(raw_payload)

data = {
"event": raw_payload['object_kind'],
"delivery": raw_payload['checkout_sha'],
"event": raw_payload["object_kind"],
"delivery": raw_payload["checkout_sha"],
"action_type": action_type,
"action": self._format_action_name(action_type),
}
Expand All @@ -71,19 +63,18 @@ def parse_gitlab_header(self, type, raw_payload):
return data

def _map_gitlab_action_type(self, raw_payload):

def is_delete_event(evt):
after = raw_payload['after']
return evt == 'push' and (after.isdigit() and not int(after))
after = raw_payload["after"]
return evt == "push" and (after.isdigit() and not int(after))

event = raw_payload['object_kind']
event = raw_payload["object_kind"]

if event not in self._gitlab_supported_event_types():
return False

# In case of a push we need to check if we have a delete event
if is_delete_event(event):
event = 'delete'
event = "delete"

return event

Expand All @@ -108,29 +99,27 @@ def parse_gitlab_push(self, context):
return {
"repository": self.parse_gitlab_repository(context),
"branches": [self.parse_gitlab_branch(context)],
"sender": self.parse_gitlab_sender(context)
"sender": self.parse_gitlab_sender(context),
}

# -------------------------------------------
# Action Delete
# -------------------------------------------
def parse_gitlab_delete(self, context):
return {
"branches": [self.parse_gitlab_branch(context, False)]
}
return {"branches": [self.parse_gitlab_branch(context, False)]}

# -------------------------------------------
# Paring methods
# -------------------------------------------
def parse_gitlab_branch(self, context, commits=True):
payload = context.raw_payload
branch_name = payload["ref"].split('/')[-1]
branch_name = payload["ref"].split("/")[-1]
data = {
"name": branch_name,
"type": context.type,
"repository_id": context.repository.id,
"url": urljoin(
payload['project']['homepage'] + '/', 'tree', branch_name
payload["project"]["homepage"] + "/", "tree", branch_name
),
}

Expand All @@ -157,12 +146,11 @@ def parse_gitlab_commit(self, context, commit):

def parse_gitlab_commit_author(self, context, commit):
author_data = dict(
name=commit["author"]["name"],
email=commit["author"]["email"]
name=commit["author"]["name"], email=commit["author"]["email"]
)

repository_owner = self.parse_gitlab_repository_owner(context)
if repository_owner['email'] == author_data['email']:
if repository_owner["email"] == author_data["email"]:
return repository_owner

return author_data
Expand All @@ -184,17 +172,17 @@ def parse_gitlab_repository(self, context):
return {
"name": project["name"],
"full_name": project["path_with_namespace"],
"url": project['homepage'],
"url": project["homepage"],
"type": context.type,
"owner": self.parse_gitlab_repository_owner(context)
"owner": self.parse_gitlab_repository_owner(context),
}

def parse_gitlab_repository_owner(self, context):
raw_payload = context.raw_payload

utils = re.search(
r"(https?://.+/)(\w+)/.+",
raw_payload["repository"]["git_http_url"]
raw_payload["repository"]["git_http_url"],
)

link = utils.group(1)
Expand Down

0 comments on commit ec0b416

Please sign in to comment.