2018-03-14 16:37:46 +07:00
|
|
|
import collections
|
2018-06-21 14:55:14 +07:00
|
|
|
import itertools
|
2018-09-28 18:05:41 +07:00
|
|
|
import json as json_
|
2018-03-14 16:37:46 +07:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2018-10-09 20:01:45 +07:00
|
|
|
from odoo.tools import topological_sort
|
2019-02-28 20:45:31 +07:00
|
|
|
from . import exceptions, utils
|
2018-03-14 16:37:46 +07:00
|
|
|
|
[FIX] runbot_merge: handle the bot user not being able to comment
If the author of a PR has blocked the bot user, commenting on the PR
will fail. While comment failure is technically handled in the feedback
cron, the cron will simply retry commenting on every run filling the
log with useless unactionable garbage.
Retrying is the right thing to do in the normal case (e.g. changing tags
often has transient failures), but if we figure out we're blocked we
might as well just log a warning and drop the comment on the floor, it's
unlikely the situation will resolve itself.
Couldn't test it, because the block API is a developer preview and I
just can't get it to work anyway (404 on /user/blocks even providing the
suggested media type).
And the way the block is inferred is iffy (based on an error message),
the error body doesn't seem to provide any clean / clear cut error code:
{
"message": "Validation Failed",
"errors": [
{
"resource": "IssueComment",
"code": "unprocessable",
"field": "data",
"message": "User is blocked"
}
],
"documentation_url": "https://developer.github.com/v3/issues/comments/#create-a-comment"
}
No useful headers either.
Fixes #127
2019-05-07 15:36:53 +07:00
|
|
|
def _is_json(r):
|
|
|
|
return r and r.headers.get('content-type', '').startswith(('application/json', 'application/javascript'))
|
|
|
|
|
2018-03-14 16:37:46 +07:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
class GH(object):
|
|
|
|
def __init__(self, token, repo):
|
|
|
|
self._url = 'https://api.github.com'
|
|
|
|
self._repo = repo
|
|
|
|
session = self._session = requests.Session()
|
2018-03-26 18:08:49 +07:00
|
|
|
session.headers['Authorization'] = 'token {}'.format(token)
|
2019-08-21 16:17:04 +07:00
|
|
|
session.headers['Accept'] = 'application/vnd.github.symmetra-preview+json'
|
2018-03-14 16:37:46 +07:00
|
|
|
|
2018-08-28 20:42:28 +07:00
|
|
|
def __call__(self, method, path, params=None, json=None, check=True):
|
2018-03-14 16:37:46 +07:00
|
|
|
"""
|
|
|
|
:type check: bool | dict[int:Exception]
|
|
|
|
"""
|
|
|
|
r = self._session.request(
|
|
|
|
method,
|
2018-03-26 18:08:49 +07:00
|
|
|
'{}/repos/{}/{}'.format(self._url, self._repo, path),
|
2018-08-28 20:42:28 +07:00
|
|
|
params=params,
|
2018-03-14 16:37:46 +07:00
|
|
|
json=json
|
|
|
|
)
|
|
|
|
if check:
|
|
|
|
if isinstance(check, collections.Mapping):
|
|
|
|
exc = check.get(r.status_code)
|
|
|
|
if exc:
|
2018-10-17 16:31:52 +07:00
|
|
|
raise exc(r.text)
|
2019-02-28 20:45:31 +07:00
|
|
|
if r.status_code >= 400:
|
|
|
|
headers = '\n'.join('\t%s: %s' % (h, v) for h, v in r.headers.items())
|
[FIX] runbot_merge: handle the bot user not being able to comment
If the author of a PR has blocked the bot user, commenting on the PR
will fail. While comment failure is technically handled in the feedback
cron, the cron will simply retry commenting on every run filling the
log with useless unactionable garbage.
Retrying is the right thing to do in the normal case (e.g. changing tags
often has transient failures), but if we figure out we're blocked we
might as well just log a warning and drop the comment on the floor, it's
unlikely the situation will resolve itself.
Couldn't test it, because the block API is a developer preview and I
just can't get it to work anyway (404 on /user/blocks even providing the
suggested media type).
And the way the block is inferred is iffy (based on an error message),
the error body doesn't seem to provide any clean / clear cut error code:
{
"message": "Validation Failed",
"errors": [
{
"resource": "IssueComment",
"code": "unprocessable",
"field": "data",
"message": "User is blocked"
}
],
"documentation_url": "https://developer.github.com/v3/issues/comments/#create-a-comment"
}
No useful headers either.
Fixes #127
2019-05-07 15:36:53 +07:00
|
|
|
if _is_json(r):
|
2019-02-28 20:45:31 +07:00
|
|
|
body = r.json()
|
|
|
|
elif r.encoding is not None:
|
|
|
|
body = utils.shorten(r.text, 200)
|
|
|
|
else:
|
|
|
|
body = utils.shorten(r.content, 200)
|
|
|
|
|
|
|
|
_logger.error("%(method)s /%(repo)s/%(path)s\n=> %(status)d %(reason)s\n%(headers)s\n\n\t%(body)r\n====================", {
|
|
|
|
'status': r.status_code,
|
|
|
|
'reason': r.reason,
|
|
|
|
'method': method,
|
|
|
|
'repo': self._repo,
|
|
|
|
'path': path,
|
|
|
|
'headers': headers,
|
|
|
|
'body': body
|
|
|
|
})
|
|
|
|
if not isinstance(body, (bytes, str)):
|
|
|
|
raise requests.HTTPError(
|
|
|
|
json_.dumps(body, indent=4),
|
|
|
|
response=r
|
|
|
|
)
|
2018-03-14 16:37:46 +07:00
|
|
|
r.raise_for_status()
|
|
|
|
return r
|
|
|
|
|
2018-11-22 00:43:05 +07:00
|
|
|
def user(self, username):
|
|
|
|
r = self._session.get("{}/users/{}".format(self._url, username))
|
|
|
|
r.raise_for_status()
|
|
|
|
return r.json()
|
|
|
|
|
2018-03-14 16:37:46 +07:00
|
|
|
def head(self, branch):
|
2018-03-26 18:08:49 +07:00
|
|
|
d = self('get', 'git/refs/heads/{}'.format(branch)).json()
|
2018-03-14 16:37:46 +07:00
|
|
|
|
2018-03-26 18:08:49 +07:00
|
|
|
assert d['ref'] == 'refs/heads/{}'.format(branch)
|
2018-03-14 16:37:46 +07:00
|
|
|
assert d['object']['type'] == 'commit'
|
2018-09-20 14:25:13 +07:00
|
|
|
_logger.debug("head(%s, %s) -> %s", self._repo, branch, d['object']['sha'])
|
2018-03-14 16:37:46 +07:00
|
|
|
return d['object']['sha']
|
|
|
|
|
|
|
|
def commit(self, sha):
|
2018-09-20 14:25:13 +07:00
|
|
|
c = self('GET', 'git/commits/{}'.format(sha)).json()
|
|
|
|
_logger.debug('commit(%s, %s) -> %s', self._repo, sha, shorten(c['message']))
|
|
|
|
return c
|
2018-03-14 16:37:46 +07:00
|
|
|
|
|
|
|
def comment(self, pr, message):
|
[FIX] runbot_merge: handle the bot user not being able to comment
If the author of a PR has blocked the bot user, commenting on the PR
will fail. While comment failure is technically handled in the feedback
cron, the cron will simply retry commenting on every run filling the
log with useless unactionable garbage.
Retrying is the right thing to do in the normal case (e.g. changing tags
often has transient failures), but if we figure out we're blocked we
might as well just log a warning and drop the comment on the floor, it's
unlikely the situation will resolve itself.
Couldn't test it, because the block API is a developer preview and I
just can't get it to work anyway (404 on /user/blocks even providing the
suggested media type).
And the way the block is inferred is iffy (based on an error message),
the error body doesn't seem to provide any clean / clear cut error code:
{
"message": "Validation Failed",
"errors": [
{
"resource": "IssueComment",
"code": "unprocessable",
"field": "data",
"message": "User is blocked"
}
],
"documentation_url": "https://developer.github.com/v3/issues/comments/#create-a-comment"
}
No useful headers either.
Fixes #127
2019-05-07 15:36:53 +07:00
|
|
|
# if the mergebot user has been blocked by the PR author, this will
|
|
|
|
# fail, but we don't want the closing of the PR to fail, or for the
|
|
|
|
# feedback cron to get stuck
|
|
|
|
try:
|
|
|
|
self('POST', 'issues/{}/comments'.format(pr), json={'body': message})
|
|
|
|
except requests.HTTPError as r:
|
|
|
|
if _is_json(r.response):
|
|
|
|
body = r.response.json()
|
|
|
|
if any(e.message == 'User is blocked' for e in (body.get('errors') or [])):
|
2019-10-10 16:36:14 +07:00
|
|
|
_logger.warning("comment(%s:%s) failed: user likely blocked", self._repo, pr)
|
[FIX] runbot_merge: handle the bot user not being able to comment
If the author of a PR has blocked the bot user, commenting on the PR
will fail. While comment failure is technically handled in the feedback
cron, the cron will simply retry commenting on every run filling the
log with useless unactionable garbage.
Retrying is the right thing to do in the normal case (e.g. changing tags
often has transient failures), but if we figure out we're blocked we
might as well just log a warning and drop the comment on the floor, it's
unlikely the situation will resolve itself.
Couldn't test it, because the block API is a developer preview and I
just can't get it to work anyway (404 on /user/blocks even providing the
suggested media type).
And the way the block is inferred is iffy (based on an error message),
the error body doesn't seem to provide any clean / clear cut error code:
{
"message": "Validation Failed",
"errors": [
{
"resource": "IssueComment",
"code": "unprocessable",
"field": "data",
"message": "User is blocked"
}
],
"documentation_url": "https://developer.github.com/v3/issues/comments/#create-a-comment"
}
No useful headers either.
Fixes #127
2019-05-07 15:36:53 +07:00
|
|
|
return
|
|
|
|
raise
|
2018-09-20 14:25:13 +07:00
|
|
|
_logger.debug('comment(%s, %s, %s)', self._repo, pr, shorten(message))
|
2018-03-14 16:37:46 +07:00
|
|
|
|
|
|
|
def close(self, pr, message):
|
|
|
|
self.comment(pr, message)
|
2018-03-26 18:08:49 +07:00
|
|
|
self('PATCH', 'pulls/{}'.format(pr), json={'state': 'closed'})
|
2018-03-14 16:37:46 +07:00
|
|
|
|
2019-08-21 16:17:04 +07:00
|
|
|
def change_tags(self, pr, to_):
|
|
|
|
labels_endpoint = 'issues/{}/labels'.format(pr)
|
|
|
|
from .models.pull_requests import _TAGS
|
|
|
|
mergebot_tags = set.union(*_TAGS.values())
|
|
|
|
tags_before = {label['name'] for label in self('GET', labels_endpoint).json()}
|
|
|
|
# remove all mergebot tags from the PR, then add just the ones which should be set
|
|
|
|
tags_after = (tags_before - mergebot_tags) | to_
|
|
|
|
# replace labels entirely
|
|
|
|
self('PUT', labels_endpoint, json={'labels': list(tags_after)})
|
|
|
|
|
|
|
|
_logger.debug('change_tags(%s, %s, from=%s, to=%s)', self._repo, pr, tags_before, tags_after)
|
2018-09-20 14:25:13 +07:00
|
|
|
|
2019-11-07 14:14:45 +07:00
|
|
|
def _check_updated(self, branch, to):
|
|
|
|
"""
|
|
|
|
:return: nothing if successful, the incorrect HEAD otherwise
|
|
|
|
"""
|
|
|
|
head = self.head(branch)
|
|
|
|
if head == to:
|
|
|
|
_logger.info("Sanity check ref update of %s to %s: ok", branch, to)
|
|
|
|
return
|
|
|
|
|
|
|
|
_logger.warning("Sanity check ref update of %s, expected %s got %s", branch, to, head)
|
|
|
|
return head
|
|
|
|
|
2018-03-14 16:37:46 +07:00
|
|
|
def fast_forward(self, branch, sha):
|
|
|
|
try:
|
2018-03-26 18:08:49 +07:00
|
|
|
self('patch', 'git/refs/heads/{}'.format(branch), json={'sha': sha})
|
2018-09-20 14:25:13 +07:00
|
|
|
_logger.debug('fast_forward(%s, %s, %s) -> OK', self._repo, branch, sha)
|
2019-11-07 14:14:45 +07:00
|
|
|
@utils.backoff(exc=exceptions.FastForwardError)
|
|
|
|
def _wait_for_update():
|
|
|
|
if not self._check_updated(branch, sha):
|
|
|
|
return
|
[IMP] runbot_merge: sanity check PATCH git/ref/heads
Turns out not only can that operation fail, that operation can succeed
but have its effect delayed. To try and guard against that,
immediately check that we get the correct ref' after having reset it.
This is the cause of the November 6 mess: when preparing a staging,
the mergebot does the following,
1. get the head of <branch>
2. hard-reset tmp.<branch> to that
3. start merging PRs, which requires getting the current state of
tmp.<branch> back
On the 6ths, these steps looked like this
```text
2019-11-06 10:03:21,588 head(odoo/odoo, master) -> ab6d0c38512e4944458b0b6f80f38d6c26b6b597
2019-11-06 10:03:22,375 set_ref(update, odoo/odoo, tmp.master, ab6d0c38512e4944458b0b6f80f38d6c26b6b597 -> 200 (OK)
2019-11-06 10:03:28,674 head(odoo/odoo, tmp.master) -> de2a852e7cc1f390e50190cfc497bc253687fba8
2019-11-06 10:03:30,292 head(odoo/odoo, tmp.master) -> de2a852e7cc1f390e50190cfc497bc253687fba8
```
So the 'bot fetched the commit at the head of master (ab6d0c), reset
tmp.master to that... and then got a different commit when it fetched
the tmp head to stage a PR on it.
That different head being of course a previous rejected staging. When
the new staging succeeded, it brought the entire thing in and made a
mess.
This was compounded by an issue I still have to investigate: the
staging of the new PR took the wrong base commit *but the right base
tree*, as a result the first thing it did was *reverse the entire
previous commit* (without that we could probably have left it as-is
rather than need to force-push master -- twice).
2019-11-07 13:39:20 +07:00
|
|
|
raise exceptions.FastForwardError(self._repo)
|
2018-03-14 16:37:46 +07:00
|
|
|
except requests.HTTPError:
|
2018-09-20 14:25:13 +07:00
|
|
|
_logger.debug('fast_forward(%s, %s, %s) -> ERROR', self._repo, branch, sha, exc_info=True)
|
2019-08-26 18:41:11 +07:00
|
|
|
raise exceptions.FastForwardError(self._repo)
|
2018-03-14 16:37:46 +07:00
|
|
|
|
|
|
|
def set_ref(self, branch, sha):
|
|
|
|
# force-update ref
|
2018-03-26 18:08:49 +07:00
|
|
|
r = self('patch', 'git/refs/heads/{}'.format(branch), json={
|
2018-03-14 16:37:46 +07:00
|
|
|
'sha': sha,
|
|
|
|
'force': True,
|
|
|
|
}, check=False)
|
2018-10-17 16:31:52 +07:00
|
|
|
|
|
|
|
status0 = r.status_code
|
|
|
|
_logger.debug(
|
|
|
|
'set_ref(update, %s, %s, %s -> %s (%s)',
|
|
|
|
self._repo, branch, sha, status0,
|
|
|
|
'OK' if status0 == 200 else r.text or r.reason
|
|
|
|
)
|
|
|
|
if status0 == 200:
|
2019-11-07 14:14:45 +07:00
|
|
|
@utils.backoff(exc=AssertionError)
|
|
|
|
def _wait_for_update():
|
|
|
|
head = self._check_updated(branch, sha)
|
|
|
|
assert not head, "Sanity check ref update of %s, expected %s got %s" % (
|
|
|
|
branch, sha, head
|
|
|
|
)
|
2018-03-14 16:37:46 +07:00
|
|
|
return
|
|
|
|
|
2018-06-07 19:44:44 +07:00
|
|
|
# 422 makes no sense but that's what github returns, leaving 404 just
|
|
|
|
# in case
|
2018-10-17 16:31:52 +07:00
|
|
|
status1 = None
|
|
|
|
if status0 in (404, 422):
|
2018-03-14 16:37:46 +07:00
|
|
|
# fallback: create ref
|
|
|
|
r = self('post', 'git/refs', json={
|
2018-03-26 18:08:49 +07:00
|
|
|
'ref': 'refs/heads/{}'.format(branch),
|
2018-03-14 16:37:46 +07:00
|
|
|
'sha': sha,
|
|
|
|
}, check=False)
|
2018-10-17 16:31:52 +07:00
|
|
|
status1 = r.status_code
|
|
|
|
_logger.debug(
|
|
|
|
'set_ref(create, %s, %s, %s) -> %s (%s)',
|
|
|
|
self._repo, branch, sha, status1,
|
|
|
|
'OK' if status1 == 201 else r.text or r.reason
|
|
|
|
)
|
|
|
|
if status1 == 201:
|
2019-11-07 14:14:45 +07:00
|
|
|
@utils.backoff(exc=AssertionError)
|
|
|
|
def _wait_for_update():
|
|
|
|
head = self._check_updated(branch, sha)
|
|
|
|
assert not head, "Sanity check ref update of %s, expected %s got %s" % (
|
|
|
|
branch, sha, head
|
|
|
|
)
|
2018-03-14 16:37:46 +07:00
|
|
|
return
|
2018-10-17 16:31:52 +07:00
|
|
|
|
|
|
|
raise AssertionError("set_ref failed(%s, %s)" % (status0, status1))
|
2018-03-14 16:37:46 +07:00
|
|
|
|
2018-08-28 20:42:28 +07:00
|
|
|
def merge(self, sha, dest, message):
|
|
|
|
r = self('post', 'merges', json={
|
|
|
|
'base': dest,
|
|
|
|
'head': sha,
|
|
|
|
'commit_message': message,
|
|
|
|
}, check={409: exceptions.MergeError})
|
2018-10-08 21:30:51 +07:00
|
|
|
try:
|
|
|
|
r = r.json()
|
|
|
|
except Exception:
|
2018-10-17 16:31:52 +07:00
|
|
|
raise exceptions.MergeError("Got non-JSON reponse from github: %s %s (%s)" % (r.status_code, r.reason, r.text))
|
2019-11-07 14:14:45 +07:00
|
|
|
_logger.debug(
|
|
|
|
"merge(%s, %s (%s), %s) -> %s",
|
|
|
|
self._repo, dest, r['parents'][0]['sha'],
|
|
|
|
shorten(message), r['sha']
|
|
|
|
)
|
2018-08-28 20:42:28 +07:00
|
|
|
return dict(r['commit'], sha=r['sha'])
|
|
|
|
|
|
|
|
def rebase(self, pr, dest, reset=False, commits=None):
|
|
|
|
""" Rebase pr's commits on top of dest, updates dest unless ``reset``
|
|
|
|
is set.
|
|
|
|
|
2019-07-31 14:19:39 +07:00
|
|
|
Returns the hash of the rebased head and a map of all PR commits (to the PR they were rebased to)
|
2018-08-28 20:42:28 +07:00
|
|
|
"""
|
2019-01-25 21:45:38 +07:00
|
|
|
logger = _logger.getChild('rebase')
|
2018-08-28 20:42:28 +07:00
|
|
|
original_head = self.head(dest)
|
|
|
|
if commits is None:
|
|
|
|
commits = self.commits(pr)
|
|
|
|
|
2019-01-25 21:45:38 +07:00
|
|
|
logger.debug("rebasing %s, %s on %s (reset=%s, commits=%s)",
|
|
|
|
self._repo, pr, dest, reset, len(commits))
|
|
|
|
|
2018-08-28 20:42:28 +07:00
|
|
|
assert commits, "can't rebase a PR with no commits"
|
|
|
|
for c in commits:
|
|
|
|
assert len(c['parents']) == 1, "can't rebase commits with more than one parent"
|
|
|
|
tmp_msg = 'temp rebasing PR %s (%s)' % (pr, c['sha'])
|
|
|
|
c['new_tree'] = self.merge(c['sha'], dest, tmp_msg)['tree']['sha']
|
|
|
|
|
|
|
|
prev = original_head
|
2019-07-31 14:19:39 +07:00
|
|
|
mapping = {}
|
2018-08-28 20:42:28 +07:00
|
|
|
for c in commits:
|
|
|
|
copy = self('post', 'git/commits', json={
|
|
|
|
'message': c['commit']['message'],
|
|
|
|
'tree': c['new_tree'],
|
|
|
|
'parents': [prev],
|
|
|
|
'author': c['commit']['author'],
|
|
|
|
'committer': c['commit']['committer'],
|
|
|
|
}, check={409: exceptions.MergeError}).json()
|
2019-01-25 21:45:38 +07:00
|
|
|
logger.debug('copied %s to %s (parent: %s)', c['sha'], copy['sha'], prev)
|
2019-07-31 14:19:39 +07:00
|
|
|
prev = mapping[c['sha']] = copy['sha']
|
2018-08-28 20:42:28 +07:00
|
|
|
|
|
|
|
if reset:
|
|
|
|
self.set_ref(dest, original_head)
|
2018-09-20 15:08:08 +07:00
|
|
|
else:
|
|
|
|
self.set_ref(dest, prev)
|
2018-08-28 20:42:28 +07:00
|
|
|
|
2019-01-25 21:45:38 +07:00
|
|
|
logger.debug('rebased %s, %s on %s (reset=%s, commits=%s) -> %s',
|
|
|
|
self._repo, pr, dest, reset, len(commits),
|
2018-09-20 14:25:13 +07:00
|
|
|
prev)
|
2018-08-28 20:42:28 +07:00
|
|
|
# prev is updated after each copy so it's the rebased PR head
|
2019-07-31 14:19:39 +07:00
|
|
|
return prev, mapping
|
2018-03-14 16:37:46 +07:00
|
|
|
|
2018-06-21 14:55:14 +07:00
|
|
|
# fetch various bits of issues / prs to load them
|
|
|
|
def pr(self, number):
|
|
|
|
return (
|
|
|
|
self('get', 'issues/{}'.format(number)).json(),
|
|
|
|
self('get', 'pulls/{}'.format(number)).json()
|
|
|
|
)
|
|
|
|
|
|
|
|
def comments(self, number):
|
|
|
|
for page in itertools.count(1):
|
2018-08-28 20:42:28 +07:00
|
|
|
r = self('get', 'issues/{}/comments'.format(number), params={'page': page})
|
2018-06-21 14:55:14 +07:00
|
|
|
yield from r.json()
|
|
|
|
if not r.links.get('next'):
|
|
|
|
return
|
|
|
|
|
|
|
|
def reviews(self, number):
|
|
|
|
for page in itertools.count(1):
|
2018-08-28 20:42:28 +07:00
|
|
|
r = self('get', 'pulls/{}/reviews'.format(number), params={'page': page})
|
2018-06-21 14:55:14 +07:00
|
|
|
yield from r.json()
|
|
|
|
if not r.links.get('next'):
|
|
|
|
return
|
|
|
|
|
2018-09-20 20:02:18 +07:00
|
|
|
def commits_lazy(self, pr):
|
|
|
|
for page in itertools.count(1):
|
|
|
|
r = self('get', 'pulls/{}/commits'.format(pr), params={'page': page})
|
|
|
|
yield from r.json()
|
|
|
|
if not r.links.get('next'):
|
|
|
|
return
|
|
|
|
|
2018-08-28 20:42:28 +07:00
|
|
|
def commits(self, pr):
|
|
|
|
""" Returns a PR's commits oldest first (that's what GH does &
|
|
|
|
is what we want)
|
|
|
|
"""
|
2018-10-09 20:01:45 +07:00
|
|
|
commits = list(self.commits_lazy(pr))
|
|
|
|
# map shas to the position the commit *should* have
|
|
|
|
idx = {
|
|
|
|
c: i
|
|
|
|
for i, c in enumerate(topological_sort({
|
|
|
|
c['sha']: [p['sha'] for p in c['parents']]
|
|
|
|
for c in commits
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
return sorted(commits, key=lambda c: idx[c['sha']])
|
2018-08-28 20:42:28 +07:00
|
|
|
|
2018-06-21 14:55:14 +07:00
|
|
|
def statuses(self, h):
|
|
|
|
r = self('get', 'commits/{}/status'.format(h)).json()
|
|
|
|
return [{
|
|
|
|
'sha': r['sha'],
|
2018-09-17 16:04:31 +07:00
|
|
|
**s,
|
2018-06-21 14:55:14 +07:00
|
|
|
} for s in r['statuses']]
|
2018-08-28 20:42:28 +07:00
|
|
|
|
2018-09-20 14:25:13 +07:00
|
|
|
def shorten(s):
|
|
|
|
if not s:
|
|
|
|
return s
|
|
|
|
|
|
|
|
line1 = s.split('\n', 1)[0]
|
|
|
|
if len(line1) < 50:
|
|
|
|
return line1
|
|
|
|
|
|
|
|
return line1[:47] + '...'
|