1

我在尝试解决一个获取所有链接问题的简单任务时有点卡住了。我猜这基本上是一个图表任务——我处理了一个 jira 问题,找到它的所有链接,然后转到链接问题的链接,直到我处理完所有问题。

我正在测试的 jira 问题的布局如下图所示: 在此处输入图像描述

但结果是这样的:

PMO-100 -> SA-300
SA-100 -> SA-300
SA-100 -> SA-200

它缺少一个链接,但它会错过更多,因为它只处理所有找到的链接中的第一个链接:

__init__       6996   Starting...
_new_conn      6996   Starting new HTTP connection (1): 10.48.34.174
collect_links  6996   Looking for links for issue PMO-100
collect_links  6996   Found 2 links: 27846, 27843
collect_links  6996   processing link 27846
put_link       6996   Appending link 27846 to link register
collect_links  6996   Looking for links for issue SA-300
collect_links  6996   Found 2 links: 27845, 27846
collect_links  6996   processing link 27845
put_link       6996   Appending link 27845 to link register
collect_links  6996   Looking for links for issue SA-100
collect_links  6996   Found 3 links: 27844, 27845, 27843
collect_links  6996   processing link 27844
put_link       6996   Appending link 27844 to link register
collect_links  6996   Looking for links for issue SA-200
collect_links  6996   Found 1 links: 27844
collect_links  6996   processing link 27844
put_link       6996   Link 27844 already exists in link register
collect_links  6996   issue SA-100 is already in tracked issues

发生这种情况是因为 collect_links 方法中的 for ... each 循环 - 每当它被调用时,它都会覆盖 for ... each 集合:

def collect_links(self, key):
        logging.info('Looking for links for issue %s' % key)
        self.tracked.append(key)
        links = self.jac.issue(key).fields.issuelinks
        logging.info('Found %s links: %s' % (len(links), ', '.join(i.id for i in links)))
        for link in links:
            logging.info('processing link %s' % link.id)
            self.put_link(link)
            rel = None
            if hasattr(link, 'outwardIssue'):
                rel = link.outwardIssue.key
            else:
                rel = link.inwardIssue.key
            linked_issue = self.jac.issue(rel)
            if linked_issue.key not in self.tracked:
                return self.collect_links(linked_issue.key)
            else:
                logging.info('issue %s is already in tracked issues' % linked_issue.key)

看起来当您从方法本身中调用 self.method 时,它不会为其创建新的数据块,而是覆盖您已经在方法中创建的数据 - 我该如何克服呢?

会发生什么:

for link in links: #links contains the links linked to current issue
    if linked_issue.key not in self.tracked:
        return self.collect_links(linked_issue.key) #links changes

在最后一行,我希望它保持我的链接集合不变,但它会将其更改为链接集合linked_issue.key

我已将整个代码粘贴在 pastebin 上

4

1 回答 1

1

呃,不确定我是否理解你的问题,但万一我明白了:

for link in links: #links contains the links linked to current issue
    if linked_issue.key not in self.tracked:
        return self.collect_links(linked_issue.key) #links changes

这当然会for在第一个未跟踪的链接处停止循环。如果要继续遍历 的其余元素links,则不能return在此处使用语句。

于 2013-04-12T11:00:52.860 回答