简体   繁体   中英

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.

Everything seems to be working except I'm being prompted for GitHub credentials when the second script runs.

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.

The repo and user variables seem to be passing to the second script correctly.

Please see code below, and thanks in advance for any guidance.

main_en_pr script:

#! /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:

#! /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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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