Last active
October 2, 2020 03:31
-
-
Save rmagatti/1f9406426dafbc38c3d354c27bf0245d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import json | |
| import subprocess | |
| import os | |
| digits = lambda string: int("".join(list(filter(str.isdigit, string)))) | |
| flatten = lambda l: [item for sublist in l for item in sublist] | |
| CH_API_TOKEN = os.getenv("CH_API_TOKEN", None) | |
| if not CH_API_TOKEN: | |
| print("No API Token provided on env var CH_API_TOKEN") | |
| IN_STAGING_STATE = 500038706 | |
| PROJECT = os.getenv("PROJECT", None) | |
| if not PROJECT: | |
| print("PROJECT env var not found") | |
| STATE = "Ready for Review" | |
| ch_headers = {"Clubhouse-Token": CH_API_TOKEN, "Content-Type": "application/json"} | |
| ch_url = lambda entity: "https://api.clubhouse.io/api/v3/{0}".format(entity) | |
| ch_search_payload = { | |
| "page_size": 25, | |
| "query": 'project:{0} state:"{1}"'.format(PROJECT, STATE), | |
| } | |
| ready_for_review_tickets = lambda: requests.get( | |
| ch_url("search"), headers=ch_headers, data=json.dumps(ch_search_payload) | |
| ) | |
| ch_in_staging_payload = {"workflow_state_id": IN_STAGING_STATE} | |
| move_to_in_staging = lambda tn: requests.put( | |
| "{0}/{1}".format(ch_url("stories"), tn), | |
| headers=ch_headers, | |
| data=json.dumps(ch_in_staging_payload), | |
| ) | |
| stories = ( | |
| json.loads(str(ready_for_review_tickets().content, "utf-8")) | |
| .get("stories") | |
| .get("data") | |
| ) | |
| ch_ticket_numbers = [tn["id"] for tn in stories] | |
| print("==== Clubhouse ticket numbers", ch_ticket_numbers) | |
| tickets_to_move = [ | |
| story["story_id"] | |
| for story in [ | |
| { | |
| "story_id": story["id"], | |
| "merged_branch_ids": flatten( | |
| [branch["merged_branch_ids"] for branch in story["branches"]] | |
| ), | |
| "prs_closed": flatten( | |
| [ | |
| [pr["closed"] for pr in prs] | |
| for prs in [branch["pull_requests"] for branch in story["branches"]] | |
| ] | |
| ), | |
| } | |
| for story in stories | |
| if not story["archived"] | |
| ] | |
| if (story["merged_branch_ids"] and not False in story["prs_closed"]) == True | |
| ] | |
| # Set tickets on env var for slack message | |
| try: | |
| ch_ticket_links = " ".join( | |
| [ | |
| "https://app.clubhouse.io/neofinancial/story/{0}".format(tn) | |
| for tn in tickets_to_move | |
| ] | |
| ) | |
| subprocess.check_output( | |
| 'envman add --key CLUBHOUSE_TICKET_LINKS --value \"{0}\"'.format(ch_ticket_links), | |
| stderr=subprocess.STDOUT, | |
| shell=True, | |
| ) | |
| except Exception as e: | |
| print("==== Error while running envman", e) | |
| if tickets_to_move: | |
| print("Moving tickets", tickets_to_move) | |
| for tn in tickets_to_move: | |
| move_to_in_staging(tn) | |
| else: | |
| print("Nothing to move. Finishing.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment