|
| 1 | +import requests |
| 2 | +import argparse |
| 3 | +import datetime |
| 4 | +import yaml |
| 5 | +import os |
| 6 | +import uuid |
| 7 | + |
| 8 | +from org_management import OrgGenerator |
| 9 | + |
| 10 | +_SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) |
| 11 | + |
| 12 | + |
| 13 | +class InactiveUserHandler: |
| 14 | + def __init__( |
| 15 | + self, |
| 16 | + github_org: [str], |
| 17 | + github_org_id: [str], |
| 18 | + activity_date: [str], |
| 19 | + github_token: [str], |
| 20 | + ): |
| 21 | + self.github_org = github_org |
| 22 | + self.github_org_id = github_org_id |
| 23 | + self.activity_date = activity_date |
| 24 | + self.github_token = github_token |
| 25 | + |
| 26 | + def _get_request_headrs(self): |
| 27 | + return {"Authorization": f"Bearer {self.github_token}"} |
| 28 | + |
| 29 | + def _process_request_result(self, request): |
| 30 | + if request.status_code == 200 or request.status_code == 201: |
| 31 | + return request.json() |
| 32 | + else: |
| 33 | + raise Exception(f"Request execution failed with status code of {request.status_code}. {request.status_code}") |
| 34 | + |
| 35 | + def _execute_query(self, query): |
| 36 | + request = requests.post("https://api.github.com/graphql", json={"query": query}, headers=self._get_request_headrs()) |
| 37 | + return self._process_request_result(request) |
| 38 | + |
| 39 | + def _build_query(self, after_cursor_value=None): |
| 40 | + after_cursor = '"{}"'.format(after_cursor_value) if after_cursor_value else "null" |
| 41 | + query = """ |
| 42 | + { |
| 43 | + organization(login: \"%s\") { |
| 44 | + membersWithRole(first: 50, after:%s) { |
| 45 | + pageInfo { |
| 46 | + hasNextPage |
| 47 | + endCursor |
| 48 | + } |
| 49 | + nodes { |
| 50 | + login |
| 51 | + contributionsCollection(organizationID: \"%s\", from: \"%s\") { |
| 52 | + hasAnyContributions |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + """ % ( |
| 59 | + self.github_org, |
| 60 | + after_cursor, |
| 61 | + self.github_org_id, |
| 62 | + self.activity_date, |
| 63 | + ) |
| 64 | + return query |
| 65 | + |
| 66 | + def get_inactive_users(self): |
| 67 | + inactive_users = set() |
| 68 | + has_next_page = True |
| 69 | + after_cursor_value = None |
| 70 | + while has_next_page: |
| 71 | + result = self._execute_query(self._build_query(after_cursor_value)) |
| 72 | + for user_node in result["data"]["organization"]["membersWithRole"]["nodes"]: |
| 73 | + user = user_node["login"] |
| 74 | + activity = user_node["contributionsCollection"]["hasAnyContributions"] |
| 75 | + print(f"The user '{user}' has activity value {activity} contributions") |
| 76 | + if not activity: |
| 77 | + print(f"Adding user '{user}' as inactive") |
| 78 | + inactive_users.add(user) |
| 79 | + |
| 80 | + has_next_page = result["data"]["organization"]["membersWithRole"]["pageInfo"]["hasNextPage"] |
| 81 | + after_cursor_value = result["data"]["organization"]["membersWithRole"]["pageInfo"]["endCursor"] |
| 82 | + |
| 83 | + return inactive_users |
| 84 | + |
| 85 | + def _load_yaml_file(self, path): |
| 86 | + with open(path, "r") as stream: |
| 87 | + return yaml.safe_load(stream) |
| 88 | + |
| 89 | + def _write_yaml_file(self, path, data): |
| 90 | + with open(path, "w") as f: |
| 91 | + yaml.dump(data, f) |
| 92 | + |
| 93 | + def delete_inactive_contributors(self, users_to_delete): |
| 94 | + path = f"{_SCRIPT_PATH}/contributors.yml" |
| 95 | + contributors_yaml = self._load_yaml_file(path) |
| 96 | + users_to_delete_lower = [user.lower() for user in users_to_delete] |
| 97 | + contributors_yaml["contributors"] = [c for c in contributors_yaml["contributors"] if c.lower() not in users_to_delete_lower] |
| 98 | + self._write_yaml_file(path, contributors_yaml) |
| 99 | + |
| 100 | + def get_inactive_users_msg(self, users_to_delete, tagusers): |
| 101 | + rfc = ( |
| 102 | + "https://github.com/cloudfoundry/community/blob/main/toc/rfc/" |
| 103 | + "rfc-0025-define-criteria-and-removal-process-for-inactive-members.md" |
| 104 | + ) |
| 105 | + rfc_revocation_rules = ( |
| 106 | + "https://github.com/cloudfoundry/community/blob/main/toc/rfc/rfc-0025-define-" |
| 107 | + "criteria-and-removal-process-for-inactive-members.md#remove-the-membership-to-the-cloud-foundry-github-organization" |
| 108 | + ) |
| 109 | + user_tagging_prefix = "@" if tagusers else "" |
| 110 | + users_as_list = "\n".join(str(user_tagging_prefix + s) for s in users_to_delete) |
| 111 | + return ( |
| 112 | + f"According to the rolues for inactivity defined in [RFC-0025]({rfc}) following users will be deleted:\n" |
| 113 | + f"{users_as_list}\nAccording to the [revocation policy in the RFC]({rfc_revocation_rules}), users have" |
| 114 | + " two weeks to refute this revocation, if they wish." |
| 115 | + ) |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def _get_bool_env_var(env_var_name, default): |
| 119 | + return os.getenv(env_var_name, default).lower() == "true" |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + one_year_back = (datetime.datetime.now() - datetime.timedelta(days=365)).strftime("%Y-%m-%dT%H:%M:%SZ") |
| 124 | + |
| 125 | + parser = argparse.ArgumentParser(description="Cloud Foundry Org Inactive User Handler") |
| 126 | + parser.add_argument("-goid", "--githuborgid", default="O_kgDOAAl8sg", help="Cloud Foundry Github org ID") |
| 127 | + parser.add_argument("-go", "--githuborg", default="cloudfoundry", help="Cloud Foundry Github org name") |
| 128 | + parser.add_argument("-sd", "--sincedate", default=one_year_back, help="Since when to analyze in format 'Y-m-dTH:M:SZ'") |
| 129 | + parser.add_argument( |
| 130 | + "-gt", "--githubtoken", default=os.environ.get("GH_TOKEN"), help="Github API access token. Supported also as env var 'GH_TOKEN'" |
| 131 | + ) |
| 132 | + parser.add_argument( |
| 133 | + "-dr", |
| 134 | + "--dryrun", |
| 135 | + action="store_true", |
| 136 | + help="Dry run execution. Supported also as env var 'INACTIVE_USER_MANAGEMENT_DRY_RUN'", |
| 137 | + ) |
| 138 | + parser.add_argument( |
| 139 | + "-tu", |
| 140 | + "--tagusers", |
| 141 | + action="store_true", |
| 142 | + help="Tag users to be notified. Supported also as env var 'INACTIVE_USER_MANAGEMENT_TAG_USERS'", |
| 143 | + ) |
| 144 | + args = parser.parse_args() |
| 145 | + |
| 146 | + print("Get information about community users") |
| 147 | + generator = OrgGenerator() |
| 148 | + generator.load_from_project() |
| 149 | + community_members_with_role = generator.get_community_members_with_role() |
| 150 | + |
| 151 | + print("Analyzing Cloud Foundry org user activity.") |
| 152 | + userHandler = InactiveUserHandler(args.githuborg, args.githuborgid, args.sincedate, args.githubtoken) |
| 153 | + inactive_users = userHandler.get_inactive_users() |
| 154 | + |
| 155 | + print(f"Inactive users length is {len(inactive_users)} and inactive users are {inactive_users}") |
| 156 | + users_to_delete = inactive_users - community_members_with_role |
| 157 | + tagusers = args.tagusers or InactiveUserHandler._get_bool_env_var("INACTIVE_USER_MANAGEMENT_TAG_USERS", "False") |
| 158 | + inactive_users_msg = userHandler.get_inactive_users_msg(users_to_delete, tagusers) |
| 159 | + if args.dryrun or InactiveUserHandler._get_bool_env_var("INACTIVE_USER_MANAGEMENT_DRY_RUN", "False"): |
| 160 | + print(f"Dry-run mode.\nInactive_users_msg is: {inactive_users_msg}") |
| 161 | + print(f"Following users will be deleted: {inactive_users}") |
| 162 | + elif users_to_delete: |
| 163 | + userHandler.delete_inactive_contributors(users_to_delete) |
| 164 | + with open(os.environ["GITHUB_OUTPUT"], "a") as env: |
| 165 | + separator = uuid.uuid1() |
| 166 | + step_output_name = "inactive_users_pr_description" |
| 167 | + print(f"{step_output_name}<<{separator}\n{inactive_users_msg}\n{separator}", file=env) |
| 168 | + |
| 169 | + inactive_users_with_role = community_members_with_role.intersection(inactive_users) |
| 170 | + print(f"Inactive users with role length is {len(inactive_users_with_role)} and users are {inactive_users_with_role}") |
0 commit comments