def get_repos(self, owner, no_cache=False): cache_key = 'gh:3:repos:{}:{}:{}'.format( md5(self.client.token.encode('utf')).hexdigest(), md5(b','.join(s.encode('utf') for s in self.scopes)).hexdigest(), md5(owner.encode('utf-8')).hexdigest() if owner else '', ) if no_cache: result = None else: result = redis.get(cache_key) if result is None: # TODO(dcramer): paginate if not owner: endpoint = '/user/repos' params = {'type': 'owner'} else: endpoint = '/orgs/{}/repos'.format(owner) params = {} result = [] has_results = True while has_results and endpoint: response = self.client.get(endpoint, params=params) result.extend([{ 'id': r['id'], 'full_name': r['full_name'], 'admin': r['permissions'].get('admin', False), } for r in response]) has_results = bool(response) if has_results: endpoint = response.rel.get('next') redis.setex(cache_key, json.dumps(result), ONE_DAY) else: result = json.loads(result) return sorted(result, key=lambda x: x['full_name'])
def get_travis_public_key(domain) -> str: cache_key = "travis:public-key:{}".format(domain) public_key = redis.get(cache_key) if public_key is None: resp = requests.get("https://{}/config".format(domain), headers={"User-Agent": USER_AGENT}) resp.raise_for_status() public_key = resp.json( )["config"]["notifications"]["webhook"]["public_key"].encode("utf-8") redis.setex(cache_key, 300, public_key) return serialization.load_pem_public_key(public_key, backend=default_backend())
def get_travis_public_key(domain) -> str: cache_key = 'travis:public-key:{}'.format(domain) public_key = redis.get(cache_key) if public_key is None: resp = requests.get( 'https://{}/config'.format(domain), headers={'User-Agent': USER_AGENT}, ) resp.raise_for_status() public_key = resp.json()[ 'config']['notifications']['webhook']['public_key'].encode('utf-8') redis.setex(cache_key, public_key, 300) return serialization.load_pem_public_key(public_key, backend=default_backend())
def get_repos(self, owner, no_cache=False): cache_key = "gh:{}:repos:{}:{}:{}".format( self.version, md5(self.client.token.encode("utf")).hexdigest(), md5(b",".join(s.encode("utf") for s in self.scopes)).hexdigest(), md5(owner.encode("utf-8")).hexdigest() if owner else "", ) if no_cache: result = None else: result = redis.get(cache_key) if result is None: # TODO(dcramer): paginate if not owner: endpoint = "/user/repos" params = {"type": "owner"} else: endpoint = "/orgs/{}/repos".format(owner) params = {} result = [] has_results = True while has_results and endpoint: response = self.client.get(endpoint, params=params) result.extend([{ "id": r["id"], "ssh_url": r["ssh_url"], "full_name": r["full_name"], "permission": Permission.admin if r["permissions"].get("admin", False) else Permission.read, } for r in response]) has_results = bool(response) if has_results: endpoint = response.rel.get("next") redis.setex(cache_key, json.dumps(result), ONE_DAY) else: result = json.loads(result) for i in result: # we need to coerce permission back into our Permission enum i["permission"] = Permission(i["permission"]) return sorted(result, key=lambda x: x["full_name"])
def get(self, repo: Repository): """ Return a list of revisions for the given repository. """ cache_key = self.cache_key.format(repo_id=repo.id.hex) result = redis.get(cache_key) if result is None: vcs = repo.get_vcs() if not vcs: return self.respond([]) vcs.ensure() result = vcs.get_known_branches() redis.setex(cache_key, json.dumps(result), self.cache_expire) else: result = json.loads(result) return self.respond([{"name": r} for r in result])
def get_repos(self, owner, no_cache=False): cache_key = 'gh:0:repos:{}'.format( md5(owner.encode('utf-8')).hexdigest() if owner else '') if no_cache: result = None else: result = redis.get(cache_key) if result is None: # TODO(dcramer): paginate if not owner: response = self.client.get('/user/repos', params={'type': 'owner'}) else: response = self.client.get('/orgs/{}/repos'.format(owner)) result = [{ 'id': r['id'], 'full_name': r['full_name'], } for r in response] redis.setex(cache_key, json.dumps(result), ONE_DAY) else: result = json.loads(result) return result