"""Connection library for GitHub"""importloggingimportsalt.utils.httpimportsalt.utils.jsonlog=logging.getLogger(__name__)
[docs]defget_user_pubkeys(users):""" Retrieve a set of public keys from GitHub for the specified list of users. Expects input in list format. Optionally, a value in the list may be a dict whose value is a list of key IDs to be returned. If this is not done, then all keys will be returned. Some example data structures that coupld be passed in would look like: .. code-block:: yaml ['user1', 'user2', 'user3'] [ 'user1': [ '12345', '67890', ], 'user2', 'user3', ] """ifnotisinstance(users,list):return{"Error":"A list of users is expected"}ret={}foruserinusers:key_ids=[]ifisinstance(user,dict):tmp_user=next(iter(user.keys()))key_ids=user[tmp_user]user=tmp_userurl=f"https://api.github.com/users/{user}/keys"result=salt.utils.http.query(url,"GET",decode=False,text=True,)keys=salt.utils.json.loads(result["text"])ret[user]={}forkeyinkeys:ifkey_ids:ifstr(key["id"])inkey_ids:ret[user][key["id"]]=key["key"]else:ret[user][key["id"]]=key["key"]returnret