|
| 1 | +import sys |
| 2 | +import json |
| 3 | +import requests |
| 4 | + |
| 5 | +OPENEDX_ORG_ID = "open-edx" |
| 6 | +TRANSIFEX_API = "https://rest.api.transifex.com" |
| 7 | + |
| 8 | + |
| 9 | +def check_arguments_health(): |
| 10 | + """ |
| 11 | + Check the correctness of the expected arguments such as: |
| 12 | + sys.argv[0]: Is not checked for and is suppose to be this file/script name. |
| 13 | + sys.argv[1]: Is the open-release name, and is expected to start with 'open-release' and end with .1 |
| 14 | + sys.argv[2]: Is the transifex token, and only checked that is not an empty string. If it's wrong, invalid, or expired...etc. It would be indicated by other function handle_request_error(). |
| 15 | + len(sys.argv): If the there was less or more than 3 arguments, it will fail. |
| 16 | + And accordingly it would return: |
| 17 | + returns True: If all good it will return True. |
| 18 | + sys.exit('error message'): Whenever any of the checks above fails. |
| 19 | + """ |
| 20 | + if len(sys.argv) < 3: |
| 21 | + sys.exit("Too few arguments, we need release name followed by TX token") |
| 22 | + elif len(sys.argv) > 3: |
| 23 | + sys.exit("Too many arguments!") |
| 24 | + else: |
| 25 | + TX_TOKEN = sys.argv[2] |
| 26 | + release_name = sys.argv[1] |
| 27 | + if TX_TOKEN == "": # Check that Transifex token is not null |
| 28 | + sys.exit("Transifex token is empty. Job aborted") |
| 29 | + if not (release_name.startswith("open-release") and release_name.endswith(".1")): |
| 30 | + sys.exit( |
| 31 | + "Aborted because: it's either not an open-release or not the first version of an open-release" |
| 32 | + ) |
| 33 | + return True |
| 34 | + |
| 35 | + |
| 36 | +def convert_release_to_project_slug(): |
| 37 | + """ |
| 38 | + This is a simple function that takes release name in this format 'open-release/tictactoe.1' and return as 'openedx-documentation-tictactoe'. This is important to do because transifex doesn't support '/' for projects slug which has to be unique as well. |
| 39 | + Return project slug: a project slug for the release. |
| 40 | + """ |
| 41 | + release_name = sys.argv[1].split("/")[1].split(".")[0] |
| 42 | + return f"openedx-documentation-{release_name}" |
| 43 | + |
| 44 | + |
| 45 | +def handle_request_error(response, context): |
| 46 | + """ |
| 47 | + A helper function for handeling failed requests while trying to request something form transifex API. It handels the cases according to the API documentation: |
| 48 | + 401: It occurs when the token is invalid |
| 49 | + 403: It occur when the token is valid, but not with sufficient permission |
| 50 | + *: It would just print the status code and response body |
| 51 | + """ |
| 52 | + code = response.status_code |
| 53 | + if code == 401: |
| 54 | + sys.exit(f"Unauthorized or invalid token! while trying to {context}.\n" |
| 55 | + f"More details: {response.text}") |
| 56 | + elif code == 403: |
| 57 | + sys.exit(f"TX token doesn't have the right permission for {context}.\n" |
| 58 | + f"More details: {response.text}") |
| 59 | + else: |
| 60 | + sys.exit( |
| 61 | + f"Request for {context} returned with {code}.\n" |
| 62 | + f"Detailed response: {response.text}" |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def is_project_exist(): |
| 67 | + """ |
| 68 | + This function given a release name, it will (or try) to get info about the project as if it was already created. And if transifex returns 404, then this means the project doesn't exist. |
| 69 | + Return False: if a project for release doesn't exist in Transifex. |
| 70 | + Exit 1: If status code is 200, i.e. (the project already exists). |
| 71 | + Otherwise it will forward the request to the helper function handle_request_error(). |
| 72 | + """ |
| 73 | + headers = { |
| 74 | + "Accept": "application/vnd.api+json", |
| 75 | + "Authorization": f"Bearer {sys.argv[2]}", |
| 76 | + } |
| 77 | + url = f"{TRANSIFEX_API}/projects/o:{OPENEDX_ORG_ID}:p:{convert_release_to_project_slug()}" |
| 78 | + response = requests.get(url, headers=headers) |
| 79 | + if response.status_code == 404: |
| 80 | + return False |
| 81 | + elif response.status_code == 200: |
| 82 | + sys.exit( |
| 83 | + f"Project with slug {convert_release_to_project_slug()}, already exist. Job aborted!." |
| 84 | + ) |
| 85 | + else: |
| 86 | + handle_request_error(response, context="getting a project detail") |
| 87 | + |
| 88 | + |
| 89 | +def create_transifex_project(): |
| 90 | + """ |
| 91 | + It tries to create a new transifex project, with configuration which can be altered, e.g. passing a new arg that indicate weather the project should machine translation, machine memory...etc. |
| 92 | + If the function succeed, (i.e. request to create project returned 200), it will exit the script with 0 code, and printing the url of the project. Otherwise it will forward the request to the helper function handle_request_error(). |
| 93 | + """ |
| 94 | + payload = { |
| 95 | + "data": { |
| 96 | + "attributes": { |
| 97 | + "machine_translation_fillup": False, |
| 98 | + "translation_memory_fillup": True, |
| 99 | + "name": f"{convert_release_to_project_slug().replace('-',' ').title()}", |
| 100 | + "private": False, |
| 101 | + "slug": convert_release_to_project_slug(), |
| 102 | + "repository_url": "https://github.com/openedx/edx-documentation" |
| 103 | + }, |
| 104 | + "relationships": { |
| 105 | + "organization": { |
| 106 | + "data": {"id": f"o:{OPENEDX_ORG_ID}", "type": "organizations"} |
| 107 | + }, |
| 108 | + "source_language": {"data": {"id": "l:en_US", "type": "languages"}}, |
| 109 | + }, |
| 110 | + "type": "projects", |
| 111 | + } |
| 112 | + } |
| 113 | + headers = { |
| 114 | + "Accept": "application/vnd.api+json", |
| 115 | + "Content-Type": "application/vnd.api+json", |
| 116 | + "Authorization": f"Bearer {sys.argv[2]}", |
| 117 | + } |
| 118 | + url = f"{TRANSIFEX_API}/projects" |
| 119 | + response = requests.post(url,headers=headers,json=payload) |
| 120 | + if response.status_code == 200: |
| 121 | + print(f"A transifex project for {sys.argv[1]}, has been successfully created! \n" |
| 122 | + f"You can access the project at: https://www.transifex.com/{OPENEDX_ORG_ID}/{convert_release_to_project_slug()} \n" |
| 123 | + f"Happy Translations!") |
| 124 | + sys.exit(0) |
| 125 | + else: |
| 126 | + handle_request_error(response,f"Creating a project for {sys.argv[1]}") |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + if check_arguments_health() and not is_project_exist(): |
| 131 | + create_transifex_project() |
0 commit comments