class Selfapprove(BotPlugin):
    """
    Self approve your github PR via a bot
    """
    def activate(self):
        """
        Triggers on plugin activation
        """
        token = self.config["GITHUB_TOKEN"]
        self.gh = GitHub(token=token)

        self.gh.is_starred(
            "github",
            "gitignore")  # test connection, will raise if connection fails

        super(Selfapprove, self).activate()

    def get_configuration_template(self):
        """
        Defines the configuration structure this plugin supports
        """
        return {"GITHUB_TOKEN": "tokenvalue"}

    @arg_botcmd(
        "pr",
        help="The URL of the Pull Request you would like to self approve",
        type=str,
    )
    @arg_botcmd(
        "--reason",
        help=
        "The reason you are self approving this PR instead of having a teammate approve",
        type=str,
        required=True,
    )
    @arg_botcmd("--test-only-changes",
                help="Are these test only changes?",
                action="store_true")
    def selfapprove(self, message, pr, reason, test_only_changes):
        if not message.to.name == "self_approve":
            return "!selfapprove command must be run from #self_approve channel"
        if not test_only_changes:
            return "Changes that touch more than just tests must be approved by another engineer."

        match = re.search(r"https://github.com/(.+)/(.+)/pull/([0-9]+)", pr)
        if not match:
            return f"PR URL {pr} did not match regex r'https://github.com/(.+)/(.+)/pull/([0-9]+)'"

        org = match.group(1)
        repo_name = match.group(2)
        if not repo_name == "apm_bundle":
            return f"selfapprove currently only works with apm_bundle"
        pr_num = match.group(3)
        repo = self.gh.repository(org, repo_name)
        pr = repo.pull_request(int(pr_num))
        pr.create_review(
            f"This PR was self-approved by {message.frm.fullname}. The stated reason for self-appoval was: {reason}",
            event="APPROVE",
        )
        return f"{pr} was succesfully self-approved by {message.frm.fullname}. Reason: {reason}"
示例#2
0
文件: helpers.py 项目: balloob/farcy
def get_session():
    """Fetch and/or load API authorization token for GITHUB."""
    ensure_config_dir()
    credential_file = os.path.join(CONFIG_DIR, 'github_auth')
    if os.path.isfile(credential_file):
        with open(credential_file) as fd:
            token = fd.readline().strip()
        gh = GitHub(token=token)
        try:  # Test connection before starting
            gh.is_starred('github', 'gitignore')
            return gh
        except GitHubError as exc:
            raise_unexpected(exc.code)
            sys.stderr.write('Invalid saved credential file.\n')

    from getpass import getpass
    from github3 import authorize

    user = prompt('GITHUB Username')
    try:
        auth = authorize(
            user, getpass('Password for {0}: '.format(user)), 'repo',
            'Farcy Code Reviewer',
            two_factor_callback=lambda: prompt('Two factor token'))
    except GitHubError as exc:
        raise_unexpected(exc.code)
        raise FarcyException(exc.message)

    with open(credential_file, 'w') as fd:
        fd.write('{0}\n{1}\n'.format(auth.token, auth.id))
    return GitHub(token=auth.token)
示例#3
0
def get_session():
    """Fetch and/or load API authorization token for GITHUB."""
    ensure_config_dir()
    credential_file = os.path.join(CONFIG_DIR, 'github_auth')
    if os.path.isfile(credential_file):
        with open(credential_file) as fd:
            token = fd.readline().strip()
        gh = GitHub(token=token)
        try:  # Test connection before starting
            gh.is_starred('github', 'gitignore')
            return gh
        except GitHubError as exc:
            raise_unexpected(exc.code)
            sys.stderr.write('Invalid saved credential file.\n')

    from getpass import getpass
    from github3 import authorize

    user = prompt('GITHUB Username')
    try:
        auth = authorize(
            user,
            getpass('Password for {0}: '.format(user)),
            'repo',
            'Farcy Code Reviewer',
            two_factor_callback=lambda: prompt('Two factor token'))
    except GitHubError as exc:
        raise_unexpected(exc.code)
        raise FarcyException(exc.message)

    with open(credential_file, 'w') as fd:
        fd.write('{0}\n{1}\n'.format(auth.token, auth.id))
    return GitHub(token=auth.token)
示例#4
0
def get_session():
    """Fetch and/or load API authorization token for GitHub."""
    if not os.path.isfile(TOKEN_FILE):
        raise HassReleaseError("Please write a GitHub token to .token")

    with open(TOKEN_FILE) as fd:
        token = fd.readline().strip()

    gh = GitHub(token=token)
    try:  # Test connection before starting
        gh.is_starred("github", "gitignore")
        return gh
    except GitHubError:
        raise HassReleaseError("Invalid token found")
示例#5
0
def get_session():
    """Fetch and/or load API authorization token for GITHUB."""
    if not os.path.isfile(TOKEN_FILE):
        sys.stderr.write('Please write a GitHub token to .token\n')
        sys.exit(1)

    with open(TOKEN_FILE) as fd:
        token = fd.readline().strip()

    gh = GitHub(token=token)
    try:  # Test connection before starting
        gh.is_starred('github', 'gitignore')
        return gh
    except GitHubError as exc:
        sys.stderr.write('Invalid token found\n')
        sys.exit(1)
示例#6
0
def get_session():
    global GITHUB_SESSION

    if GITHUB_SESSION is not None:
        return GITHUB_SESSION

    if not os.path.isfile('data/gh_token'):
        raise EsphomeReleaseError(
            'Please write a GitHub token to data/gh_token')

    with open('data/gh_token') as fd:
        token = fd.readline().strip()

    gh = GitHub(token=token)
    try:  # Test connection before starting
        gh.is_starred('github', 'gitignore')
    except GitHubError as exc:
        raise EsphomeReleaseError('Invalid token found')
    GITHUB_SESSION = gh
    return GITHUB_SESSION
示例#7
0
def get_session():
    """Fetch and/or load API authorization token for GitHub."""
    token = None
    if os.path.isfile(TOKEN_FILE):
        with open(TOKEN_FILE) as fd:
            token = fd.readline().strip()
    elif "GITHUB_TOKEN" in os.environ:
        token = os.environ["GITHUB_TOKEN"]

    if token is None:
        raise OppReleaseError(
            "Please write a GitHub token to .token or set GITHUB_TOKEN env var"
        )

    gh = GitHub(token=token)
    try:  # Test connection before starting
        gh.is_starred("github", "gitignore")
        return gh
    except GitHubError:
        raise OppReleaseError("Invalid token found")