简体   繁体   English

Python 3.5:将GitHub凭证传递给另一个python脚本

[英]Python 3.5: Passing GitHub credentials to another python script

I have a python script that polls a GitHub repository for pull requests and an additional script that looks for commits and changed files equal to 1. 我有一个python脚本轮询GitHub存储库中的拉取请求,还有一个附加脚本查找提交和更改的文件等于1。

Everything seems to be working except I'm being prompted for GitHub credentials when the second script runs. 似乎一切正常,除了在第二个脚本运行时提示我输入GitHub凭据。

I'm trying to pass the gh variable which holds the credentials for the repository in the main script to the second script so that the credentials do not have to be entered again. 我正在尝试将gh变量传递给第二个脚本,该变量将主脚本中存储库的凭据保存到第二个脚本中,因此不必再次输入凭据。

The repo and user variables seem to be passing to the second script correctly. repouser变量似乎已正确传递给第二个脚本。

Please see code below, and thanks in advance for any guidance. 请参见下面的代码,并在此先感谢您的指导。

main_en_pr script: main_en_pr脚本:

#! /usr/bin/python
import os
import github3
from github3 import login, GitHub, authorize
from getpass import getuser, getpass
import requests
import csv
import configparser
import sys
import codecs

sys.__stdout__ = codecs.getwriter('utf8')(sys.stdout)



# authenticate to GIT
try:
    import readline
except ImportError:
    pass

try:
    user = input('GitHub username: ')
except KeyboardInterrupt:
    user = getuser()

password = getpass('GitHub token for {0}: '.format(user))

gh = login(user, password)

# read the contents of the config file to pull in the repo name
config = configparser.ConfigParser()
config.read('repo.ini')
repo = config.get('repos', 'repo1')
# repo = input("Please enter your repo name: ")




result = gh.repository(user, repo).pull_requests('open')


def list_all_prs():
    # open csv file and create header rows

    with open('\\\\share\\pull.csv', 'w+', newline='') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['Id', 'Login', 'Title', 'Commits', 'Changed Files'])

    # iterate through repo for pull requests based on criteria and output to csv file
    for pr in result:
        data = pr.as_dict()
        changes = (gh.repository(user, repo).pull_request(data['number'])).as_dict()
        # keep print to console statement for testing purposes
        # print(changes['id'], changes['user']['login'], changes['title'], changes['commits'], changes['changed_files'])


        with open('\\\\share\\pull.csv','a+',newline='') as f:
            csv_writer = csv.writer(f)

            csv_writer.writerow([changes['id'], changes['user']['login'], changes['title'], changes['commits'],
                                 changes['changed_files']])


list_all_prs()

exec(open("one_commit_one_file_change.py").read())

one_commit_one_file_change script: one_commit_one_file_change脚本:

#! /usr/bin/python
import os
import github3
# from github3 import login, GitHub, authorize
# from getpass import getuser, getpass
import requests
import csv
import configparser
import sys
import main_en_pr
import codecs

sys.__stdout__ = codecs.getwriter('utf8')(sys.stdout)

def one_commit_one_file_change_pr():

    #open csv file and create header rows
    with open('\\\\share\\commit_filechange.csv', 'w+') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['Login', 'Title', 'Commits', 'Changed Files','Deletions', 'Additions'])

#iterate through repo for pull requests based on criteria and output to csv file
    for pr in main_en_pr.result:
        data = pr.as_dict()
        changes = (main_en_pr.gh.repository(main_en_pr.user, main_en_pr.repo).pull_request(data['number'])).as_dict()   

        if changes['commits'] == 1 and changes['changed_files'] == 1:
        #keep print to console statement for testing purposes
        #print changes['user']['login']


            with open('\\\\share\\commit_filechange.csv', 'a+') as f:
                csv_writer = csv.writer(f)

                csv_writer.writerow([changes['user']['login'], changes['title'], changes['commits'], changes['changed_files']])

one_commit_one_file_change_pr()

A way to do this is to have a class in the first script that does the authentication in its constructor. 一种方法是在第一个脚本中有一个类,该类在其构造函数中进行身份验证。

Create an object of the above class in the second script and you will have access to the creds 在第二个脚本中创建上述类的对象,您将可以访问证书

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM