[英]Python 3.5: Passing GitHub credentials to another python script
我有一個python腳本輪詢GitHub存儲庫中的拉取請求,還有一個附加腳本查找提交和更改的文件等於1。
似乎一切正常,除了在第二個腳本運行時提示我輸入GitHub憑據。
我正在嘗試將gh
變量傳遞給第二個腳本,該變量將主腳本中存儲庫的憑據保存到第二個腳本中,因此不必再次輸入憑據。
repo
和user
變量似乎已正確傳遞給第二個腳本。
請參見下面的代碼,並在此先感謝您的指導。
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腳本:
#! /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()
一種方法是在第一個腳本中有一個類,該類在其構造函數中進行身份驗證。
在第二個腳本中創建上述類的對象,您將可以訪問證書
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.