示例#1
0
def trigger_build(repo, branch):
    """ Sends a request to Travis, rebuilding the content
    """
    if not app.config["TRAVIS_REPO_SLUG"] or not app.config["TRAVIS_TOKEN"]:
        return

    t = TravisPy(app.config['TRAVIS_TOKEN'])

    # it doesn't make sense for multiple builds of the same branch to run at the same time
    # so if some are still running for our target branch, lets stop them
    for build in t.builds(slug=app.config["TRAVIS_REPO_SLUG"]):
        if not build.pull_request and build.pending and build.commit.branch == app.config[
                "NAUCSE_BRANCH"]:
            build.cancel()

    # unfortunately, TravisPy doesn't provide a method to trigger a build, so triggering manually:
    requests.post("https://api.travis-ci.org/repo/{}/requests".format(
        urllib.parse.quote_plus(app.config["TRAVIS_REPO_SLUG"])),
                  json={
                      "request": {
                          "branch": app.config["NAUCSE_BRANCH"],
                          "message": f"Triggered by {repo}/{branch}"
                      }
                  },
                  headers={
                      "Authorization": f"token {app.config['TRAVIS_TOKEN']}",
                      "Travis-API-Version": "3"
                  })
示例#2
0
class Source():
    def __init__(self, username, uri=None):
        kwargs = {}
        if uri:  # leave as unspecified to use default
            kwargs['uri'] = uri
        self.client = TravisPy(**kwargs)
        self.username = username
        self.logger = logger.Logger('travis_ci')

    def list_projects(self):
        repos = self.client.repos(owner_name=self.username)
        repos = filter(lambda x: x.active == True, repos)
        return list(map(lambda x: x.slug.split('/')[1], repos))

    def project_status(self, project, branch='master'):
        try:
            state = self.client.repo(self.username + '/' +
                                     project).last_build_state
        except Exception, e:
            self.logger.log("Error while computing state for project '%s': %s",
                            project, str(e))
            return STATUS.POLL_ERROR

        if not state:
            return STATUS.UNKNOWN

        return _STATUS[state]
示例#3
0
    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)
示例#4
0
def runit(days, tokenfile, project,debug):
    if not os.path.exists("logs"):
        os.mkdir("logs")

    if debug:
        do_debug()
    
    monthcut = (datetime.datetime.now() - datetime.timedelta(days=days)).replace(
        tzinfo=datetime.timezone.utc)
    token = open(tokenfile).read().strip()
    t = TravisPy(token)
    user = t.user()
    repos = user.repos()
    respository_id = None
    for x in repos['repositories']:
        print(x)
        print(x['name'])
        #print(dir(x))
        slug =x['slug']
        print (slug, x['id'])
        repo_id = x['id']

        
        ppath = "logs/" + slug
        if not os.path.exists(ppath):
            os.makedirs(ppath)

        if project == "":
            do_project(t, repo_id,monthcut, slug)
        elif project == x.slug:
            do_project(t, repo_id,monthcut, slug)
            return
示例#5
0
 def __init__(self, username, uri=None):
     kwargs = {}
     if uri:  # leave as unspecified to use default
         kwargs['uri'] = uri
     self.client = TravisPy(**kwargs)
     self.username = username
     self.logger = logger.Logger('travis_ci')
示例#6
0
    def on_message(self, headers, message):
        logging.warning("New build id arrived: %s" % message)
        # Remove output file after each run to avoid filling up the container.
        self.conn.ack(headers.get('message-id'), headers.get('subscription'))
        os.system("rm -rf clonedrepo")
        # Fetch slug and branch name from travis given buildid.
        t = TravisPy()
        build = t.build(message)
        repoid = build['repository_id']
        repobranch = build.commit.branch
        repo = t.repo(repoid)
        reposlug = repo.slug
        # Clone the repo branch
        token = os.getenv('GITHUB_OAUTH') or ''
        url = 'https://github.com/' + reposlug + '.git'
        repo = None
        branch_exist = True
        logging.warning('Cloning url ' + url)

        try:
            repo = git.Repo.clone_from(url, 'clonedrepo', branch=repobranch)
        except:
            logging.warning("Branch " + str(repobranch) + " or the repo " +
                            str(reposlug) + " itself does not exist anymore")
            branch_exist = False

        # Check if project support dspot otherwise try autoconfiguring
        # assuming that dspot configurations is in the top most directory of
        # the project
        POM_FILE = "clonedrepo/pom.xml"
        timecap = '-{date:%Y-%m-%d-%H-%M-%S}'.format(
            date=datetime.datetime.now())

        if branch_exist:
            if not run_Dspot_preconfig(POM_FILE, reposlug, timecap):
                logging.warning("PROJECT DOES NOT SUPPORT DSPOT")
                run_Dspot_autoconfig(reposlug, timecap)

            # Commit build to github
            if check_endprocess_gitcommit(repo):
                templist = os.getenv("PUSH_URL").split('//')
                pushurl = templist[0] + '//' + token + '@' + templist[1]
                branch_name = reposlug.replace('/', "-") + timecap

                logging.warning('Commit to git as new branch with name ' +
                                branch_name)

                current = repo.git.checkout('-b', branch_name)
                # current.checkout()
                time.sleep(10)
                repo.git.add(A=True)
                repo.git.commit(m='update from dspot')
                repo.git.push(pushurl, branch_name)
                logging.warning("GITHUB NAME: " + os.getenv("GITHUB_USERNAME"))
            logging.warning(
                "PIPELINE MESSAGE: DONE , AWAITING FOR NEW BUILD ID")
示例#7
0
    def on_message(self, headers, message):
        logging.warn("New build id arrived: %s" % message)
        # Remove output file after each run to avoid filling up the container.
        self.conn.ack(headers.get('message-id'), headers.get('subscription'))
        os.system("rm -rf clonedrepo")
        # Fetch slug and branch name from travis given buildid.
        reposlug = ""
        repobranch = ""
        selector = ""
        if not SLUG_MODE:
            t = TravisPy()
            build = t.build(message)
            repoid = build['repository_id']
            repobranch = build.commit.branch
            repo = t.repo(repoid)
            reposlug = repo.slug

        else:
            # Expect message format 'selector,branchname,selector'
            strList = message.split(",")
            logging.warn(message)
            logging.warn(strList)
            reposlug = strList[0]
            repobranch = strList[1]
            selector = strList[2]

        # Clone the repo branch
        token = os.getenv('GITHUB_OAUTH') or ''
        url = 'https://:@github.com/' + reposlug + '.git'
        repo = None
        branch_exist = True
        logging.warn('Cloning url ' + url)

        try:
            repo = git.Repo.clone_from(url, 'clonedrepo', branch=repobranch)
        except:
            logging.warn("Branch " + str(repobranch) + " or the repo " +
                         str(reposlug) + " itself does not exist anymore")
            branch_exist = False
            report_error(
                "Invalid repo or branch, double check if the input repo url is correct \n \n --STAMP/Dspot",
                "", reposlug, repobranch)

        # Check if project support dspot otherwise try autoconfiguring
        # assuming that dspot configurations is in the top most directory of
        # the project
        timecap = '-{date:%Y-%m-%d-%H-%M-%S}'.format(
            date=datetime.datetime.now())
        if branch_exist:
            if not run_Dspot_preconfig(reposlug, repobranch, selector):
                logging.warn(
                    "PROJECT DOES NOT SUPPORT DSPOT, COMMENCING AUTOCONFIGURING"
                )
                run_Dspot_autoconfig(reposlug, repobranch, selector)

            logging.warn("PIPELINE MESSAGE: DONE , AWAITING FOR NEW BUILD ID")
示例#8
0
def get_travis_session(username, travis_ci_token, github_token):
  travis_session = TravisPy(token=travis_ci_token)
  try:
    travis_session.repo('some_repo')
  except TravisError:
    logger.error("Travis session expired for {}. Please manually generate it by doing:\n{}"
                 .format(username, TOKEN_INSTRUCTION.format(github_token)))
    exit(1)
  else:
    return travis_session
示例#9
0
def stop_all_builds():
  for username, repo_name, github_token, travis_ci_token in CREDS:
    travis_session = TravisPy(token=travis_ci_token)
    builds = travis_session.builds(slug="{}/{}".format(username, repo_name))
    for build in builds:
      if not build.finished:
        success = build.cancel()
        url = calculate_build_url(username, repo_name, build)
        if success:
          logger.info("Build {} aborted".format(url))
        else:
          logger.error("Build {} fails to abort".format(url))
示例#10
0
    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)
示例#11
0
文件: app.py 项目: btxlzh/peergrader
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 __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)
示例#13
0
 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)
示例#14
0
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)
示例#15
0
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
示例#16
0
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
示例#17
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')
        ]
示例#18
0
    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 _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('')
示例#20
0
 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)
示例#21
0
def main():
    travis = TravisPy()
    revision = check_output(["git", "rev-parse", "HEAD"]).strip()
    build_passed = False
    for build in travis.builds(slug="datawire/mdk"):
        if build.commit.sha == revision:
            if build.passed:
                build_passed = True
                break
            else:
                error("Found the build but it has not passed.\n    Build state: "
                      + build.state +
                      "\n    Build URL: https://travis-ci.org/datawire/mdk/builds/"
                      + str(build.id))

    if not build_passed:
        error("No matching build found on Travis CI.")
示例#22
0
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])
示例#23
0
文件: views.py 项目: eaudeweb/drogo
 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)
示例#24
0
def main():
    travis = TravisPy()
    revision = check_output(["git", "rev-parse", "HEAD"]).strip()
    build_passed = False
    for build in travis.builds(slug="datawire/mdk"):
        if build.commit.sha == revision:
            if build.passed:
                build_passed = True
                break
            else:
                error(
                    "Found the build but it has not passed.\n    Build state: "
                    + build.state +
                    "\n    Build URL: https://travis-ci.org/datawire/mdk/builds/"
                    + str(build.id))

    if not build_passed:
        error("No matching build found on Travis CI.")
示例#25
0
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 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 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()
示例#28
0
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
示例#29
0
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
示例#30
0
文件: app.py 项目: btxlzh/peergrader
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
示例#31
0
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
示例#32
0
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
示例#33
0
文件: tronci.py 项目: runt18/tron-ci
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'])
示例#34
0
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/'}
示例#35
0
文件: love.py 项目: takluyver/Love
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
示例#36
0
    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()
示例#37
0
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)
示例#38
0
 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))
示例#39
0
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)
示例#40
0
 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
示例#41
0
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
示例#42
0
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
示例#43
0
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
示例#44
0
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
示例#45
0
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') ]
示例#47
0
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()
示例#48
0
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"]
示例#49
0
 def test_github_auth(self):
     assert TravisPy.github_auth('invalid') is None
示例#50
0
 def travis_auth(self, token): 
     return TravisPy.github_auth(token)
示例#51
0
 def setup_method(self, method):
     self._travis = TravisPy.github_auth(os.environ['TRAVISPY_GITHUB_ACCESS_TOKEN'])
示例#52
0
    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:
示例#53
0
 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'
示例#54
0
def build_passed(repo):
    t = TravisPy()
    r = t.repo(repo)
    while True:
        yield r.last_build_state == 'passed'
示例#55
0
def build_passed(repo='RPi-Distro/python-gpiozero', delay=3600):
    t = TravisPy()
    r = t.repo(repo)
    while True:
        yield r.last_build_state == 'passed'
        sleep(delay) # Sleep an hour before hitting travis again
示例#56
0
 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'
示例#57
0
	'grosser/parallel',
	'wvanbergen/request-log-analyzer',
	'troessner/reek',
	'ruboto/ruboto',
	'markevans/dragonfly',
	'grails/grails-core',
	'grosser/parallel_tests',
	'mongodb/mongo-python-driver',
	'scambra/devise_invitable',
	'dennisreimann/ioctocat',
	'cython/cython',
	'mongomapper/mongomapper',
	'publify/publify'	
]

t = TravisPy()

builds_result = []

for project in projects:

	builds = t.builds(slug=project, event_type='pull_request')

	labels = ['project', 'pull_number', 'started_at', 'finished_at', 'branch', 'build_status']
	count = 1

	print ' {:_<6} {:_<26} {:_^12} {:_^8} {:_^8} '.format('','','','','')
	print '|{:^6}|{:^26}|{:^12}|{:^8}|{:^8}|'.format('#','project','pull_number', 'branch', 'status')

	while builds:
		for build in builds:
示例#58
0
 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)
示例#59
0
文件: release.py 项目: HANNATH/vsm
### 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"
示例#60
0
 def test(self):
   with self.assertRaises(TravisError):
     travis = TravisPy(token='invalid_token')
     travis.repo('some_repo')