|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 4 | +# http://nexb.com and https://github.com/nexB/scancode-toolkit/ |
| 5 | +# The ScanCode software is licensed under the Apache License version 2.0. |
| 6 | +# Data generated with ScanCode require an acknowledgment. |
| 7 | +# ScanCode is a trademark of nexB Inc. |
| 8 | +# |
| 9 | +# You may not use this software except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 11 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 12 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 13 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 14 | +# specific language governing permissions and limitations under the License. |
| 15 | +# |
| 16 | +# When you publish or redistribute any data created with ScanCode or any ScanCode |
| 17 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 18 | +# |
| 19 | +# Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 20 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 21 | +# ScanCode should be considered or used as legal advice. Consult an Attorney |
| 22 | +# for any legal advice. |
| 23 | +# ScanCode is a free software code scanning tool from nexB Inc. and others. |
| 24 | +# Visit https://github.com/nexB/scancode-toolkit/ for support and download. |
| 25 | + |
| 26 | +from __future__ import absolute_import |
| 27 | +from __future__ import print_function |
| 28 | + |
| 29 | +import argparse |
| 30 | +from pathlib import Path |
| 31 | +import os |
| 32 | +import sys |
| 33 | + |
| 34 | +from github_release_retry import github_release_retry as grr |
| 35 | + |
| 36 | +from commoncode.fileutils import resource_iter |
| 37 | + |
| 38 | + |
| 39 | +""" |
| 40 | +Create GitHUb releases and upload files there. |
| 41 | +This depends on the `github_release_retry` utility |
| 42 | +https://github.com/google/github-release-retry |
| 43 | +""" |
| 44 | + |
| 45 | + |
| 46 | +def create_or_update_release_and_upload_directory( |
| 47 | + user, |
| 48 | + repo, |
| 49 | + tag_name, |
| 50 | + token, |
| 51 | + directory, |
| 52 | + retry_limit=10, |
| 53 | + description=None |
| 54 | +): |
| 55 | + """ |
| 56 | + Create or update a GitHub release at https://github.com/<user>/<repo> for |
| 57 | + `tag_name` tag using the optional `description` for this release. |
| 58 | + Use the provided `token` as a GitHub token for API calls authentication. |
| 59 | + Upload all files found in the `directory` tree to that GitHub release. |
| 60 | + Retry API calls up to `retry_limit` time to work around instability the |
| 61 | + GitHub API. |
| 62 | + """ |
| 63 | + |
| 64 | + api = grr.GithubApi( |
| 65 | + github_api_url='https://api.github.com', |
| 66 | + user=user, |
| 67 | + repo=repo, |
| 68 | + token=token, |
| 69 | + retry_limit=retry_limit, |
| 70 | + ) |
| 71 | + release = grr.Release(tag_name=tag_name, body=description) |
| 72 | + files = [Path(r) for r in resource_iter(directory, with_dirs=False)] |
| 73 | + grr.make_release(api, release, files) |
| 74 | + |
| 75 | + |
| 76 | +def main_with_args(args): |
| 77 | + parser = argparse.ArgumentParser( |
| 78 | + description=( |
| 79 | + 'Create (or update) a GitHub release and upload all the ' |
| 80 | + 'files of DIRECTORY to that release.' |
| 81 | + ), |
| 82 | + ) |
| 83 | + |
| 84 | + parser.add_argument( |
| 85 | + '--user', |
| 86 | + help='The GitHub username or organization in which the repository resides.', |
| 87 | + type=str, |
| 88 | + required=True, |
| 89 | + ) |
| 90 | + |
| 91 | + parser.add_argument( |
| 92 | + '--repo', |
| 93 | + help=' The GitHub repository name in which to create the release.', |
| 94 | + type=str, |
| 95 | + required=True, |
| 96 | + ) |
| 97 | + |
| 98 | + parser.add_argument( |
| 99 | + '--tag-name', |
| 100 | + help='The name of the tag to create (or re-use) for this release.', |
| 101 | + type=str, |
| 102 | + required=True, |
| 103 | + ) |
| 104 | + |
| 105 | + parser.add_argument( |
| 106 | + '--directory', |
| 107 | + help='The directory that contains files to upload to the release.', |
| 108 | + type=str, |
| 109 | + required=True, |
| 110 | + ) |
| 111 | + |
| 112 | + TOKEN_HELP = ( |
| 113 | + 'The Github personal acess token is used to authenticate API calls. ' |
| 114 | + 'Required unless you set the GITHUB_TOKEN environment variable as an alternative. ' |
| 115 | + 'See for details: https://github.com/settings/tokens and ' |
| 116 | + 'https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token' |
| 117 | + ) |
| 118 | + |
| 119 | + parser.add_argument( |
| 120 | + '--token', |
| 121 | + help=TOKEN_HELP, |
| 122 | + type=str, |
| 123 | + required=False, |
| 124 | + ) |
| 125 | + |
| 126 | + parser.add_argument( |
| 127 | + '--description', |
| 128 | + help='Text description for the release. Ignored if the release exists.', |
| 129 | + type=str, |
| 130 | + required=False, |
| 131 | + ) |
| 132 | + |
| 133 | + parser.add_argument( |
| 134 | + '--retry_limit', |
| 135 | + help=( |
| 136 | + 'Number of retries when making failing GitHub API calls. ' |
| 137 | + 'Retrying helps work around transient failures of the GitHub API.' |
| 138 | + ), |
| 139 | + type=int, |
| 140 | + default=10, |
| 141 | + ) |
| 142 | + |
| 143 | + args = parser.parse_args() |
| 144 | + token = args.token or os.environ.get('GITHUB_TOKEN', None) |
| 145 | + if not token: |
| 146 | + print('--token required option is missing.') |
| 147 | + print(TOKEN_HELP) |
| 148 | + sys.exit(1) |
| 149 | + |
| 150 | + create_or_update_release_and_upload_directory( |
| 151 | + user=args.user, |
| 152 | + repo=args.repo, |
| 153 | + tag_name=args.tag_name, |
| 154 | + description=args.description, |
| 155 | + retry_limit=args.retry_limit, |
| 156 | + token=token, |
| 157 | + directory=args.directory, |
| 158 | + ) |
| 159 | + |
| 160 | + |
| 161 | +def main(): |
| 162 | + main_with_args(sys.argv[1:]) |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == '__main__': |
| 166 | + main() |
0 commit comments