GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( 02e3c9...94bc53 )
by Plexxi
08:58 queued 04:23
created

GetInstalled._parse_yaml_file()   A

Complexity

Conditions 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import os
17
import yaml
18
19
from git.repo import Repo
20
from git.exc import InvalidGitRepositoryError
21
22
from st2common.runners.base_action import Action
23
from st2common.content.utils import get_packs_base_paths
24
from st2common.constants.pack import MANIFEST_FILE_NAME
25
26
27
class GetInstalled(Action):
28
    """"Get information about installed pack."""
29
    def run(self, pack):
30
        """
31
        :param pack: Installed Pack Name to get info about
32
        :type pack: ``str``
33
        """
34
        packs_base_paths = get_packs_base_paths()
35
36
        pack_path = None
37
        metadata_file = None
38
        for packs_base_path in packs_base_paths:
39
            pack_path = os.path.join(packs_base_path, pack)
40
            pack_yaml_path = os.path.join(pack_path, MANIFEST_FILE_NAME)
41
42
            if os.path.isfile(pack_yaml_path):
43
                metadata_file = pack_yaml_path
44
                break
45
46
        # Pack doesn't exist, finish execution normally with empty metadata
47
        if not os.path.isdir(pack_path):
48
            return {
49
                'pack': None,
50
                'git_status': None
51
            }
52
53
        if not metadata_file:
54
            error = ('Pack "%s" doesn\'t contain pack.yaml file.' % (pack))
55
            raise Exception(error)
56
57
        try:
58
            details = self._parse_yaml_file(metadata_file)
59
        except Exception as e:
60
            error = ('Pack "%s" doesn\'t contain a valid pack.yaml file: %s' % (pack, str(e)))
61
            raise Exception(error)
62
63
        try:
64
            repo = Repo(pack_path)
65
            git_status = "Status:\n%s\n\nRemotes:\n%s" % (
66
                repo.git.status().split('\n')[0],
67
                "\n".join([remote.url for remote in repo.remotes])
68
            )
69
70
            ahead_behind = repo.git.rev_list(
71
                '--left-right', '--count', 'HEAD...origin/master'
72
            ).split()
73
            # Dear god.
74
            if ahead_behind != [u'0', u'0']:
75
                git_status += "\n\n"
76
                git_status += "%s commits ahead " if ahead_behind[0] != u'0' else ""
77
                git_status += "and " if u'0' not in ahead_behind else ""
78
                git_status += "%s commits behind " if ahead_behind[1] != u'0' else ""
79
                git_status += "origin/master."
80
        except InvalidGitRepositoryError:
81
            git_status = None
82
83
        return {
84
            'pack': details,
85
            'git_status': git_status
86
        }
87
88
    def _parse_yaml_file(self, file_path):
89
        with open(file_path) as data_file:
90
            details = yaml.load(data_file)
91
        return details
92