def create(self, request): user = request.user if not user: raise AccessError from_proj = request.get_form_var('from_proj') from_ref = request.get_form_var('from_ref') to_ref = request.get_form_var('to_ref') to_proj = request.get_form_var('to_proj') title = request.get_form_var('title', '').decode('utf-8') comment = request.get_form_var('body', '').decode('utf-8') if not all([from_ref, from_proj, to_ref, to_proj]): raise TraversalError from_proj = CodeDoubanProject.get_by_name(from_proj) to_proj = CodeDoubanProject.get_by_name(to_proj) if from_proj != to_proj: if not from_proj.has_push_perm(user.name): raise AccessError( "Need push permission to create PR on another project") pullreq = PullRequest.open(from_proj, from_ref, to_proj, to_ref) ticket = Ticket(None, None, to_proj.id, title, comment, user.username, None, None) pullreq = add_pull(ticket, pullreq, user) ticket = pullreq.ticket return request.redirect( str('/%s/pull/%s/' % (to_proj.name, ticket.ticket_id)))
def create(self, request): user = request.user if not user: raise AccessError from_proj = request.get_form_var('from_proj') from_ref = request.get_form_var('from_ref') to_ref = request.get_form_var('to_ref') to_proj = request.get_form_var('to_proj') title = request.get_form_var('title', '').decode('utf-8') comment = request.get_form_var('body', '').decode('utf-8') if not all([from_ref, from_proj, to_ref, to_proj]): raise TraversalError from_proj = CodeDoubanProject.get_by_name(from_proj) to_proj = CodeDoubanProject.get_by_name(to_proj) if from_proj != to_proj: if not from_proj.has_push_perm(user.name): raise AccessError( "Need push permission to create PR on another project") pullreq = PullRequest.open(from_proj, from_ref, to_proj, to_ref) ticket = Ticket(None, None, to_proj.id, title, comment, user.username, None, None) pullreq = add_pull(ticket, pullreq, user) ticket = pullreq.ticket return request.redirect(str('/%s/pull/%s/' % (to_proj.name, ticket.ticket_id)))
def test_get_forked_from_should_return_origin_project(self): prj = CodeDoubanProject.get_by_name('orig') prj.delete() prj = CodeDoubanProject.get_by_name('fork') prj.delete() orig = CodeDoubanProject.add('orig', owner_id="test1") fork = orig.fork('fork', 'user2') source = fork.get_forked_from() eq_(source, orig)
def test_transfer_project(self): pname1 = 'project6' pname2 = 'project7' proj_owner = 'admin_user' to_user = '******' p = CodeDoubanProject.add(pname1, owner_id=proj_owner, summary="test", product="fire") _ = CodeDoubanProject.add(pname2, owner_id=proj_owner, summary="test", product="fire") p.transfer_to(to_user) p1 = CodeDoubanProject.get_by_name(pname1) assert p1.owner_id == to_user p2 = CodeDoubanProject.get_by_name(pname2) assert p2.owner_id == proj_owner
def test_update_can_push(self): project_name = "project11" owner_id = "testuser" summary = "a summary" product = "fire" CodeDoubanProject.add(project_name, owner_id, summary, product) p = CodeDoubanProject.get_by_name('project11') assert p.can_push == 1 p.update_can_push(False) p = CodeDoubanProject.get_by_name('project11') assert p.can_push == 0 p.update_can_push(True) p = CodeDoubanProject.get_by_name('project11') assert p.can_push == 1
def _q_index(self, request): group = self.group project = request.get_form_var('project') p = CodeDoubanProject.get_by_name(project) if project else None if project and p: group.add_project(project_id=p.id) return request.redirect(group.url)
def test_project_meta_dict(self): project_name = "project3" owner_id = "testuser" summary = "a summary" product = "fire" project = CodeDoubanProject.add( project_name, owner_id, summary, product) # hookfile_path = "%s/hooks/post-receive" % project.git_real_path project = CodeDoubanProject.get_by_name(project_name) data = { 'url': "%s/%s" % (DOMAIN, project_name), 'name': project_name, 'description': summary, 'product': product, 'committers_count': 0, 'forked_count': 0, 'open_issues_count': 0, 'open_tickets_count': 0, 'watched_count': 0, 'owner': { 'name': owner_id, 'avatar': gravatar_url(owner_id + '@douban.com'), }, } commits = project.git.get_revisions("HEAD~1", "HEAD") if commits: data['last_commit'] = commits[0] ok_(project.get_info() == data) project.delete()
def push_data_receiver(sender, **kw): EMPTY_COMMIT_HASH = "0" * 40 date = datetime.now() repo_name = kw.get('repo_name') before = kw.get('before') after = kw.get('after') username = kw.get('username') data = { 'repo_name': repo_name, 'repo_url': kw.get('repo_url'), 'ref': kw.get('ref'), 'branch': kw.get('branch'), 'date': date, 'before': before, 'after': after, 'is_new_branch': before == EMPTY_COMMIT_HASH, 'is_delete_branch': after == EMPTY_COMMIT_HASH, 'commits': kw.get('commits'), 'type': 'push', 'username': username, } feeds = get_related_feeds( '', project=CodeDoubanProject.get_by_name(repo_name)) for feed in feeds: feed.add_action(data)
def _get_tmpl_raw(tmpl_target, rev, path, project_name, request): project = CodeDoubanProject.get_by_name(project_name) if rev is None: rev = project.default_branch try: blob = project.repo.get_file(rev, path.decode("utf-8")) except IOError: raise TraversalError() if not blob: raise TraversalError("No content found") resp = request.response if is_image(path): if path.endswith("svg"): resp.set_header("Content-Type", "image/svg+xml") else: resp.set_header("Content-Type", "image/jpeg") resp.set_header("Expires", "Sun, 1 Jan 2006 01:00:00 GMT") resp.set_header("Pragma", "no-cache") resp.set_header("Cache-Control", "must-revalidate, no-cache, private") return blob.data if path.endswith(".pdf"): resp.set_header("Content-Type", "application/pdf") return blob.data if is_binary(path): resp.set_header("Content-Type", "application/octet-stream") resp.set_header("Content-Disposition", "attachment;filename=%s" % path.split("/")[-1]) resp.set_header("Content-Transfer-Encoding", "binary") return blob.data resp.set_header("Content-Type", "text/plain;charset=utf-8") return blob.data.encode("utf8")
def test_project_meta_dict(self): project_name = "project3" owner_id = "testuser" summary = "a summary" product = "fire" project = CodeDoubanProject.add(project_name, owner_id, summary, product) # hookfile_path = "%s/hooks/post-receive" % project.git_real_path project = CodeDoubanProject.get_by_name(project_name) data = { 'url': "%s/%s" % (DOMAIN, project_name), 'name': project_name, 'description': summary, 'product': product, 'committers_count': 0, 'forked_count': 0, 'open_issues_count': 0, 'open_tickets_count': 0, 'watched_count': 0, 'owner': { 'name': owner_id, 'avatar': gravatar_url(owner_id + '@douban.com'), }, } commits = project.git.get_revisions("HEAD~1", "HEAD") if commits: data['last_commit'] = commits[0] ok_(project.get_info() == data) project.delete()
def create(self, request): if request.method == 'POST': user = request.user if not user: raise AccessError project = request.get_form_var('project') title = request.get_form_var('title', '').decode('utf-8') description = request.get_form_var('body', '').decode('utf-8') tags = request.get_form_var('issue_tags', []) if isinstance(tags, list): tags = [tag.decode('utf-8') for tag in tags if tag] elif isinstance(tags, basestring): tags = [tags.decode('utf-8')] if not project: raise TraversalError if not title.strip(): return request.redirect('/%s/issues/new?error=empty' % project) project = CodeDoubanProject.get_by_name(project) pissue = ProjectIssue.add(title, description, user.name, project=project.id) pissue.add_tags(tags, pissue.project_id) # TODO: 重构feed后取消信号发送 issue_signal.send(author=user.name, content=description, issue_id=pissue.issue_id) dispatch('issue', data={ 'sender': user.name, 'content': description, 'issue': pissue }) return request.redirect(pissue.url) project_name = self.proj_name return request.redirect('/%s/issues' % project_name)
def has_permission_handler(username, path, perm): if not username or not path: return False if path.endswith('.git'): path = path[:-4] # gist if path.startswith('gist/'): gist_id = path.rpartition("/")[-1] if username == Gist.get(gist_id).owner_id: return True return False # project project = Project.get_by_name(path) if not project: return False if perm == 'read': return True if not project.can_push: return False if project.has_push_perm(username): return True return False
def push_data_receiver(sender, **kw): EMPTY_COMMIT_HASH = "0" * 40 date = datetime.now() repo_name = kw.get('repo_name') before = kw.get('before') after = kw.get('after') username = kw.get('username') data = { 'repo_name': repo_name, 'repo_url': kw.get('repo_url'), 'ref': kw.get('ref'), 'branch': kw.get('branch'), 'date': date, 'before': before, 'after': after, 'is_new_branch': before == EMPTY_COMMIT_HASH, 'is_delete_branch': after == EMPTY_COMMIT_HASH, 'commits': kw.get('commits'), 'type': 'push', 'username': username, } feeds = get_related_feeds('', project=CodeDoubanProject.get_by_name(repo_name)) for feed in feeds: feed.add_action(data)
def destroy(self, request): group = self.group project = request.get_form_var('project') p = CodeDoubanProject.get_by_name(project) if project else None if project and p: group.remove_project(project_id=p.id) return request.redirect(group.url)
def delete_project(names): if isinstance(names, basestring): names = [names] for n in names: prj = CodeDoubanProject.get_by_name(n) if prj: prj.delete()
def count(self, request): request.response.set_content_type('application/json; charset=utf8') q = request.get_form_var('q', '') project = CodeDoubanProject.get_by_name(self.project) \ if self.project else None state = request.get_form_var('state', '') language = request.get_form_var('language', '') doctype = request.get_form_var('doctype', '') counts = {} for kind, cls in KIND_CLASS_MAP.iteritems(): kwargs = dict(phrase=q, from_=0, size=0) if project and kind in (K_DOC, K_CODE, K_PULL, K_ISSUE): kwargs.update(project_id=project.id) if language and kind == K_CODE: kwargs.update(language=language) if state and kind in (K_PULL, K_ISSUE): kwargs.update(state=state) if doctype and kind == K_DOC: kwargs.update(doctype=doctype) result = cls.search_a_phrase(**kwargs) counts[kind] = SearchEngine.get_count(result) tdt = { 'q': q, 'repos': counts[K_REPO], 'codes': counts[K_CODE], 'users': counts[K_USER], 'docs': counts[K_DOC], 'pulls': counts[K_PULL], 'issues': counts[K_ISSUE], } return json.dumps(tdt)
def _get_tmpl_raw(tmpl_target, rev, path, project_name, request): project = CodeDoubanProject.get_by_name(project_name) if rev is None: rev = project.default_branch try: blob = project.repo.get_file(rev, path.decode('utf-8')) except IOError: raise TraversalError() if not blob: raise TraversalError("No content found") resp = request.response if is_image(path): if path.endswith('svg'): resp.set_header("Content-Type", "image/svg+xml") else: resp.set_header("Content-Type", "image/jpeg") resp.set_header('Expires', 'Sun, 1 Jan 2006 01:00:00 GMT') resp.set_header('Pragma', 'no-cache') resp.set_header('Cache-Control', 'must-revalidate, no-cache, private') return blob.data if path.endswith('.pdf'): resp.set_header("Content-Type", "application/pdf") return blob.data if is_binary(path): resp.set_header("Content-Type", "application/octet-stream") resp.set_header("Content-Disposition", "attachment;filename=%s" % path.split('/')[-1]) resp.set_header("Content-Transfer-Encoding", "binary") return blob.data resp.set_header("Content-Type", "text/plain;charset=utf-8") return blob.data.encode('utf8')
def _tmpl_common_data(rev, path, project_name, request): project = CodeDoubanProject.get_by_name(project_name) user = request.user ref = rev if ref is None: ref = project.default_branch branches = project.repo.branches tags = project.repo.tags ref_type = 'branch' if ref in branches else 'tag' \ if ref in tags else 'tree' blob_path = path.decode('utf-8') tdt = { 'errors': '', 'branches': branches, 'rev': rev, 'tags': tags, 'blob_path': blob_path, 'file_name': blob_path.rpartition('/')[-1], 'request': request, 'project': project, 'project_name': project_name, 'path': path, 'ref_type': ref_type, 'blob_ref': ref, 'ref': ref, 'user': user, } return tdt
def has_permission_handler(username, path, perm): if not username or not path: return False if path.endswith(".git"): path = path[:-4] # gist if path.startswith("gist/"): gist_id = path.rpartition("/")[-1] if username == Gist.get(gist_id).owner_id: return True return False # project project = Project.get_by_name(path) if not project: return False if perm == "read": return True if not project.can_push: return False if project.has_push_perm(username): return True return False
def get_by_proj_name_and_number(cls, proj_name, number): from vilya.models.project import CodeDoubanProject project = CodeDoubanProject.get_by_name(proj_name) project_issue = cls.get(project.id, number=number) issue_id = project_issue.issue_id return Issue.get_cached_issue(issue_id)
def destroy(self, request): group = self.group project = request.get_form_var("project") p = CodeDoubanProject.get_by_name(project) if project else None if project and p: group.remove_project(project_id=p.id) return request.redirect(group.url)
def _q_index(self, request): project_name = self.proj_name project = CodeDoubanProject.get_by_name(project_name) if not project: raise TraversalError() tickets = CodeDoubanTicketBoard( self.proj_name).tickets_group_by_status() return st('ticket_board.html', **locals())
def _q_index(self, request): user = request.user tdt = { 'user': user, 'project': CodeDoubanProject.get_by_name(self.proj_name), 'request': request, } return st('settings/config.html', **tdt)
def __init__(self, proj_name): self.proj_name = proj_name self.project = CodeDoubanProject.get_by_name(self.proj_name) self._index = self._filter('repo') self._created_by = self._filter('created_by') self._assigned = self._filter('assigned') self._search = self._filter('search')
def check_permission(self, request, proj_name): project = CodeDoubanProject.get_by_name(proj_name) user = request.user if not project or \ not user and project.intern_banned or \ user and user.is_intern and project.intern_banned \ and not project.is_admin(user.username): return request.redirect(User.create_login_url(request.url))
def _q_index(self, request): errors = '' project_name = self.proj_name user = request.user project = CodeDoubanProject.get_by_name(project_name) if not project: raise TraversalError() data = project.git.get_gitstats_data() return st('graph.html', **locals())
def _q_lookup(self, request, rev): name = 'shire_git_RO' project = CodeDoubanProject.get_by_name(name) repo = project.repo query = 'trunk@' if rev == 'HEAD' else "trunk@%s " % rev commits = repo.get_commits("HEAD", query=query, max_count=1) commit = commits[0] if commits else None if commit: return request.redirect(commit.url)
def __init__(self, proj_name, issue_number): self.target = CodeDoubanProject.get_by_name(proj_name) self.issue_number = issue_number project_issue = ProjectIssue.get(self.target.id, number=self.issue_number) self.issue_id = project_issue.issue_id self.issue = Issue.get_cached_issue(self.issue_id) self.issue_template = 'issue/issue.html'
def _get_tmpl_edit(tmpl_target, rev, path, project_name, request): rgf = request.get_form_var tdt = _tmpl_common_data(rev, path, project_name, request) project = CodeDoubanProject.get_by_name(project_name) errors = "" success = "" ref = rev user = request.user direct_edit_allowed = True if not user: direct_edit_allowed = False elif not project.has_push_perm(user.name): direct_edit_allowed = False if ref is None: ref = project.default_branch if request.method == "POST": source = rgf("code") message = rgf("message") desc = rgf("desc") if message == "": errors = u"commit summary不能为空" if rgf("newfile") and not re.match("^[a-zA-Z_\-0-9.]+$", rgf("newfilename", "")): errors = "new filename incorrect: (%s)" % rgf("newfilename") if not errors: if rgf("newfile"): commit_fn = os.path.join(path, rgf("newfilename")) assert not project.repo.get_commit("%s:%s" % (ref, commit_fn)), "file_already_exist" else: commit_fn = rgf("filename") message = "%s\n\n%s" % (message, desc) message = message.decode("utf8") data = [] data.append([commit_fn, source, "insert"]) if direct_edit_allowed: reflog = "commit_one_file on %s" % ref assert ( ref == "master" or ref in project.repo.branches ), "commit online allowed on existing branches only" try: project.repo.commit_file(ref, ref, user.name, user.email, message, reflog, data) success = u"提交成功" except AssertionError, err: errors = ( "Error updating file %s: REPO COULD BE CORRUPTED, FETCH IT MANUALLY AND CHECK" % err ) # noqa redir = str("/%s/blob/%s/%s" % (project_name, rev, commit_fn)) return request.redirect(redir) else: reflog = TEMP_BRANCH_MARKER tmp_branch = project.repo.get_temp_branch() project.repo.commit_file(tmp_branch, ref, user.name, user.email, message, reflog, data) redir = "/%s/newpull/new?base_repo=%s&head_ref=%s" % ( project_name, project_name, urllib.quote(tmp_branch), ) return request.redirect(redir)
def __call__(self, request): proj = CodeDoubanProject.get_by_name(self.proj_name) ref = request.get_form_var('rev', 'HEAD') filename = '/'.join(self.filepath_parts) # TODO: check ref ret = proj.repo.get_file(ref, filename.decode('utf-8')) if ret is None: raise TraversalError() return str(ret.data.encode('utf8'))
def _get_tmpl_commits(tmpl_target, rev, path, project_name, request): project = CodeDoubanProject.get_by_name(project_name) show_merges = request.get_form_var('show_merges', None) if show_merges and show_merges.isdigit(): show_merges = int(show_merges) else: show_merges = 0 if path else 1 # Keep start_rev for older links start_rev = request.get_form_var('start_rev', None) if start_rev and not rev: rev = start_rev if not rev: rev = project.default_branch page = int(request.get_form_var('page', 1)) author = request.get_form_var('author', None) query = request.get_form_var('query', None) skip = NB_COMMITS_PER_PAGE * (page - 1) tdt = _tmpl_common_data(rev, path, project_name, request) revlist = project.repo.get_commits(rev, max_count=NB_COMMITS_PER_PAGE, skip=skip, path=path, author=author, query=query, no_merges=(not show_merges)) comment_counts = _get_comment_counts(revlist, project.id) # handle renamed file renames = dict() if revlist: # and not next: oldcommit = revlist[-1] renames = project.repo.get_renamed_files(oldcommit.sha) rename_from = renames.get(path, '') graph_data = generate_graph_data(revlist) older_revlist = project.repo.get_commits(rev, max_count=NB_COMMITS_PER_PAGE, skip=(NB_COMMITS_PER_PAGE * page), path=path, author=author, query=query) release = get_release(project.repository) tdt.update({ 'comment_counts': comment_counts, 'rename_from': rename_from, 'author': author, 'query': query, 'revlist': revlist, 'renames': renames, 'page': page, 'link_prev': _make_links(project, rev, author, path, page - 1, query), 'link_next': _make_links(project, rev, author, path, page + 1, query) if older_revlist else False, 'release': release, 'graph_data': graph_data, }) return st(tmpl_target, **tdt)
def new(self, request): user = request.user if not user: raise AccessError from_proj = self.project from_ref = request.get_form_var('head_ref', from_proj.default_branch) parent_proj = from_proj.get_forked_from() to_proj = request.get_form_var('base_repo') if to_proj: to_proj = CodeDoubanProject.get_by_name(to_proj) elif parent_proj: to_proj = parent_proj else: to_proj = from_proj if not to_proj: raise TraversalError("The PR's upstream project is not existed") to_ref = request.get_form_var('base_ref', to_proj.default_branch) if from_proj != to_proj: # Allow to create PR to a different project only if user has push perm # ~~A bit weird, maybe should be separate perms # ~~If from and to projects are the same, we should be in online edit mode if not from_proj.has_push_perm(user.name): raise AccessError( "Need push permission to add a PR on another project") pullreq = PullRequest.open(from_proj, from_ref, to_proj, to_ref) family = from_proj.get_fork_network() from_branches = from_proj.repo.branches to_branches = to_proj.repo.branches from_commit = pullreq.from_commit to_commit = pullreq.to_commit if not pullreq.can_pull: raise TraversalError( "The PR's head_ref or base_ref is not existed") highlighted_projects = filter(None, [from_proj, parent_proj]) commits = pullreq.commits n_commits = len(commits) n_authors = len(set(c.author.username for c in commits)) ticket_title, ticket_desc = self._choose_default_PR_title_and_description( commits) # noqa # get diff diff = pullreq.get_diff(rename_detection=True) n_files = diff.length grouped_commits = groupby(commits, lambda c: c.author_time.date()) prs = PullRequest.get_by_from_and_to(from_proj.id, from_ref, to_proj.id, to_ref) open_pullreqs = [] for pr in prs: t = Ticket.get_by_projectid_and_ticketnumber( to_proj.id, pr.ticket_id) if t and t.closed is None: open_pullreqs.append(pr) guideline_url = get_project_guidelines(to_proj) teams = Team.get_all_team_uids() return st('/pull/new.html', **locals())
def new(self, request): user = request.user if not user: raise AccessError from_proj = self.project from_ref = request.get_form_var('head_ref', from_proj.default_branch) parent_proj = from_proj.get_forked_from() to_proj = request.get_form_var('base_repo') if to_proj: to_proj = CodeDoubanProject.get_by_name(to_proj) elif parent_proj: to_proj = parent_proj else: to_proj = from_proj if not to_proj: raise TraversalError("The PR's upstream project is not existed") to_ref = request.get_form_var('base_ref', to_proj.default_branch) if from_proj != to_proj: # Allow to create PR to a different project only if user has push perm # ~~A bit weird, maybe should be separate perms # ~~If from and to projects are the same, we should be in online edit mode if not from_proj.has_push_perm(user.name): raise AccessError( "Need push permission to add a PR on another project") pullreq = PullRequest.open(from_proj, from_ref, to_proj, to_ref) family = from_proj.get_fork_network() from_branches = from_proj.repo.branches to_branches = to_proj.repo.branches from_commit = pullreq.from_commit to_commit = pullreq.to_commit if not pullreq.can_pull: raise TraversalError( "The PR's head_ref or base_ref is not existed") highlighted_projects = filter(None, [from_proj, parent_proj]) commits = pullreq.commits n_commits = len(commits) n_authors = len(set(c.author.username for c in commits)) ticket_title, ticket_desc = self._choose_default_PR_title_and_description(commits) # noqa # get diff diff = pullreq.get_diff(rename_detection=True) n_files = diff.length grouped_commits = groupby(commits, lambda c: c.author_time.date()) prs = PullRequest.get_by_from_and_to( from_proj.id, from_ref, to_proj.id, to_ref) open_pullreqs = [] for pr in prs: t = Ticket.get_by_projectid_and_ticketnumber( to_proj.id, pr.ticket_id) if t and t.closed is None: open_pullreqs.append(pr) guideline_url = get_project_guidelines(to_proj) teams = Team.get_all_team_uids() return st('/pull/new.html', **locals())
def __init__(self, request, proj_name, issue_number): self.proj_name = proj_name self.issue_number = issue_number self.project = CodeDoubanProject.get_by_name(self.proj_name) self.project_issue = ProjectIssue.get(project_id=self.project.id, number=self.issue_number) if not self.project_issue: raise api_errors.NotFoundError('project issue') self.comments = IssueCommentsUI(request, self.project_issue) self.user = request.user
def add_related_project(self, repo_url): info = urlparse(repo_url) project_name = info.path.strip('/') from vilya.models.project import CodeDoubanProject prj = CodeDoubanProject.get_by_name(project_name) if prj not in self.related_projects: store.execute('insert into issue_related_projects ' '(issue_id, project_id) values(%s, %s)', (self.issue_id, prj.id)) store.commit()
def new(self, request): project = CodeDoubanProject.get_by_name(self.proj_name) user = request.user ref = request.get_form_var('ref') assert ref, "comment ref cannot be empty" content = request.get_form_var('content', '') new_comment = Comment.add(project, ref, user.name, content) return request.redirect("/%s/commit/%s#%s" % (self.proj_name, ref, new_comment.uid))
def _q_index(self, request): errors = '' project_name = self.proj_name user = request.user project = CodeDoubanProject.get_by_name(project_name) hooks = [hook for hook in project.hooks if hook.url not in FEATURE_HOOK_URLS] enabled_telchar = next((hook for hook in project.hooks if hook.url == TELCHAR_URL), False) return st('settings/hooks.html', **locals())
def _q_lookup(self, request, hook_id): project_name = self.proj_name user = request.user project = CodeDoubanProject.get_by_name(project_name) if request.get_form_var('_method') == 'delete' \ and project.is_owner(user): hooks = project.hooks hook = (h for h in hooks if int(h.id) == int(hook_id)).next() hook.destroy() return request.redirect('/%s/settings/hooks' % project_name)
def _get_tmpl_blob(tmpl_target, rev, path, project_name, request): """ get blob view """ tdt = _tmpl_common_data(rev, path, project_name, request) project = CodeDoubanProject.get_by_name(project_name) ref = rev if ref is None: ref = project.default_branch last_commit = project.repo.get_last_commit(ref, path=path, no_merges=True) if ref and path else "" tdt.update({"lastcommit": last_commit}) return st(tmpl_target, **tdt)
def __init__(self, request, proj_name, ticket_id): self.proj_name = proj_name self.ticket_id = ticket_id self.project = CodeDoubanProject.get_by_name(self.proj_name) if not self.project: raise TraversalError() self.ticket = Ticket.get_by_projectid_and_ticketnumber( self.project.id, self.ticket_id) if not self.ticket: raise TraversalError()
def _get_tmpl_edit(tmpl_target, rev, path, project_name, request): rgf = request.get_form_var tdt = _tmpl_common_data(rev, path, project_name, request) project = CodeDoubanProject.get_by_name(project_name) errors = '' success = '' ref = rev user = request.user direct_edit_allowed = True if not user: direct_edit_allowed = False elif not project.has_push_perm(user.name): direct_edit_allowed = False if ref is None: ref = project.default_branch if request.method == 'POST': source = rgf('code') message = rgf('message') desc = rgf('desc') if message == '': errors = u'commit summary不能为空' if rgf('newfile') and not re.match('^[a-zA-Z_\-0-9.]+$', rgf('newfilename', '')): errors = 'new filename incorrect: (%s)' % rgf('newfilename') if not errors: if rgf('newfile'): commit_fn = os.path.join(path, rgf('newfilename')) assert not project.repo.get_commit( "%s:%s" % (ref, commit_fn)), "file_already_exist" else: commit_fn = rgf('filename') message = "%s\n\n%s" % (message, desc) message = message.decode("utf8") data = [] data.append([commit_fn, source, 'insert']) if direct_edit_allowed: reflog = 'commit_one_file on %s' % ref assert ref == 'master' or ref in project.repo.branches, \ "commit online allowed on existing branches only" try: project.repo.commit_file( ref, ref, user.name, user.email, message, reflog, data) success = u'提交成功' except AssertionError, err: errors = 'Error updating file %s: REPO COULD BE CORRUPTED, FETCH IT MANUALLY AND CHECK' % err # noqa redir = str('/%s/blob/%s/%s' % (project_name, rev, commit_fn)) return request.redirect(redir) else: reflog = TEMP_BRANCH_MARKER tmp_branch = project.repo.get_temp_branch() project.repo.commit_file( tmp_branch, ref, user.name, user.email, message, reflog, data) redir = '/%s/newpull/new?base_repo=%s&head_ref=%s' % ( project_name, project_name, urllib.quote(tmp_branch)) return request.redirect(redir)
def pages(self, request): user = request.user docs = SphinxDocs(self.proj_name) tdt = { 'project': CodeDoubanProject.get_by_name(self.proj_name), 'request': request, 'user': user, 'docs': docs, 'last_build': docs.last_build_info(), } return st('settings/pages.html', **tdt)
def add_related_project(self, repo_url): info = urlparse(repo_url) project_name = info.path.strip('/') from vilya.models.project import CodeDoubanProject prj = CodeDoubanProject.get_by_name(project_name) if prj not in self.related_projects: store.execute( 'insert into issue_related_projects ' '(issue_id, project_id) values(%s, %s)', (self.issue_id, prj.id)) store.commit()
def _index(self, request): project = CodeDoubanProject.get_by_name(self.proj_name) open_tickets = Ticket.gets_by_proj(project.id, limit=9999) pr_list = [] for t in open_tickets: pullreq = PullRequest.get_by_proj_and_ticket( project.id, t.ticket_number) if pullreq: pr_list.append(pullreq.as_dict()) return pr_list