modules: github_release: support anonymous access (#47817)

Releases can be listed without logging in for public projects, so allow
`github_release` to be called without `token` or `password`.

Signed-off-by: Benoît Knecht <benoit.knecht@fsfe.org>
This commit is contained in:
Benoît Knecht 2018-11-22 18:02:39 +01:00 committed by John R Barker
parent 679b7dce63
commit e39fbb9db4

View file

@ -23,14 +23,14 @@ version_added: 2.2
options: options:
token: token:
description: description:
- GitHub Personal Access Token for authenticating - GitHub Personal Access Token for authenticating. Mutually exclusive with C(password).
user: user:
description: description:
- The GitHub account that owns the repository - The GitHub account that owns the repository
required: true required: true
password: password:
description: description:
- The GitHub account password for the user - The GitHub account password for the user. Mutually exclusive with C(token).
version_added: "2.4" version_added: "2.4"
repo: repo:
description: description:
@ -77,6 +77,12 @@ requirements:
''' '''
EXAMPLES = ''' EXAMPLES = '''
- name: Get latest release of a public repository
github_release:
user: ansible
repo: ansible
action: latest_release
- name: Get latest release of testuseer/testrepo - name: Get latest release of testuseer/testrepo
github_release: github_release:
token: tokenabc1234567890 token: tokenabc1234567890
@ -149,9 +155,9 @@ def main():
prerelease=dict(type='bool', default=False), prerelease=dict(type='bool', default=False),
), ),
supports_check_mode=True, supports_check_mode=True,
required_one_of=(('password', 'token'),),
mutually_exclusive=(('password', 'token'),), mutually_exclusive=(('password', 'token'),),
required_if=[('action', 'create_release', ['tag'])], required_if=[('action', 'create_release', ['tag']),
('action', 'create_release', ['password', 'token'], True)],
) )
if not HAS_GITHUB_API: if not HAS_GITHUB_API:
@ -172,14 +178,17 @@ def main():
# login to github # login to github
try: try:
if user and password: if password:
gh_obj = github3.login(user, password=password) gh_obj = github3.login(user, password=password)
elif login_token: elif login_token:
gh_obj = github3.login(token=login_token) gh_obj = github3.login(token=login_token)
else:
gh_obj = github3.GitHub()
# test if we're actually logged in # test if we're actually logged in
if password or login_token:
gh_obj.me() gh_obj.me()
except github3.AuthenticationFailed as e: except github3.exceptions.AuthenticationFailed as e:
module.fail_json(msg='Failed to connect to GitHub: %s' % to_native(e), module.fail_json(msg='Failed to connect to GitHub: %s' % to_native(e),
details="Please check username and password or token " details="Please check username and password or token "
"for repository %s" % repo) "for repository %s" % repo)