def testCreateFile(self): newFile = '/doc/testCreateUpdateDeleteFile.md' content = 'Hello world' self.repo.create_file( path=newFile, message='Create file for testCreateFile', content=content, branch="master", committer=github.InputGitAuthor("Enix Yu", "*****@*****.**", "2016-01-15T16:13:30+12:00"), author=github.InputGitAuthor("Enix Yu", "*****@*****.**", "2016-01-15T16:13:30+12:00"))
def testUpdateFile(self): updateFile = '/doc/testCreateUpdateDeleteFile.md' content = 'Hello World' sha = self.repo.get_contents(updateFile).sha self.repo.update_file( path=updateFile, message='Update file for testUpdateFile', content=content, sha=sha, branch="master", committer=github.InputGitAuthor("Enix Yu", "*****@*****.**", "2016-01-15T16:13:30+12:00"), author=github.InputGitAuthor("Enix Yu", "*****@*****.**", "2016-01-15T16:13:30+12:00"))
def testCreateGitCommitWithAllArguments(self): tree = self.repo.get_git_tree( "107139a922f33bab6fbeb9f9eb8787e7f19e0528") commit = self.repo.create_git_commit( "Commit created by PyGithub", tree, [], github.InputGitAuthor("John Doe", "*****@*****.**", "2008-07-09T16:13:30+12:00"), github.InputGitAuthor("John Doe", "*****@*****.**", "2008-07-09T16:13:30+12:00")) self.assertEqual(commit.sha, "526946197ae9da59c6507cacd13ad6f1cfb686ea")
def testCreateGitTagWithAllArguments(self): tag = self.repo.create_git_tag( "TaggedByPyGithub2", "Tag also created by PyGithub", "526946197ae9da59c6507cacd13ad6f1cfb686ea", "commit", github.InputGitAuthor("John Doe", "*****@*****.**", "2008-07-09T16:13:30+12:00")) self.assertEqual(tag.sha, "f0e99a8335fbc84c53366c4a681118468f266625")
def __init__(self, ACCESS_TOKEN, REPO_NAME, PATH, BRANCH, PULL_REQUEST, COMMIT_MESSAGE, COMMITTER): ''' Initial GithubContributors Args: ACCESS_TOKEN (str): Personal Access Token for Github REPO_NAME (str): The name of the repository PATH (str): The path to the file BRANCH (str): The branch of the file PULL_REQUEST (str): Pull request target branch, none means do not open a pull request COMMIT_MESSAGE (str): Commit message you want to use COMMITTER (str): Committer you want to use to commit the file ''' self.__commit_message = COMMIT_MESSAGE self.__path = PATH self.__branch = BRANCH self.__pull_request = PULL_REQUEST self.__sha = '' self.__releases = {} self.__changelog = '' self.__file_exists = False # Use PyGithub to login to the repository # References: https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository g = github.Github(ACCESS_TOKEN) self.__repo = g.get_repo(REPO_NAME) self.__author = github.GithubObject.NotSet if COMMITTER == '' else github.InputGitAuthor( COMMITTER.split(' ')[0], COMMITTER.split(' ')[1])
def run(): args = parse_args() codetools.setup_logging(args.debug) gh_org_name = args.org tags = args.tag git_email = codetools.lookup_email(args) git_user = codetools.lookup_user(args) tagger = github.InputGitAuthor( git_user, git_email, codetools.current_timestamp() ) debug(tagger) global g g = pygithub.login_github(token_path=args.token_path, token=args.token) org = g.get_organization(gh_org_name) info("tagging repos in org: {org}".format(org=org.login)) tag_teams = get_candidate_teams(org, args.allow_team) target_repos = get_candidate_repos(tag_teams) problems = [] # do not fail-fast on non-write operations problems += check_repos( target_repos, args.allow_team, args.deny_team, fail_fast=False, ) # existing tags are always ignored (not an error) under --delete ignore_existing = True if args.delete else args.ignore_existing_tag # do not fail-fast on non-write operations present_tags, absent_tags, err = check_tags( target_repos, tags, ignore_existing=ignore_existing, fail_fast=False, ) problems += err if problems: msg = "{n} repo(s) have errors".format(n=len(problems)) raise codetools.DogpileError(problems, msg) if args.delete: untag_repos(present_tags, dry_run=args.dry_run) else: tag_repos(absent_tags, tagger=tagger, dry_run=args.dry_run)
def create_file(self, file_path, file_name, file_data, branch_name, commit_message, author='Integration'): author = github.InputGitAuthor(author, GITHUB_AUTHOR_EMAIL) return self._repo.create_file(file_path + file_name, commit_message, file_data, branch=branch_name, author=author)
def update_file(self, file_path, file_name, file_data, branch_name, commit_message, author='Integration'): author = github.InputGitAuthor(author, GITHUB_AUTHOR_EMAIL) contents = self._repo.get_contents(file_path + file_name, ref=branch_name) return self._repo.update_file(contents.path, commit_message, file_data, contents.sha, branch=branch_name, author=author)
def create_git_author(named_user): """ :param named_user: :class:`github.NamedUser.NamedUser` :rtype: :class:`github.InputGitAuthor.InputGitAuthor` """ user_name = named_user.login display_name = named_user.name email = named_user.email assert user_name if not display_name: display_name = user_name if not email: email = user_name + "@users.noreply.github.com" return github.InputGitAuthor(display_name, email)
def git(): # create a github instance with the current token g = Github(token) # get user will get me, because I authenticated my account user = g.get_user() # I want to get the OSF-test repo repo = user.get_repo('OSF-test') # we're going to create a blob. our first step is to get the content # of that blob file = open('/Users/samportnow/Documents/git-test/test.txt', 'r') contents = file.read() # now we can create a blob of the contents my_blob = repo.create_git_blob(contents, 'utf-8') # now we need to get the master branch master_branch = repo.get_git_ref('heads/master') # and the base commit of that master branch base_commit = repo.get_commit(sha=master_branch._object._sha) # and the tree were are going to committing to tree = github.InputGitTreeElement(path='test.txt', mode='100755', type='blob', sha=my_blob.sha) # now we create a NEW Tree! new_tree = repo.create_git_tree(base_tree=base_commit._commit.tree, tree=[tree]) # now we can finally commit! # lets try to use a DIFFERENT author, whose on my collaborator team # (this works) contributor = g.get_user('sullytesting1987') email = contributor._email name = contributor._name author = github.InputGitAuthor(name=name, email=email, date=str(datetime.datetime.now())) commit = repo.create_git_commit("This is a commit", tree=new_tree, parents=[master_branch._object._sha], author=author) # note: i changed the pygithub code for the parents list. # they are looking for github commit objects, but all that # is really needed is the sha of the master branch. so i got that instead # and finally we update the ref # note: pygithub's equivalent of update ref is edit! master_branch.edit(commit.sha) return 'SUCCESS'
def create_pull(self): g = github.Github(self.config["token"]) repo = g.get_repo(self.config["repo"]) slug = self.form['post_id'] name = self.form['name'] commentid = self.data['id'] branchname = f"comment-{commentid}" sb = repo.get_branch(self.config['mainbranch']) repo.create_git_ref(ref='refs/heads/' + branchname, sha=sb.commit.sha) committer = github.InputGitAuthor(name, self.form['email']) filename = f"_data/comments/{slug}/{commentid}.yml" title = f"Comment {commentid} from {name} to {slug}" repo.create_file(filename, title, yaml.dump(self.data), branchname, committer) repo.create_pull(title=title, body="", head=branchname, base=self.config['mainbranch'])
def tagRefRelease(repo, out): try: if os.environ["MBSIMENVTAGNAME"] != "latest": print( "Skipping setting rlease tags on github, this is the staging system!" ) out.clear() out['success'] = True out['message'] = "Skipping setting rlease tags on github, this is the staging system!" return ghrepo = gh.getMbsimenvRepo(repo) commitid = getattr(run, repo + "UpdateCommitID") message="Release "+releaseVersion+" of MBSim-Env for "+platform+"\n"+\ "\n"+\ "The binary "+platform+" release can be downloaded from\n"+\ "https://"+os.environ['MBSIMENVSERVERNAME']+django.urls.reverse("service:releases")+"\n"+\ "Please note that this binary release includes a full build of MBSim-Env not only of this repository." # create github tag gittag = ghrepo.create_git_tag( tagName, message, commitid, "commit", tagger=github.InputGitAuthor( request.user.username, request.user.email, datetime.date.today().strftime("%Y-%m-%dT%H:%M:%SZ"))) # create git tag ghrepo.create_git_ref("refs/tags/" + tagName, gittag.sha) # create release ghrepo.create_git_release( tagName, "Release " + releaseVersion + " of MBSim-Env for " + platform, message) except github.GithubException as ex: out.clear() out['success'] = False out['message'] = ex.data[ "message"] if "message" in ex.data else str(ex.data) except: import traceback out.clear() out['success'] = False out['message'] = "Internal error: Please report the following error to the maintainer:\n" + traceback.format_exc( )
def run(proposed_version: str = os.getenv("TBS_CLI_VERSION")): gh = Github(os.getenv("GITHUB_TOKEN")) repo = gh.get_organization("3blades").get_repo("python-cli-tools") tags = list(repo.get_tags()) names_only = sorted([t.name for t in tags], key=semver_to_int, reverse=True) if proposed_version in names_only: raise ValueError( f"Proposed version {proposed_version} already exists as a release in Github" ) elif sorted(names_only + [proposed_version], key=semver_to_int, reverse=True)[0] != proposed_version: raise ValueError( f"Proposed version {proposed_version} would not be the highest version." ) else: release_time = datetime.now().isoformat(timespec="seconds") + "-00:00" tagger = github.InputGitAuthor(name="3Blades", email="*****@*****.**", date=release_time) kwargs = dict(tag=proposed_version, tag_message=os.getenv("TRAVIS_COMMIT_MESSAGE"), release_name=proposed_version, release_message=os.getenv("TRAVIS_COMMIT_MESSAGE"), object=os.getenv("TRAVIS_COMMIT"), type="commit", tagger=tagger, draft=True) if os.getenv("TRAVIS_PULL_REQUEST") != "false": print( f"Would be creating a tag and release with the following values:\n{kwargs}" ) else: repo.create_git_tag_and_release(**kwargs) print("Successfully created tag and release")
def run(): """Create the tag""" args = parse_args() codetools.setup_logging(args.debug) git_tag = args.tag # if email not specified, try getting it from the gitconfig git_email = codetools.lookup_email(args) # ditto for the name of the git user git_user = codetools.lookup_user(args) # The default eups tag is derived from the git tag, otherwise specified # with the --eups-tag option. The reason to currently do this is that for # weeklies and other internal builds, it's okay to eups publish the weekly # and git tag post-facto. However for official releases, we don't want to # publish until the git tag goes down, because we want to eups publish the # build that has the official versions in the eups ref. if not args.manifest_only: eups_tag = args.eups_tag if not eups_tag: # generate eups-style version eups_tag = eups.git_tag2eups_tag(git_tag) debug("using eups tag: {eups_tag}".format(eups_tag=eups_tag)) # sadly we need to "just" know this # XXX this can be parsed from the eups tag file post d_2018_05_08 manifest = args.manifest debug("using manifest: {manifest}".format(manifest=manifest)) if not args.manifest_only: # release from eups tag message_template = "Version {{git_tag}}"\ " release from {eups_tag}/{manifest}".format( eups_tag=eups_tag, manifest=manifest, ) else: # release from manifest only message_template = "Version {{git_tag}}"\ " release from manifest {manifest}".format( manifest=manifest, ) debug("using tag message: {msg}".format(msg=message_template)) tagger = github.InputGitAuthor( git_user, git_email, codetools.current_timestamp(), ) debug("using taggger: {tagger}".format(tagger=tagger)) global g g = pygithub.login_github(token_path=args.token_path, token=args.token) org = g.get_organization(args.org) info("tagging repos in org: {org}".format(org=org.login)) problems = [] manifest_products = versiondb.Manifest( manifest, base_url=args.versiondb_base_url).products if not args.manifest_only: # cross-reference eups tag version strings with manifest eups_products = eups.EupsTag(eups_tag, base_url=args.eupstag_base_url).products # do not fail-fast on non-write operations products, err = cross_reference_products( eups_products, manifest_products, ignore_manifest_versions=args.ignore_manifest_versions, fail_fast=False, ) problems += err else: # no eups tag; use manifest products without sanity check against eups # tag version strings products = manifest_products if args.limit: products = dict(itertools.islice(products.items(), args.limit)) # do not fail-fast on non-write operations products, err = get_repo_for_products( org=org, products=products, allow_teams=args.allow_team, ext_teams=args.external_team, deny_teams=args.deny_team, fail_fast=False, ) problems += err # do not fail-fast on non-write operations products_to_tag, err = check_product_tags( products, git_tag, tag_message_template=message_template, tagger=tagger, force_tag=args.force_tag, fail_fast=False, ignore_git_message=args.ignore_git_message, ignore_git_tagger=args.ignore_git_tagger, ) problems += err if args.verify: # in verify mode, it is an error if there are products that need to be # tagged. err = identify_products_missing_tags(products_to_tag) problems += err if problems: msg = "{n} pre-flight error(s)".format(n=len(problems)) raise codetools.DogpileError(problems, msg) tag_products( products_to_tag, fail_fast=args.fail_fast, dry_run=args.dry_run, )
def sync(self, entrega_dir: pathlib.Path, rama: str, *, target_subdir: str = None): """Importa una entrega a los repositorios de alumnes. Args: entrega_dir: ruta en repo externo con los archivos actualizados. rama: rama en la que actualizar la entrega. target_subdir: directorio que se debe actuaizar dentro el repositorio. Si no se especifica, se usa el nombre de la rama (usar la cadena vacía para actualizar el toplevel). Raises: github.UnknownObjectException si el repositorio no existe. github.GithubException si se recibió algún otro error de la API. """ if target_subdir is None: target_subdir = rama gh = github.Github(GITHUB_TOKEN) repo = self.gh_repo or gh.get_repo(self.repo_full) gitref = repo.get_git_ref(f"heads/{rama}") ghuser = random.choice(self.github_users) # ¯\_(ツ)_/¯ Only entregas knows. prefix_re = re.compile(re.escape(target_subdir.rstrip("/") + "/")) # Estado actual del repo. cur_sha = gitref.object.sha # NOTE: como solo trabajamos en un subdirectorio, se podría limitar el uso # de recursive a ese directorio (si trabajáramos con repos muy grandes). cur_tree = repo.get_git_tree(cur_sha, recursive=True) cur_commit = repo.get_git_commit(cur_sha) # Tree de la entrega en master, para manejar borrados. baseref = repo.get_git_ref(f"heads/{repo.default_branch}") base_tree = repo.get_git_tree(baseref.object.sha, recursive=True) # Examinar el repo de entregas para obtener los commits a aplicar. entrega_repo = git.Repo(entrega_dir, search_parent_directories=True) entrega_relpath = entrega_dir.relative_to(entrega_repo.working_dir).as_posix() pending_commits = [] cur_commit_date = cur_commit.author.date # La fecha de la API siempre viene en UTC, pero PyGithub no le asigna # timezone, y se interpretaría en zona horaria local por omisión. Ver # https://github.com/PyGithub/PyGithub/pull/704. cur_commit_date = cur_commit_date.replace(tzinfo=timezone.utc) for commit in entrega_repo.iter_commits(paths=[entrega_relpath]): if commit.authored_date > cur_commit_date.timestamp(): pending_commits.append(commit) for commit in reversed(pending_commits): entrega_tree = commit.tree.join(entrega_relpath) tree_contents = tree_to_github(entrega_tree, target_subdir, repo) entrega_files = set(tree_contents.keys()) tree_elements = list(tree_contents.values()) tree_elements.extend( deleted_files(entrega_files, cur_tree, prefix_re, base_tree) ) author_date = datetime.fromtimestamp(commit.authored_date).astimezone() author_info = github.InputGitAuthor( ghuser, f"{ghuser}@users.noreply.github.com", author_date.isoformat() ) cur_tree = repo.create_git_tree(tree_elements, cur_tree) cur_commit = repo.create_git_commit( commit.message, cur_tree, [cur_commit], author_info ) # Se necesita obtener el árbol de manera recursiva para tener # los contenidos del subdirectorio de la entrega. cur_tree = repo.get_git_tree(cur_tree.sha, recursive=True) gitref.edit(cur_commit.sha)
def git_author(): return github.InputGitAuthor(name='foo', email='*****@*****.**')
#!/bin/env python3 """ Pushed the content in version file as github tag. run the script on merge. """ import os import github GITHUB_TOKEN = os.environ['GITHUB_TOKEN'] REPO_NAME = os.environ['TRAVIS_REPO_SLUG'] G = github.Github(base_url="https://api.github.com", login_or_token=GITHUB_TOKEN) REPO = G.get_repo(REPO_NAME) SHA = REPO.get_commit('master').sha VERSION_FILE = open("version", "r") VERSION = VERSION_FILE.read().strip('\n').split('=')[1] VERSION_FILE.close() TAGGER = github.InputGitAuthor('bot', 'bot@localhost') REPO.create_git_tag(VERSION, 'auto push tag' + VERSION, SHA, 'commit', TAGGER) REPO.create_git_ref("refs/tags/v" + VERSION, SHA)
conn = http.client.HTTPConnection('www.pref.osaka.lg.jp') conn.request('GET', '/default.html') response = conn.getresponse() if (response.status != 200): raise RuntimeError(f'HTTP request failed (status: {response.status})') return response.read().decode('CP932') def get_repository(token, repository): return github.Github(token).get_repo(repository) def save_json(repository, report, committer): return repository.create_file( path=f'v1/{report.filename()}', content=report.content(), message='Update', branch='gh-pages', committer=committer, ) html = fetch_page() report = Report(html) repository = get_repository(os.environ.get('GITHUB_TOKEN'), os.environ.get('GITHUB_REPOSITORY')) committer = github.InputGitAuthor(os.environ.get('GIT_COMMITTER_NAME'), os.environ.get('GIT_COMMITTER_EMAIL')) save_json(repository, ReportJsonPresenter(report), committer)