• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chaoss / grimoirelab-sirmordred / 8664792747

12 Apr 2024 04:00PM UTC coverage: 71.08%. Remained the same
8664792747

push

github

sduenas
Release 1.0.0-rc.3

816 of 1148 relevant lines covered (71.08%)

2.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/sirmordred/github.py
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
#
4
# Copyright (C) 2015-2019 Bitergia
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#
19
# Authors:
20
#     Luis Cañas-Díaz <lcanas@bitergia.com>
21
#
22

23
import logging
3✔
24
import urllib.request
3✔
25

26
from sirmordred.error import GithubFileNotFound
3✔
27

28
logger = logging.getLogger(__name__)
3✔
29

30

31
class Github:
3✔
32

33
    def __init__(self, token):
3✔
34
        self.token = token
3✔
35

36
    def __check_looks_like_uri(self, uri):
3✔
37
        """Checks the URI looks like a RAW uri in github:
38

39
        - 'https://raw.githubusercontent.com/github/hubot/master/README.md'
40
        - 'https://github.com/github/hubot/raw/master/README.md'
41

42
        :param uri: uri of the file
43
        """
44
        if uri.split('/')[2] == 'raw.githubusercontent.com':
3✔
45
            return True
×
46
        elif uri.split('/')[2] == 'github.com':
3✔
47
            if uri.split('/')[5] == 'raw':
3✔
48
                return True
×
49
        else:
×
50
            raise GithubFileNotFound('URI %s is not a valid link to a raw file in Github' % uri)
3✔
51

52
    def read_file_from_uri(self, uri):
3✔
53
        """Reads the file from Github
54

55
        :param uri: URI of the Github raw File
56

57
        :returns: UTF-8 text with the content
58
        """
59
        logger.debug("Reading %s" % (uri))
3✔
60

61
        self.__check_looks_like_uri(uri)
3✔
62

63
        try:
3✔
64
            req = urllib.request.Request(uri)
3✔
65
            req.add_header('Authorization', 'token %s' % self.token)
3✔
66
            r = urllib.request.urlopen(req)
3✔
67
        except urllib.error.HTTPError as err:
3✔
68
            if err.code == 404:
3✔
69
                raise GithubFileNotFound('File %s is not available. Check the URL to ensure it really exists' % uri)
3✔
70
            else:
×
71
                raise
1✔
72

73
        return r.read().decode("utf-8")
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc