1925ccdbfSTom Stellard#!/usr/bin/env python3 2f181dd99STom Stellard# ===-- github-upload-release.py ------------------------------------------===# 3925ccdbfSTom Stellard# 4925ccdbfSTom Stellard# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5925ccdbfSTom Stellard# See https://llvm.org/LICENSE.txt for license information. 6925ccdbfSTom Stellard# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7925ccdbfSTom Stellard# 8925ccdbfSTom Stellard# ===------------------------------------------------------------------------===# 9925ccdbfSTom Stellard# 10925ccdbfSTom Stellard# Create and manage releases in the llvm github project. 11925ccdbfSTom Stellard# 12925ccdbfSTom Stellard# This script requires python3 and the PyGithub module. 13925ccdbfSTom Stellard# 14925ccdbfSTom Stellard# Example Usage: 15925ccdbfSTom Stellard# 16925ccdbfSTom Stellard# You will need to obtain a personal access token for your github account in 17925ccdbfSTom Stellard# order to use this script. Instructions for doing this can be found here: 18925ccdbfSTom Stellard# https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line 19925ccdbfSTom Stellard# 20925ccdbfSTom Stellard# Create a new release from an existing tag: 21f181dd99STom Stellard# ./github-upload-release.py --token $github_token --release 8.0.1-rc4 create 22925ccdbfSTom Stellard# 23925ccdbfSTom Stellard# Upload files for a release 24f181dd99STom Stellard# ./github-upload-release.py --token $github_token --release 8.0.1-rc4 upload --files llvm-8.0.1rc4.src.tar.xz 25925ccdbfSTom Stellard# 26925ccdbfSTom Stellard# You can upload as many files as you want at a time and use wildcards e.g. 27f181dd99STom Stellard# ./github-upload-release.py --token $github_token --release 8.0.1-rc4 upload --files *.src.* 28925ccdbfSTom Stellard# ===------------------------------------------------------------------------===# 29925ccdbfSTom Stellard 30925ccdbfSTom Stellard 31925ccdbfSTom Stellardimport argparse 32925ccdbfSTom Stellardimport github 33aa020024STom Stellardimport sys 34600e38b1SDavid Spickettfrom textwrap import dedent 35b71edfaaSTobias Hieta 36aa020024STom Stellard 37925ccdbfSTom Stellarddef create_release(repo, release, tag=None, name=None, message=None): 38925ccdbfSTom Stellard if not tag: 39b71edfaaSTobias Hieta tag = "llvmorg-{}".format(release) 40925ccdbfSTom Stellard 41925ccdbfSTom Stellard if not name: 42b71edfaaSTobias Hieta name = "LLVM {}".format(release) 43925ccdbfSTom Stellard 44925ccdbfSTom Stellard if not message: 45600e38b1SDavid Spickett message = dedent( 46600e38b1SDavid Spickett """\ 47600e38b1SDavid Spickett LLVM {} Release 48600e38b1SDavid Spickett 49600e38b1SDavid Spickett # A note on binaries 50600e38b1SDavid Spickett 51600e38b1SDavid Spickett Volunteers make binaries for the LLVM project, which will be uploaded 52600e38b1SDavid Spickett when they have had time to test and build these binaries. They might 53600e38b1SDavid Spickett not be available directly or not at all for each release. We suggest 54600e38b1SDavid Spickett you use the binaries from your distribution or build your own if you 55600e38b1SDavid Spickett rely on a specific platform or configuration.""" 56600e38b1SDavid Spickett ).format(release) 57925ccdbfSTom Stellard 58925ccdbfSTom Stellard prerelease = True if "rc" in release else False 59925ccdbfSTom Stellard 60b71edfaaSTobias Hieta repo.create_git_release(tag=tag, name=name, message=message, prerelease=prerelease) 61b71edfaaSTobias Hieta 62925ccdbfSTom Stellard 63925ccdbfSTom Stellarddef upload_files(repo, release, files): 64b71edfaaSTobias Hieta release = repo.get_release("llvmorg-{}".format(release)) 65925ccdbfSTom Stellard for f in files: 66b71edfaaSTobias Hieta print("Uploading {}".format(f)) 67925ccdbfSTom Stellard release.upload_asset(f) 68925ccdbfSTom Stellard print("Done") 69925ccdbfSTom Stellard 70925ccdbfSTom Stellard 71925ccdbfSTom Stellardparser = argparse.ArgumentParser() 72aa020024STom Stellardparser.add_argument( 73aa020024STom Stellard "command", type=str, choices=["create", "upload", "check-permissions"] 74aa020024STom Stellard) 75925ccdbfSTom Stellard 76925ccdbfSTom Stellard# All args 77b71edfaaSTobias Hietaparser.add_argument("--token", type=str) 78b71edfaaSTobias Hietaparser.add_argument("--release", type=str) 79aa020024STom Stellardparser.add_argument("--user", type=str) 802836d8edSTom Stellardparser.add_argument("--user-token", type=str) 81925ccdbfSTom Stellard 82925ccdbfSTom Stellard# Upload args 83b71edfaaSTobias Hietaparser.add_argument("--files", nargs="+", type=str) 84925ccdbfSTom Stellard 85925ccdbfSTom Stellardargs = parser.parse_args() 86925ccdbfSTom Stellard 872836d8edSTom Stellardgh = github.Github(args.token) 882836d8edSTom Stellardllvm_org = gh.get_organization("llvm") 89aa020024STom Stellardllvm_repo = llvm_org.get_repo("llvm-project") 90aa020024STom Stellard 91aa020024STom Stellardif args.user: 922836d8edSTom Stellard if not args.user_token: 932836d8edSTom Stellard print("--user-token option required when --user is used") 942836d8edSTom Stellard sys.exit(1) 95aa020024STom Stellard # Validate that this user is allowed to modify releases. 962836d8edSTom Stellard user = gh.get_user(args.user) 972836d8edSTom Stellard team = ( 982836d8edSTom Stellard github.Github(args.user_token) 992836d8edSTom Stellard .get_organization("llvm") 1002836d8edSTom Stellard .get_team_by_slug("llvm-release-managers") 1012836d8edSTom Stellard ) 102aa020024STom Stellard if not team.has_in_members(user): 103aa020024STom Stellard print("User {} is not a allowed to modify releases".format(args.user)) 104aa020024STom Stellard sys.exit(1) 105aa020024STom Stellardelif args.command == "check-permissions": 106aa020024STom Stellard print("--user option required for check-permissions") 107aa020024STom Stellard sys.exit(1) 108925ccdbfSTom Stellard 109b71edfaaSTobias Hietaif args.command == "create": 110*0b9ce71aSTom Stellard create_release(llvm_repo, args.release) 111b71edfaaSTobias Hietaif args.command == "upload": 112925ccdbfSTom Stellard upload_files(llvm_repo, args.release, args.files) 113