| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright Ansible Team |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: github_release |
| 11 |
short_description: Interact with GitHub Releases |
| 12 |
description: |
| 13 |
- Fetch metadata about GitHub Releases. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
token: |
| 23 |
description: |
| 24 |
- GitHub Personal Access Token for authenticating. Mutually exclusive with O(password). |
| 25 |
type: str |
| 26 |
user: |
| 27 |
description: |
| 28 |
- The GitHub account that owns the repository. |
| 29 |
type: str |
| 30 |
required: true |
| 31 |
password: |
| 32 |
description: |
| 33 |
- The GitHub account password for the user. Mutually exclusive with O(token). |
| 34 |
type: str |
| 35 |
repo: |
| 36 |
description: |
| 37 |
- Repository name. |
| 38 |
type: str |
| 39 |
required: true |
| 40 |
action: |
| 41 |
description: |
| 42 |
- Action to perform. |
| 43 |
type: str |
| 44 |
required: true |
| 45 |
choices: ['latest_release', 'create_release'] |
| 46 |
tag: |
| 47 |
description: |
| 48 |
- Tag name when creating a release. Required when using O(action=create_release). |
| 49 |
type: str |
| 50 |
target: |
| 51 |
description: |
| 52 |
- Target of release when creating a release. |
| 53 |
type: str |
| 54 |
name: |
| 55 |
description: |
| 56 |
- Name of release when creating a release. |
| 57 |
type: str |
| 58 |
body: |
| 59 |
description: |
| 60 |
- Description of the release when creating a release. |
| 61 |
type: str |
| 62 |
draft: |
| 63 |
description: |
| 64 |
- Sets if the release is a draft or not. (boolean). |
| 65 |
type: bool |
| 66 |
default: false |
| 67 |
prerelease: |
| 68 |
description: |
| 69 |
- Sets if the release is a prerelease or not. (boolean). |
| 70 |
type: bool |
| 71 |
default: false |
| 72 |
|
| 73 |
author: |
| 74 |
- "Adrian Moisey (@adrianmoisey)" |
| 75 |
requirements: |
| 76 |
- "github3.py >= 1.0.0a3" |
| 77 |
""" |
| 78 |
|
| 79 |
EXAMPLES = r""" |
| 80 |
- name: Get latest release of a public repository |
| 81 |
community.general.github_release: |
| 82 |
user: ansible |
| 83 |
repo: ansible |
| 84 |
action: latest_release |
| 85 |
|
| 86 |
- name: Get latest release of testuseer/testrepo |
| 87 |
community.general.github_release: |
| 88 |
token: tokenabc1234567890 |
| 89 |
user: testuser |
| 90 |
repo: testrepo |
| 91 |
action: latest_release |
| 92 |
|
| 93 |
- name: Get latest release of test repo using username and password |
| 94 |
community.general.github_release: |
| 95 |
user: testuser |
| 96 |
password: secret123 |
| 97 |
repo: testrepo |
| 98 |
action: latest_release |
| 99 |
|
| 100 |
- name: Create a new release |
| 101 |
community.general.github_release: |
| 102 |
token: tokenabc1234567890 |
| 103 |
user: testuser |
| 104 |
repo: testrepo |
| 105 |
action: create_release |
| 106 |
tag: test |
| 107 |
target: master |
| 108 |
name: My Release |
| 109 |
body: Some description |
| 110 |
""" |
| 111 |
|
| 112 |
RETURN = r""" |
| 113 |
tag: |
| 114 |
description: Version of the created/latest release. |
| 115 |
type: str |
| 116 |
returned: success |
| 117 |
sample: 1.1.0 |
| 118 |
""" |
| 119 |
|
| 120 |
import traceback |
| 121 |
|
| 122 |
GITHUB_IMP_ERR = None |
| 123 |
try: |
| 124 |
import github3 |
| 125 |
|
| 126 |
HAS_GITHUB_API = True |
| 127 |
except ImportError: |
| 128 |
GITHUB_IMP_ERR = traceback.format_exc() |
| 129 |
HAS_GITHUB_API = False |
| 130 |
|
| 131 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 132 |
|
| 133 |
|
| 134 |
def main(): |
| 135 |
module = AnsibleModule( |
| 136 |
argument_spec=dict( |
| 137 |
repo=dict(required=True), |
| 138 |
user=dict(required=True), |
| 139 |
password=dict(no_log=True), |
| 140 |
token=dict(no_log=True), |
| 141 |
action=dict(required=True, choices=["latest_release", "create_release"]), |
| 142 |
tag=dict(type="str"), |
| 143 |
target=dict(type="str"), |
| 144 |
name=dict(type="str"), |
| 145 |
body=dict(type="str"), |
| 146 |
draft=dict(type="bool", default=False), |
| 147 |
prerelease=dict(type="bool", default=False), |
| 148 |
), |
| 149 |
supports_check_mode=True, |
| 150 |
mutually_exclusive=(("password", "token"),), |
| 151 |
required_if=[("action", "create_release", ["tag"]), ("action", "create_release", ["password", "token"], True)], |
| 152 |
) |
| 153 |
|
| 154 |
if not HAS_GITHUB_API: |
| 155 |
module.fail_json(msg=missing_required_lib("github3.py >= 1.0.0a3"), exception=GITHUB_IMP_ERR) |
| 156 |
|
| 157 |
repo = module.params["repo"] |
| 158 |
user = module.params["user"] |
| 159 |
password = module.params["password"] |
| 160 |
login_token = module.params["token"] |
| 161 |
action = module.params["action"] |
| 162 |
tag = module.params.get("tag") |
| 163 |
target = module.params.get("target") |
| 164 |
name = module.params.get("name") |
| 165 |
body = module.params.get("body") |
| 166 |
draft = module.params.get("draft") |
| 167 |
prerelease = module.params.get("prerelease") |
| 168 |
|
| 169 |
# login to github |
| 170 |
try: |
| 171 |
if password: |
| 172 |
gh_obj = github3.login(user, password=password) |
| 173 |
elif login_token: |
| 174 |
gh_obj = github3.login(token=login_token) |
| 175 |
else: |
| 176 |
gh_obj = github3.GitHub() |
| 177 |
|
| 178 |
# GitHub's token formats: |
| 179 |
# - ghp_ - Personal access token (classic) |
| 180 |
# - github_pat_ - Fine-grained personal access token |
| 181 |
# - gho_ - OAuth access token |
| 182 |
# - ghu_ - User access token for a GitHub App |
| 183 |
# - ghs_ - Installation access token for a GitHub App |
| 184 |
# - ghr_ - Refresh token for a GitHub App |
| 185 |
# |
| 186 |
# References: |
| 187 |
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/about-authentication-to-github#githubs-token-formats |
| 188 |
# |
| 189 |
# Test if we're actually logged in, but skip this check for some token prefixes |
| 190 |
SKIPPED_TOKEN_PREFIXES = ["ghs_"] |
| 191 |
if password or (login_token and not any(login_token.startswith(prefix) for prefix in SKIPPED_TOKEN_PREFIXES)): |
| 192 |
gh_obj.me() |
| 193 |
except github3.exceptions.AuthenticationFailed as e: |
| 194 |
module.fail_json( |
| 195 |
msg=f"Failed to connect to GitHub: {e}", |
| 196 |
details=f"Please check username and password or token for repository {repo}", |
| 197 |
) |
| 198 |
except github3.exceptions.GitHubError as e: |
| 199 |
module.fail_json( |
| 200 |
msg=f"GitHub API error: {e}", details=f"Please check username and password or token for repository {repo}" |
| 201 |
) |
| 202 |
|
| 203 |
repository = gh_obj.repository(user, repo) |
| 204 |
|
| 205 |
if not repository: |
| 206 |
module.fail_json(msg=f"Repository {user}/{repo} doesn't exist") |
| 207 |
|
| 208 |
if action == "latest_release": |
| 209 |
release = repository.latest_release() |
| 210 |
if release: |
| 211 |
module.exit_json(tag=release.tag_name) |
| 212 |
else: |
| 213 |
module.exit_json(tag=None) |
| 214 |
|
| 215 |
if action == "create_release": |
| 216 |
release_exists = repository.release_from_tag(tag) |
| 217 |
if release_exists: |
| 218 |
module.exit_json(changed=False, msg=f"Release for tag {tag} already exists.") |
| 219 |
|
| 220 |
release = repository.create_release(tag, target, name, body, draft, prerelease) |
| 221 |
if release: |
| 222 |
module.exit_json(changed=True, tag=release.tag_name) |
| 223 |
else: |
| 224 |
module.exit_json(changed=False, tag=None) |
| 225 |
|
| 226 |
|
| 227 |
if __name__ == "__main__": |
| 228 |
main() |