def Rebuild(request, pk): try: build = Build.objects.get(id=pk) except Build.DoesNotExist: raise Http404('Build does not exist') if not request.user.is_staff: g = get_github(request.user) grepo = g.get_repo(build.repo.full_name) if not grepo.full_name: raise Http404('Repo not found') guser = g.get_user(request.user.username) is_collab = grepo.has_in_collaborators(guser) if not is_collab: raise Http404('You are not a collaborator of this repo') Result.objects.filter(build=build).delete() auth = request.user.get_auth() build.enqueue(auth) return redirect(reverse('build_detail', kwargs={'pk': build.id}))
def get(self, request, *args, **kwargs): g = get_github(self.request.user) try: repos = [r for r in g.get_user().get_repos()] except BadCredentialsException: UserSocialAuth.objects.filter(user=request.user).delete() return redirect(reverse('social:begin', args=['github'])) + '?next=' + request.path self.object_list = Repo.objects.filter( full_name__in=[i.full_name for i in repos], disabled=False ).annotate(builds_count=Count('builds')) names = [x.full_name for x in self.object_list] filtered = [] for repo in repos: if repo.full_name not in names: filtered.append(repo) context = self.get_context_data() context['repos'] = filtered context['welcome'] = request.GET.get('welcome', False) return self.render_to_response(context)
def get(self, request, *args, **kwargs): g = get_github(self.request.user) try: repos = [r for r in g.get_user().get_repos()] except BadCredentialsException: UserSocialAuth.objects.filter(user=request.user).delete() return redirect(reverse('social:begin', args=['github'])) + '?next=' + request.path self.object_list = Repo.objects.filter( full_name__in=[i.full_name for i in repos], disabled=False).annotate(builds_count=Count('builds')) names = [x.full_name for x in self.object_list] filtered = [] for repo in repos: if repo.full_name not in names: filtered.append(repo) context = self.get_context_data() context['repos'] = filtered context['welcome'] = request.GET.get('welcome', False) return self.render_to_response(context)
def user_is_collaborator(self, user): if not user.is_authenticated(): return False if self.user == user or user.is_staff: return True g = get_github(user) grepo = g.get_repo(self.full_name) guser = g.get_user(user.username) return grepo.has_in_collaborators(guser)
def get_context_data(self, **kwargs): queryset = kwargs.pop('object_list', self.object_list) names = [x[0] for x in queryset.values_list('full_name')] # Get list of user repos g = get_github(self.request.user) repos = [r for r in g.get_user().get_repos()] filtered = [] for repo in repos: if repo.full_name not in names: filtered.append(repo) kwargs['repos'] = filtered return super(RepoListView, self).get_context_data(**kwargs)
def remove_webhook(self): if not settings.DEBUG: g = get_github(self.user) grepo = g.get_repo(self.full_name) try: hook = grepo.get_hook(self.webhook_id) hook.delete() except UnknownObjectException: pass self.webhook_id = None self.save()
def ProcessRepo(request, full_name): user = request.user g = get_github(request.user) grepo = g.get_repo(full_name) if not grepo.full_name: raise Http404('Repo not found') guser = g.get_user(user.username) is_collab = grepo.has_in_collaborators(guser) if not is_collab and grepo.private: raise Http404('You are not a collaborator of this repo') try: repo = Repo.objects.get(full_name=grepo.full_name) repo.disabled = False repo.is_private = grepo.private repo.save() except Repo.DoesNotExist: repo = Repo.objects.create( full_name=grepo.full_name, user=user, default_branch=grepo.default_branch, is_private=grepo.private ) if not repo.webhook_id: try: repo.add_webhook(request) except UnknownObjectException: raise Http404('Github failed to create a hook') # Lint all open branches auth = request.user.get_auth() for branch in grepo.get_branches(): build, created = Build.objects.get_or_create( repo=repo, ref=branch.name, sha=branch.commit.sha ) if created: build.enqueue(auth) url = reverse('repo_detail', kwargs={'full_name': repo.full_name}) return redirect(url)
def ProcessRepo(request, full_name): user = request.user g = get_github(user) grepo = g.get_repo(full_name) hook = grepo.create_hook( 'web', { 'content_type': 'json', 'url': request.build_absolute_uri(reverse('webhook')) }, events=['push'], active=True ) repo = Repo.objects.create(full_name=grepo.full_name, user=user, webhook_id=hook.id) return redirect(reverse('repo_list'))
def ProcessRepo(request, full_name): user = request.user g = get_github(request.user) grepo = g.get_repo(full_name) if not grepo.full_name: raise Http404('Repo not found') guser = g.get_user(user.username) is_collab = grepo.has_in_collaborators(guser) if not is_collab and grepo.private: raise Http404('You are not a collaborator of this repo') try: repo = Repo.objects.get(full_name=grepo.full_name) repo.disabled = False repo.is_private = grepo.private repo.save() except Repo.DoesNotExist: repo = Repo.objects.create(full_name=grepo.full_name, user=user, default_branch=grepo.default_branch, is_private=grepo.private) if not repo.webhook_id: try: repo.add_webhook(request) except UnknownObjectException: raise Http404('Github failed to create a hook') # Lint all open branches auth = request.user.get_auth() for branch in grepo.get_branches(): build, created = Build.objects.get_or_create(repo=repo, ref=branch.name, sha=branch.commit.sha) if created: build.enqueue(auth) url = reverse('repo_detail', kwargs={'full_name': repo.full_name}) return redirect(url)
def get(self, request, *args, **kwargs): self.object = self.get_object() context = self.get_context_data(object=self.object) is_collab = self.object.user_is_collaborator(request.user) context['is_owner'] = is_collab if self.object.is_private and not is_collab: raise Http404('You are not allowed to view this Repo') if is_collab: url = reverse('badge', kwargs={'full_name': self.object.full_name}) context['absolute_url'] = self.request.build_absolute_uri( self.request.path) context['badge_url'] = self.request.build_absolute_uri(url) g = get_github(self.object.user) grepo = g.get_repo(self.object.full_name) context['branches'] = [i.name for i in grepo.get_branches()] ref = request.GET.get('ref', False) context['ref'] = ref if ref: build_results = Build.objects.filter(repo=self.object, ref=ref) else: build_results = Build.objects.filter(repo=self.object) paginator = Paginator(build_results, 20) page = self.request.GET.get('page') try: context['builds'] = paginator.page(page) except PageNotAnInteger: context['builds'] = paginator.page(1) except EmptyPage: context['builds'] = paginator.page(paginator.num_pages) if paginator.num_pages > 1: context['pages'] = get_page_number_list(context['builds'].number, paginator.num_pages) context['num_objects'] = paginator.count return self.render_to_response(context)
def add_webhook(self, request): if settings.DEBUG: self.webhook_id = 123 else: g = get_github(self.user) grepo = g.get_repo(self.full_name) hook = grepo.create_hook( 'web', { 'content_type': 'json', 'url': request.build_absolute_uri(reverse('webhook')), 'secret': settings.WEBHOOK_SECRET }, events=['push'], active=True ) self.webhook_id = hook.id self.save()
def get(self, request, *args, **kwargs): self.object = self.get_object() context = self.get_context_data(object=self.object) is_collab = self.object.user_is_collaborator(request.user) context['is_owner'] = is_collab if self.object.is_private and not is_collab: raise Http404('You are not allowed to view this Repo') if is_collab: url = reverse('badge', kwargs={'full_name': self.object.full_name}) context['absolute_url'] = self.request.build_absolute_uri(self.request.path) context['badge_url'] = self.request.build_absolute_uri(url) g = get_github(self.object.user) grepo = g.get_repo(self.object.full_name) context['branches'] = [i.name for i in grepo.get_branches()] ref = request.GET.get('ref', False) context['ref'] = ref if ref: build_results = Build.objects.filter(repo=self.object, ref=ref) else: build_results = Build.objects.filter(repo=self.object) paginator = Paginator(build_results, 20) page = self.request.GET.get('page') try: context['builds'] = paginator.page(page) except PageNotAnInteger: context['builds'] = paginator.page(1) except EmptyPage: context['builds'] = paginator.page(paginator.num_pages) if paginator.num_pages > 1: context['pages'] = get_page_number_list(context['builds'].number, paginator.num_pages) context['num_objects'] = paginator.count return self.render_to_response(context)
def delete(self, using=None, keep_parents=False): g = get_github(self.user) hook = g.get_repo(self.full_name).get_hook(self.webhook_id) hook.delete() super(Repo, self).delete(using=using, keep_parents=keep_parents)
def get_issues(self): g = get_github(self.repo.user) issues = g.search_issues('%s+repo:%s' % (self.short_sha, self.repo.full_name)) return issues