Esempio n. 1
0
def link():
	config_yaml = open("config.yaml", "r")
	config = yaml.load(config_yaml)
	config_yaml.close()
	trello_board = config["trello_board"]
	git = Git(config["git_username"], config["git_email"], config["git_password"])
	trello = Trello(config["trello_key"], config["trello_token"])
	#first get all issues from the github repo
	shared_topics = {}
	issues = git.getIssues("Brewmaster")
	issue_titles = []
	for issue in issues:
		issue_titles.append(issue["title"])
	#get all cards from trello board
	card_names = []
	cards = trello.getCards(trello_board)
	for card in cards:
		card_names.append(card["name"])
		for label in card["labels"]:
			if GIT == label["name"]:
				if not card["name"] in issue_titles:
					label_list = []
					for label in card["labels"]:
						if not label["name"] == GIT:
							label_list.append(label["name"])
					title = card["name"]
					body = card["desc"]
					response = git.raiseIssue("Brewmaster", title, body, 
						labels=label_list)
					comments = trello.getCardComments(card["id"])
					for comment in comments:
						body = comment["data"]["text"]
						git.comment("Brewmaster", response["number"],
							body)	
				else:
					shared_topics[str(card["name"])] = [card]

	lists = trello.getLists(config["trello_board"])
	labels = trello.getLabels(config["trello_board"])
	for issue in issues:
		if not issue["title"] in card_names:
			#add issue as a trello card
			#label it
			name = issue["title"]
			desc = issue["body"]
			label_list = []
			for label in issue["labels"]:
				label_list.append(label["name"])
			label_ids = []
			for label in labels:
				if label["name"].lower() in label_list:
					label_ids.append(label["id"])
				if label["name"] == GIT:
					label_ids.append(label["id"])
			list_name = None
			if issue["state"] == "open":
				if issue["comments"] > 0:
					list_name = "Doing"
				else:
					list_name = "To Do"
			else:
				list_name = "Done"
			list_id = None
			for list in lists:
				if list["name"] == list_name:
					list_id = list["id"]
			response = trello.addCard(name, desc, list_id, label_ids)
			#add all comments as trello card comments
			comments = git.getIssueComments("Brewmaster", str(issue["number"]))
			for comment in comments:
				body = comment["body"]
				trello.comment(response["id"], body)
		else:
			shared_topics[issue["title"]].append(issue)

	"""	next check if each issue/card in the dictionary are synced
	if not, then adjust the older version to match the most recently
	updated version
	Check update times:
	card["dateLastActivity"], issue["updated_at"]
	Formatted:
	2015-09-27T00:12:32.556Z 2015-09-26T21:52:01Z """

	for key in shared_topics.keys():
		#both on Zulu time
		issue = shared_topics[key][1]
		card = shared_topics[key][0]
		issue_time = issue["updated_at"]
		card_time = card["dateLastActivity"]
		issue_time = datetime.strptime(issue_time,"%Y-%m-%dT%H:%M:%SZ")
		card_time = datetime.strptime(card_time,"%Y-%m-%dT%H:%M:%S.%fZ")
		if issue_time > card_time:
			sync(git, trello, issue, card, "issue", config)
		elif card_time > issue_time:
			sync(git, trello, issue, card, "card", config)