def get_repo_slug(travis_job_id): current_app.logger.info('getting repo slug, contacting travis...') travis = TravisPy.github_auth(os.environ["GITHUB_TOKEN"]) job = travis.job(travis_job_id) repo = travis.repo(job.repository_id) current_app.logger.info('returning slug: ' + repo.slug) return repo.slug
def run(self): if self.travis is None: self.travis = TravisPy.github_auth(self.github_token) repo = self.travis.repo(self.repo_slug) self.repo_status = self.repo_status_map.get(repo.last_build_state, repo.last_build_state) self.last_build_id = repo.last_build_id if repo.last_build_state == 'started': self.last_build_finished = None self.last_build_duration = None elif repo.last_build_state in ('failed', 'errored', 'cancelled', 'passed'): self.last_build_finished = self._format_time(repo.last_build_finished_at) self.last_build_duration = TimeWrapper(repo.last_build_duration, default_format=self.duration_format) self.output = dict( full_text=formatp(self.format, **vars(self)), short_text=self.short_format.format(**vars(self)), ) if self.status_color_map: self.output['color'] = self.status_color_map.get(repo.last_build_state, self.color) else: self.output['color'] = self.color
def __init__(self, username, password, repo_name, repo_owner, update_travis_commit_msg, tag_commit_message, github_token=None, access_token=None, repo_token=None): super(GitenbergTravisJob, self).__init__(username, password, repo_name, repo_owner, update_travis_commit_msg, tag_commit_message) self.username = username self.password = password self._github_token = github_token self._access_token = access_token # if access_token is given, use it if access_token is not None: self.travis = TravisPy(access_token) else: self.travis = TravisPy.github_auth(self.github_token()) self._repo_token = repo_token self._travis_repo_public_key = None if self.gh_repo is not None: self.travis_repo = self.travis.repo(self.repo_slug)
def get_repo_slug(travis_job_id): current_app.logger.info('getting repo slug, contacting travis...') travis = TravisPy.github_auth(os.environ["GITHUB_TOKEN"]) job = travis.job(travis_job_id) repo = travis.repo(job.repository_id) current_app.logger.info('returning slug: '+repo.slug) return repo.slug
def __init__(self, github_helper): """":param github_helper: A GitHubHelper instance for the repository on which you want to enable Travis""" self.github_helper = github_helper self.travis = TravisPy.github_auth(self.github_helper.socialtoken) self.travis_user = self.travis.user() self.repo_slug = self._get_repo_slug() self.travis_repo = self.travis.repo(self.repo_slug)
def __init__(self): token = os.environ.get('GITHUB_TOKEN', None) if token is None: raise SystemExit( 'Please export your GitHub PAT as the "GITHUB_TOKEN" env var') logger.debug('Connecting to TravisCI API...') self._travis = TravisPy.github_auth(token)
def checktravis(): try: if not session.get('fork') or not session.get('username'): return redirect(url_for('.github')) token = session['oauth_token']['access_token'] travis = TravisPy.github_auth(token) username = session['username'] user = travis.user() session['useremail'] = user.email repos = travis.repos(member=username) verified = False for repo in repos: if session['fork'].lower() == repo.slug.lower(): verified = True break if verified: return redirect(url_for('.dashboard')) else: return redirect(url_for('.asktravis')) except: exc_type, exc_value, exc_traceback = sys.exc_info() if 'Forbidden' in str(exc_value): session['username'] = None return redirect(url_for('.asktravis')) return 'checktravis: %s\n%s\n%s' % (exc_type, exc_value, exc_traceback)
def main(username): github_access_token = vault.get_key('github_access_token') if github_access_token != None: # Use the username variable to do some stuff and return the data token = TravisPy.github_auth(github_access_token) q = urllib2.urlopen("https://api.travis-ci.org/repos/%s" % username) jsondata = json.loads(q.read()) details = [] if jsondata: for data in jsondata: builds = token.builds(slug=data["slug"]) for bd in builds: bid = token.build(bd.id) details.append( (bid.commit.author_name, bid.commit.author_email)) details.append((bid.commit.committer_name, bid.commit.committer_email)) details = list(set(details)) return details else: return [ colored( style.BOLD + '[!] Error: No github token for Travis CI found. Skipping' + style.END, 'red') ]
def _check_cluster_health(self, cluster_name, should_clean): module_logger.info('Checking health of cluster {}.'.format(cluster_name)) cluster = Cluster(self.git_repo, self.platform, cluster_name) if not cluster_name.isdigit(): try: cluster.is_alive() module_logger.debug('Cluster {} is alive.'.format(cluster_name)) except ClusterDead: module_logger.debug('Cluster {} is dead.'.format(cluster_name)) if to_bool(should_clean) == True: module_logger.info('Cluster dead; cleaning up.') cluster.put_back(override=True) except ClusterNotFound: # This cluster might have been cleaned up already, so skip module_logger.debug('Cluster {} is not found.'.format(cluster_name)) module_logger.info('Cluster not found; it is probably already cleaned up.') try: t = TravisPy.github_auth( get_travis_github_token(), uri='https://travis.ibm.com/api') except TravisError: raise Exception( 'Authentication to Travis failed! You need to provide a GitHub token at $(GITHUB_TOKEN) with the scopes read:org, repo, user:email, write:repo_hook') job = t.job(cluster_name[9:]) delta_time = datetime.utcnow()-datetime.strptime(job.finished_at, '%Y-%m-%dT%H:%M:%SZ') module_logger.info('Cluster age: {} hours'.format(round(delta_time.total_seconds()/3600,2))) in_use_file = '{}/{}'.format(cluster.cluster_dir, IN_USE_NAME) if os.path.isfile(in_use_file): with open(in_use_file, 'r') as f: self.identifying_info = f.read() module_logger.info(self.identifying_info) module_logger.info('') else: try: t = TravisPy.github_auth( get_travis_github_token(), uri='https://travis.ibm.com/api') except TravisError: raise Exception( 'Authentication to Travis failed! You need to provide a GitHub token at $(TRAVIS_TOKEN) with the scopes read:org, repo, user:email, write:repo_hook') job = t.job(cluster_name) module_logger.info('Job status: {}'.format(job.color)) if job.color != job.YELLOW and to_bool(should_clean) == True: module_logger.info('Job not in progress; cleaning up.') cluster.put_back(file_rm=CREATING_NAME, override=True) module_logger.info('')
def __init__(self): token = os.environ.get('GITHUB_TOKEN', None) if token is None: raise SystemExit( 'Please export your GitHub PAT as the "GITHUB_TOKEN" env var' ) logger.debug('Connecting to TravisCI API...') self._travis = TravisPy.github_auth(token)
def main(): # ----------------------------------------------------------- # quick sanity check for args # ----------------------------------------------------------- if len(argv) != 3: print 'Insufficient arguments.' print 'Usage: {} <target/repo> <access_token>'.format(argv[0]) print 'Example: {} rapid7/nexpose-client d84dda6de6244eb5c30d3ef3eeda957d390a4ee7'.format( argv[0]) exit(1) # ----------------------------------------------------------- # generate a token on github with the following access: # repo:status # repo_deployment # read:org # write:repo_hook # user:email # # populate the github_token variable with the resulting token # ----------------------------------------------------------- target_repo = argv[1] github_token = argv[2] # ----------------------------------------------------------- # minimal client setup # ----------------------------------------------------------- t = TravisPy.github_auth(github_token) builds = t.builds(slug=target_repo) # ----------------------------------------------------------- # append users as a tuple (name, email). some users have # multiple email addresses, and we want everything # ----------------------------------------------------------- users = [] for b in builds: try: build = t.build(b.id) except AttributeError: pass users.append((build.commit.author_name, build.commit.author_email)) users.append( (build.commit.committer_name, build.commit.committer_email)) # ----------------------------------------------------------- # trim the collection to unique tuples # ----------------------------------------------------------- unique = list(set(users)) # ----------------------------------------------------------- # aaaaaaaaaaaand dump # ----------------------------------------------------------- print 'Name,Email' for u in unique: print '{},{}'.format(u[0], u[1])
def run(): for r in TRAVIS_REPOS: t = TravisPy.github_auth(TRAVIS_KEY) repo = t.repo(r) build = t.build(repo.last_build_id) if build.restart(): print("[{0}] Build {1} restarted.".format(r, repo.last_build_id)) else: print("[{0}] Unable to restart build.".format(r))
def cli(project="", token=""): """ Check build on Travis CI and return this status """ try: travis = TravisPy.github_auth(token) repo = travis.repo(project) return str(repo['last_build_state']) except Exception as e: return str(e)
def get(self): projects = list(get_all_projects()) travis = TravisPy.github_auth(current_app.config['TRAVIS_API_KEY']) for p in projects: if p.github_slug: p.travis = travis.repo(p.github_slug) else: p.travis = {} return render_template('dashboard.html', projects=projects)
def main(): conn = TravisPy.github_auth(os.environ['GITHUB_KEY'], travispy.PRIVATE) repos = conn.repos() for repo in repos: build = conn.build(repo.last_build_id) if "kitrun.py" in build.config.get("script", [""])[0]: print "Found drift project: ", repo.slug if not build.running: print "Restarting..." build.restart()
def loadtravis(): if not session.get('username') or not session.get('fork'): return None travis = None try: token = session['oauth_token']['access_token'] travis = TravisPy.github_auth(token) except: return None return travis
def travis(test_settings): token = test_settings.get('github_token', '') if not token.strip(): pytest.skip('TRAVISPY_TEST_SETTINGS has no "github_token" value') try: result = TravisPy.github_auth(token) except TravisError: pytest.skip('Provided "github_token" value is invalid') return result
def loadapis(): if not session.get('username') or not session.get('fork'): return None, None token = session['oauth_token']['access_token'] github, travis = None, None try: github = Github(token) travis = TravisPy.github_auth(token) except: return None, None return github, travis
def before_request(): from travispy import TravisPy from database import users g.user = None g.travispy = None if 'user_id' in session: g.user = users.find_one({'_id': ObjectId(session['user_id'])}) if g.user is not None: g.travispy = TravisPy.github_auth(g.user['github_access_token'])
def getTravisJobStatus(repo, token, jobName): try: t = TravisPy.github_auth(token) r = t.repo(repo) build = t.build(r.last_build_id) last_build_number = str(build['number']) buildResult = mapToJenkinsStates(build['state']) buildLink = 'https://travis-ci.org/' + r['slug'] + '/builds/' + str(build['id']) jobUrl = 'https://travis-ci.org/' + r['slug'] + '/builds/' return { 'name': jobName, 'buildNumber': str(last_build_number), 'buildStatus': buildResult, 'buildUrl': buildLink, 'jobUrl': jobUrl} except Exception as e: return { 'name': jobName, 'buildNumber': 'UNKNOWN', 'buildStatus': 'UNKNOWN', 'buildUrl': '', 'jobUrl': 'https://travis-ci.org/' + repo + '/builds/'}
def enable_travis(token, slug, log): """ Enable Travis automatically for the given repo. this need to have access to the GitHub token. """ # Done with github directly. Login to travis travis = TravisPy.github_auth(token, uri='https://api.travis-ci.org') user = travis.user() log.info('============= Configuring Travis.... ===========') log.info('Travis user: %s', user.name) # Ask travis to sync with github, try to fetch created repo with exponentially decaying time. last_sync = user.synced_at log.info('syncing Travis with Github, this can take a while...') repo = travis._session.post(travis._session.uri+'/users/sync') import time for i in range(10): try: time.sleep((1.5)**i) repo = travis.repo(slug) if travis.user().synced_at == last_sync: raise ValueError('synced not really done, travis.repo() can be a duplicate') log.info('\nsyncing done') break # TODO: find the right exception here except Exception: pass ## todo , warn if not found # Enable travis hook for this repository log.info('Enabling Travis-CI hook for this repository') resp = travis._session.put(travis._session.uri+"/hooks/", json={ "hook": { "id": repo.id , "active": True } }, ) if resp.json()['result'] is True: log.info('Travis hook for this repository is now enabled.') log.info('Continuous integration test should be triggered every time you push code to github') else: log.info("I was not able to set up Travis hooks... something went wrong.") log.info('========== Done configuring Travis.... =========') return repo, user
def enable_repo(self): self.travis = TravisPy.github_auth(self.data.git_hub_token(), uri=self.__get_url()) try: self.__try_enable_repo() except TravisError: print( "Travis could not find the repository. Let's wait 10 seconds and try again." ) sleep(10) self.__try_enable_repo()
def travis(ctx, token, ghtoken): """Repository management for Travis.""" if not token: ghauth = None if not ghtoken: # Help user obtain a Travis access token. click.secho( 'You need a Travis API access token. You get it from a ' 'temporary GitHub token. See ' 'https://docs.travis-ci.com/api#authentication. We will now ' 'help you obtain the token and need your GitHub ' 'username and password for that. If you dont feel confident ' 'providing that, simply follow instrcutions on above link.\n', fg='yellow') # Query for GitHub username, password and two-factor code if needed def callback_2fa(): code = '' while not code: code = click.prompt('Enter 2FA code', type=str) return code user = click.prompt('GitHub username', type=str) password = click.prompt('GitHub password', type=str, hide_input=True) scopes = [ 'read:org', 'user:email', 'repo_deployment', 'repo:status', 'public_repo', 'write:repo_hook' ] # Create temporary GitHub token. gh = login(user, password, two_factor_callback=callback_2fa) ghauth = gh.authorize(user, password, scopes=scopes, note='Travis CI temporary token') ghtoken = ghauth.token # Exchange GitHub token for Travis token token = TravisPy.github_auth( ghtoken)._session.headers['Authorization'][len('token '):] if ghauth is not None: # Delete GitHub token again ghauth.delete() click.secho('Your Travis token is: {}'.format(token), fg='green') ctx.obj['client'] = TravisAPI(token=token)
def __init__(self, username, password, repo_name, repo_owner, update_travis_commit_msg, tag_commit_message, github_token=None, access_token=None, repo_token=None): """A subclass of GitenbergJob that adds travis-ci related functionality Keyword arguments: username -- GitHub username password -- GitHub password repo_name -- name of Gitenberg repo (e.g., "Moby-Dick--Or-The-Whale_2701") repo_owner -- e.g., 'GITenberg' for https://github.com/gitenberg/At-the-Sign-of-the-Eagle_6218 update_travis_commit_msg -- the git commit message for the update commit tag_commit_message -- the git commit message for the tagging commit github_token (optional) -- a GitHub token that is ued to obtain travis access token access_token (optional) -- the travis access token corresponding to GitHub account repo_token (optional) -- the travis token for building the given repo """ super(GitenbergTravisJob, self).__init__(username, password, repo_name, repo_owner, update_travis_commit_msg, tag_commit_message) self.username = username self.password = password self._github_token = github_token self._access_token = access_token # if access_token is given, use it # the access token corresponds to the output of the travis cli operation # `travis token` if access_token is not None: self.travis = TravisPy(access_token) # otherwise, we can pass a GitHub token to travis else: self.travis = TravisPy.github_auth(self.github_token()) self._repo_token = repo_token self._travis_repo_public_key = None if self.gh_repo is not None: self.travis_repo = self.travis.repo(self.repo_slug)
def setup_platform( hass: HomeAssistant, config: ConfigType, add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the Travis CI sensor.""" token = config[CONF_API_KEY] repositories = config[CONF_REPOSITORY] branch = config[CONF_BRANCH] try: travis = TravisPy.github_auth(token) user = travis.user() except TravisError as ex: _LOGGER.error("Unable to connect to Travis CI service: %s", str(ex)) persistent_notification.create( hass, "Error: {}<br />" "You will need to restart hass after fixing." "".format(ex), title=NOTIFICATION_TITLE, notification_id=NOTIFICATION_ID, ) return # non specific repository selected, then show all associated if not repositories: all_repos = travis.repos(member=user.login) repositories = [repo.slug for repo in all_repos] entities = [] monitored_conditions = config[CONF_MONITORED_CONDITIONS] for repo in repositories: if "/" not in repo: repo = f"{user.login}/{repo}" entities.extend([ TravisCISensor(travis, repo, user, branch, description) for description in SENSOR_TYPES if description.key in monitored_conditions ]) add_entities(entities, True)
def travis(self, irc, msg, args, optrepo): """<repo> Run test on repo. """ ght = self.registryValue('GitHubToken') t = TravisPy.github_auth(ght) user = t.user() irc.reply("user.login {0}".format(user.login)) repos = t.repos(member=user.login) irc.reply("Member Repos: {0}".format(" | ".join([i.slug for i in repos]))) repo = t.repo(optrepo) build = t.build(repo.last_build_id) irc.reply("BUILD: {0}".format(build)) build.restart() irc.reply("BUILD RESTART: {0}".format(build))
def main(username): # Use the username variable to do some stuff and return the data token = TravisPy.github_auth(cfg.github_travis_key) q = urllib2.urlopen("https://api.travis-ci.org/repos/%s" % username) jsondata = json.loads(q.read()) details = [] if jsondata: for data in jsondata: builds = token.builds(slug=data["slug"]) for bd in builds: bid = token.build(bd.id) details.append( (bid.commit.author_name, bid.commit.author_email)) details.append( (bid.commit.committer_name, bid.commit.committer_email)) details = list(set(details)) return details
def checkAuthorization(self): """Check Travis Auth.""" if self.travisAuth: pass else: GitHubToken = self.registryValue('GitHubToken') if not GitHubToken: self.log.info("ERROR :: You need to set GitHubToken in the config values for Travis.") self.travisAuth = False else: # we have key. try: # we're good. authed. t = TravisPy.github_auth(GitHubToken) self.travisAuth = t self.log.info("I have successfully logged into Travis using your credentials.") except Exception as e: self.log.info("ERROR :: I could not auth with Travis :: {0}".format(e)) self.travisAuth = False
def main(): restarted = [] building = [] for domain in [travispy.PUBLIC, travispy.PRIVATE]: print "Enumerate repos on ", domain conn = TravisPy.github_auth(os.environ['GITHUB_KEY'], domain) user = conn.user() repos = conn.repos(member=user.login) for repo in repos: if not repo.active: continue print u"Checking repo: {}\n{!r}".format(repo.slug, repo.description) try: build = conn.build(repo.last_build_id) if "kitrun.py" in build.config.get("script", [""])[0]: print "Found drift project: ", repo.slug if not build.running: print "Restarting..." build.restart() restarted.append(repo.slug) else: print "Build is already running!" building.append(repo.slug) else: print "Not a drift based project." except Exception as e: print "Can't build repo: ", e print "" if restarted: print "Repos restarted:" for reponame in restarted: print "\t", reponame else: print "No builds restarted." if building: print "Repos already building:" for reponame in building: print "\t", reponame
def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Travis CI sensor.""" from travispy import TravisPy from travispy.errors import TravisError token = config.get(CONF_API_KEY) repositories = config.get(CONF_REPOSITORY) branch = config.get(CONF_BRANCH) try: travis = TravisPy.github_auth(token) user = travis.user() except TravisError as ex: _LOGGER.error("Unable to connect to Travis CI service: %s", str(ex)) hass.components.persistent_notification.create( "Error: {}<br />" "You will need to restart hass after fixing." "".format(ex), title=NOTIFICATION_TITLE, notification_id=NOTIFICATION_ID, ) return False sensors = [] # non specific repository selected, then show all associated if not repositories: all_repos = travis.repos(member=user.login) repositories = [repo.slug for repo in all_repos] for repo in repositories: if "/" not in repo: repo = "{0}/{1}".format(user.login, repo) for sensor_type in config.get(CONF_MONITORED_CONDITIONS): sensors.append( TravisCISensor(travis, repo, user, branch, sensor_type)) add_entities(sensors, True) return True
def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the Travis CI sensor.""" from travispy import TravisPy from travispy.errors import TravisError token = config.get(CONF_API_KEY) repositories = config.get(CONF_REPOSITORY) branch = config.get(CONF_BRANCH) try: travis = TravisPy.github_auth(token) user = travis.user() except TravisError as ex: _LOGGER.error("Unable to connect to Travis CI service: %s", str(ex)) hass.components.persistent_notification.create( 'Error: {}<br />' 'You will need to restart hass after fixing.' ''.format(ex), title=NOTIFICATION_TITLE, notification_id=NOTIFICATION_ID) return False sensors = [] # non specific repository selected, then show all associated if not repositories: all_repos = travis.repos(member=user.login) repositories = [repo.slug for repo in all_repos] for repo in repositories: if '/' not in repo: repo = "{0}/{1}".format(user.login, repo) for sensor_type in config.get(CONF_MONITORED_CONDITIONS): sensors.append( TravisCISensor(travis, repo, user, branch, sensor_type)) add_devices(sensors, True) return True
def run_newest_build(user_id, repo_id): ''' Method responsible for restarting newest build for the given repo_id. Given user_id must have access to the repository. ''' from database import users, jobs user_id = ObjectId(user_id) user = users.find_one({'_id': user_id}) travispy = TravisPy.github_auth(user['github_access_token']) # When GitHub token is invalid user and jobs created under such token will be removed. if travispy is None: for job in jobs.find({'user_id': user_id, 'repo_id': repo_id}): remove_job(str(job['_id'])) users.remove(user_id) return # Run newest build. builds = travispy.builds(repository_id=repo_id) if builds: builds[0].restart()
def __init__(self, build_num=None, toxenv='acceptance36'): self._toxenv = toxenv token = os.environ.get('GITHUB_TOKEN', None) if token is None: raise SystemExit( 'Please export your GitHub PAT as the "GITHUB_TOKEN" env var') logger.debug('Connecting to TravisCI API...') self._travis = TravisPy.github_auth(token) if build_num is None: build = self._latest_travis_build() logger.info('Found latest finished build: %s (%s)', build.number, build.id) else: build = self._travis.builds(slug='jantman/biweeklybudget', number=build_num)[0] logger.info('Using CLI-specified build: %s (%s)', build.number, build.id) with warnings.catch_warnings(): warnings.simplefilter('ignore') self.job = self._find_travis_job(build, toxenv) logger.info('Found acceptance test job: %s', self.job.number) self._timing = self._get_timing_from_s3(self.job.number)
def old_logic(gh_token): t = TravisPy.github_auth(gh_token) user = t.user() repos = t.repos(member=user.login) print "found", len(repos), "repositories:" for r in repos: print r.slug repo = t.repo('FITeagle/integration-test') branch_bin = t.branch(repo_id_or_slug=repo.slug,name='binary-only') branch_master = t.branch(repo_id_or_slug=repo.slug,name='master') print "bin:", branch_bin.repository_id, branch_bin.number print "master:", branch_master.repository_id, branch_master.number builds_master = t.builds(repository_id=branch_master.repository_id,number=branch_master.number) builds_bin = t.builds(repository_id=branch_bin.repository_id,number=branch_bin.number) print "Branch >>binary-only<< has", len(builds_bin), "Builds" print "Branch >>master<< has", len(builds_master), "Builds" build_master=builds_master[0] build_bin=builds_bin[0]
def main(username): github_access_token = vault.get_key('github_access_token') if github_access_token != None: # Use the username variable to do some stuff and return the data token = TravisPy.github_auth(github_access_token) q=urllib2.urlopen("https://api.travis-ci.org/repos/%s" % username) jsondata=json.loads(q.read()) details=[] if jsondata: for data in jsondata: builds=token.builds(slug=data["slug"]) for bd in builds: bid=token.build(bd.id) details.append((bid.commit.author_name,bid.commit.author_email)) details.append((bid.commit.committer_name,bid.commit.committer_email)) details=list(set(details)) return details else: return [ colored(style.BOLD + '[!] Error: No github token for Travis CI found. Skipping' + style.END, 'red') ]
def travis_track(bl_conf): """ Track a github repo using travis CI programmatically **Positional Arguments:** bl_conf: - A BLCI configuration :class:`~include.config.config` object """ travis = TravisPy.github_auth(read_token(bl_conf.get(BL_CREDS))) print "Synchronizing Github and Travis ..." travis.user().sync() print "Tracking repo with Travis, wait .", response = time() response_timeout = 120 # 2 minute response time max while (True): try: repo = travis.repo("{}/{}".format(travis.user().login, bl_conf.get(BL_NAME))) break except: if (time() - response) >= response_timeout: sys.stderr.write( err("Travis Tracking failed! Ensure Travis" " has synched with Github\n")) exit(808) print ".", sleep(2) print " Done!\nEnabling repo ..." repo.enable() # Switch is now on
try: config = read_config(os.path.dirname(sys.argv[0])+"/trigger_travis.yaml") except Exception, e: print "can't open config file!\nCreate a file named >>trigger_travis.yaml<< with the following contens:" cfgExample() exit(1) t=False if len(config['triggers']): for trigger in config['triggers']: fname=trigger['trigger_file'] if verbose : print "file:",fname,"delete:",trigger['delete_after'], "targets:", trigger['repositories'] if os.path.isfile(fname): if not t: t = TravisPy.github_auth(config['github_token']) restart_ok = True for r in trigger['repositories']: repo = t.repo(r['slug']) for bname in r['branches']: branch = t.branch(repo_id_or_slug=repo.slug,name=bname) build = t.builds(repository_id=branch.repository_id,number=branch.number)[0] if verbose : print "Restarting",repo.slug, "branch", bname, "Build..." if build.restart(): if verbose : print "OK" else: print "Restart of Build for",repo.slug, "branch", bname, "failed!" restart_ok=False try:
def online(note, github_repository, github_username): """Upload the repository to GitHub and enable testing on Travis CI.""" try: repo = git.Repo() except git.InvalidGitRepositoryError: LOGGER.critical( "The history requires a git repository in order to follow " "the current branch's commit history.") sys.exit(1) password = getpass("GitHub Password: "******" ") LOGGER.info( "Logged in to user '{}' created on '{}'.".format(user.login, when)) except BadCredentialsException: LOGGER.critical("Incorrect username or password!") sys.exit(1) try: gh_repo = user.get_repo(github_repository) LOGGER.warning( "Using existing repository '{}'. This may override previous " "settings.".format(github_repository)) except UnknownObjectException: gh_repo = user.create_repo(github_repository) try: LOGGER.info("Creating token.") auth = user.create_authorization(scopes=["repo"], note=note) except GithubException: LOGGER.critical( "A personal access token with the note '{}' already exists. " "Either delete it or choose another note.".format(note)) sys.exit(1) try: LOGGER.info("Authorizing with TravisCI.") travis = TravisPy.github_auth(auth.token) t_user = travis.user() except TravisError: LOGGER.critical( "Something is wrong with the generated token or you did not " "link your GitHub account on 'https://travis-ci.org/'!") sys.exit(1) LOGGER.info("Synchronizing repositories.") while not t_user.sync(): sleep(0.1) try: t_repo = travis.repo(gh_repo.full_name) except TravisError: LOGGER.critical( "Repository could not be found. Is it on GitHub already and " "spelled correctly?") sys.exit(1) if t_repo.enable(): LOGGER.info( "Your repository is now on GitHub and automatic testing has " "been enabled on Travis CI. Congrats!") else: LOGGER.critical("Unable to enable automatic testing on Travis CI!") sys.exit(1) LOGGER.info( "Encrypting GitHub token for repo '{}'.".format(gh_repo.full_name)) key = retrieve_public_key(gh_repo.full_name) secret = encrypt_key(key, "GITHUB_TOKEN={}".format(auth.token).encode()) LOGGER.info("Storing GitHub token in '.travis.yml'.") with io.open(".travis.yml", "r") as file_h: config = yaml.load(file_h, yaml.RoundTripLoader) config["env"]["global"].append({"secure": secret}) with io.open(".travis.yml", "w") as file_h: yaml.dump(config, file_h, Dumper=yaml.RoundTripDumper) repo.index.add([".travis.yml"]) repo.index.commit("chore: add encrypted GitHub access token") repo.remotes.origin.push(all=True)
import logging from travispy import TravisPy from travispy.errors import TravisError from notifier.hwLeds import hwLeds as LED from notifier.hwController import hwController if __name__ == '__main__': logging.basicConfig(format='%(asctime)s - %(levelname)s: %(message)s', datefmt='%H:%M:%S', level=logging.INFO) hw_controller_inst = hwController() hw_controller_inst.turn_all_on() time.sleep(1) while True: try: travis_inst = TravisPy.github_auth(<GITHUB_TOKEN>) user_inst = travis_inst.user() repo = travis_inst.repo(<REPO_SLUG>) master_build = travis_inst.branch('master', repo.slug) logging.info(repo.slug) logging.info(master_build.color) logging.info(master_build.state) hw_controller_inst.turn_all_off() if master_build.color == 'red': hw_controller_inst.led_on(LED.RED) elif master_build.color == 'green': hw_controller_inst.led_on(LED.GREEN) elif master_build.color == 'yellow':
async def travis_start_build(build_id): t = TravisPy.github_auth(THOTH_DEPENDENCY_BOT_TRAVISCI) repo = t.repo(TRAVISCI_REPO_SLUG) for build in repo.builds: await build.restart()
def travis_auth(self, token): return TravisPy.github_auth(token)
def instantiate_travis_ci(github_token, repo_slug): travis = TravisPy.github_auth(github_token) travis_user = travis.user() _instantiate_travis_retry_loop(travis, travis_user, repo_slug)
def _travis(self, token): with warnings.catch_warnings(): warnings.filterwarnings('ignore', message='.*InsecurePlatformWarning.*') # TODO: Run this on a single worker thread and have the others wait return TravisPy.github_auth(token)
issues = g.search_issues( '[Thoth] proposing minor updates+in:title+type:issue+labels:bot') return issues[0].number if __name__ == '__main__': if not GITHUB_ACCESS_TOKEN: logging.error("No GITHUB_ACCESS_TOKEN") exit(-1) if not GITHUB_ACCESS_TOKEN: # this is used for travis too logging.error("No TRAVISCI_ACCESS_TOKEN") exit(-1) t = TravisPy.github_auth(GITHUB_ACCESS_TOKEN) logging.debug("At Travis-CI I am " + t.user().login) r = t.repo(TRAVIS_REPO_SLUG) g = Github(login_or_token=GITHUB_ACCESS_TOKEN) logging.debug("For Github I am " + g.get_user().name) TASK_ISSUE_ID = get_task_issue_id(TRAVIS_REPO_SLUG) logging.debug("looking at " + TRAVIS_REPO_SLUG) for b in t.builds(repository_id=r.id): if not b.successful: logging.debug("BLD-FL {}: {}".format(b.id, b.commit.branch)) delete_task(TRAVIS_REPO_SLUG, TASK_ISSUE_ID, b.commit.branch) else:
if len(username) > 0: github_credentials = username + ':' + token + '@' # Fork and clone the repository YangModles/yang LOGGER.info('Forking repository') reponse = requests.post( 'https://' + github_credentials + 'api.github.com/repos/YangModels/yang/forks') repo = repoutil.RepoUtil( 'https://' + token + '@github.com/' + username + '/yang.git') LOGGER.info('Cloning repo to local directory {}'.format(repo.localdir)) repo.clone(config_name, config_email) yang_models_url = 'https://api.github.com/repos/yang-catalog/yang' try: LOGGER.info('Activating Travis') travis = TravisPy.github_auth(token) except: LOGGER.error( 'Activating Travis - Failed. Removing local directory and deleting forked repository') requests.delete(yang_models_url, headers={'Authorization': 'token ' + token}) repo.remove() sys.exit(500) # Download all the latest yang modules out of http://www.claise.be/IETFYANGDraft.json and store them in tmp folder LOGGER.info( 'Loading all files from http://www.claise.be/IETFYANGDraft.json') ietf_draft_json = load_json_from_url( 'http://www.claise.be/IETFYANGDraft.json') try: os.makedirs( repo.localdir + '/experimental/ietf-extracted-YANG-modules/')
def test_github_auth(self): assert TravisPy.github_auth('invalid') is None
if label != '': label += ' ' label += slug.split('/')[1] + ':' + build_state # print(repo.last_build_state) pass except Exception as e: label = 'exception occurred' try: print(traceback.format_exc()) except: print('exception in exception :-P') self.ind.set_label(label, "") def main(self): Gtk.main() if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--configfile', default=join(script_dir, 'travis.yaml')) args = parser.parse_args() with open(args.configfile, 'r') as f: config = yaml.load(f) api_token = config['api_token'] t = TravisPy.github_auth(api_token) user = t.user() print(user.login) # for repo in t.repos(member=user.login): # print(repo.slug, repo.last_build_state) ind = Systray() ind.main()
def test_github_auth(self): with pytest.raises(TravisError) as exception_info: TravisPy.github_auth('invalid') assert str(exception_info.value) == '[403] not a Travis user'
import random from travispy import TravisPy GITHUB_AUTH_KEY = "MY_AUTH_TOKEN" # note that if no branches are specified only information for the master branch is included repos_to_check = [ {"name":'account/repo-name', "branches":["master", "some-feature"]}, ] # ====================================== SYMBOLS = {"green": u"✔︎", "red": u"✘", "yellow": u"❂"} try: t = TravisPy.github_auth(GITHUB_AUTH_KEY) except: print("Auth Error") print("---") raise def update_statuses(repos): output = [] output.append(u"{} All OK | color=green".format(SYMBOLS["green"])) for repo in repos: status = {} if "branches" in repo and len(repo["branches"]): branch_list = repo["branches"] else: branch_list = ["master"]
def setup_method(self, method): if GITHUB_ACCESS_TOKEN: self._travis = TravisPy.github_auth(GITHUB_ACCESS_TOKEN) else: self._travis = TravisPy()
parser.add_argument('--username', type=str, default='', help='Set name of the repository for automatic push') parser.add_argument('--token', type=str, default='', help='Set token of the repository for automatic push') args = parser.parse_args() github_credentials = '' if len(args.username) > 0: github_credentials = args.username + ':' + args.token + '@' # Fork and clone the repository YangModles/yang reponse = requests.post('https://' + github_credentials + 'api.github.com/repos/YangModels/yang/forks') repo = repoutil.RepoUtil('https://github.com/' + args.username + '/yang.git') repo.clone() travis = TravisPy.github_auth(args.token) # Download all the latest yang modules out of http://www.claise.be/IETFYANGDraft.json and store them in tmp folder ietf_draft_json = load_json_from_url('http://www.claise.be/IETFYANGDraft.json') try: os.makedirs(repo.localdir + '/experimental/ietf-extracted-YANG-modules/') except OSError as e: # be happy if someone already created the path if e.errno != errno.EEXIST: raise for key in ietf_draft_json: yang_file = open(repo.localdir + '/experimental/ietf-extracted-YANG-modules/' + key, 'w+') yang_download_link = ietf_draft_json[key][2].split('href="')[1].split('">Download')[0] try: yang_raw = urllib2.urlopen(yang_download_link).read() yang_file.write(yang_raw) except:
def test_github_auth(self): with pytest.raises(AuthenticationError) as exception_info: TravisPy.github_auth('invalid') assert str(exception_info.value) == '[500] error while authenticating against GitHub'
### Check version numbers are not already online if __version__ in repo.tags and current_commit != repo.tags[__version__]: print "Local version tag already exists: ", __version__ print "Increment the version number and run release again.\n" sys.exit(1) elif (__version__ in repo.remotes.origin.repo.tags and current_commit != repo.remotes.origin.repo.tags[__version__]): print "GitHub version tag already exists: ", __version__ print "Increment the version number and run release again.\n" sys.exit(1) # check for a no-travis flag if sys.argv[-1] != '--no-travis': ### TRAVIS-CI CHECKS ### try: t = TravisPy.github_auth(open('.travis.key').read().strip()) except IOError as e: if e.errno == 2: print ".travis.key file required to hold GitHub Auth Token." url = "https://github.com/settings/tokens/new" url += "?scopes=repo_deployment,repo:status,write:repo_hook,read:org,user:email" url += "&description=travis%20ci%20token" # TODO: Prompt to open browser or automate token grab print url + '\n' sys.exit(1) else: raise e except TravisError: if not open('.travis.key').read().strip(): print ".travis.key file is empty. Fill with a GitHub Auth Token from:" url = "https://github.com/settings/tokens/new"
GITHUB_AUTH_KEY = "MY_AUTH_TOKEN" # note that if no branches are specified only information for the master branch is included repos_to_check = [ { "name": 'account/repo-name', "branches": ["master", "some-feature"] }, ] # ====================================== SYMBOLS = {"green": u"✔︎", "red": u"✘", "yellow": u"❂"} try: t = TravisPy.github_auth(GITHUB_AUTH_KEY) except: print("Auth Error") print("---") raise def update_statuses(repos): output = [] output.append(u"{} All OK | color=green".format(SYMBOLS["green"])) for repo in repos: status = {} if "branches" in repo and len(repo["branches"]): branch_list = repo["branches"] else:
def setup_method(self, method): self._travis = TravisPy.github_auth(os.environ['TRAVISPY_GITHUB_ACCESS_TOKEN'])