mirror of
https://github.com/elastic/logstash.git
synced 2025-06-27 17:08:55 -04:00
devtools/label: Add initial script that can label issues/PRs (#14296)
devtools/label: Add initial script to auto-label issues/PRs. The first iteration of this script only allows users to add a version label based on the local branch version.yml.
This commit is contained in:
parent
dfb109843d
commit
e9d4d022f9
1 changed files with 79 additions and 0 deletions
79
devtools/label
Executable file
79
devtools/label
Executable file
|
@ -0,0 +1,79 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Cherry pick and backport a PR"""
|
||||
from __future__ import print_function
|
||||
|
||||
from builtins import input
|
||||
import sys
|
||||
import os
|
||||
import argparse
|
||||
from os.path import expanduser
|
||||
import re
|
||||
from subprocess import check_call, call, check_output
|
||||
import requests
|
||||
import json
|
||||
|
||||
usage = """
|
||||
Example usage:
|
||||
./devtools/label <pr_number>
|
||||
"""
|
||||
|
||||
|
||||
def main():
|
||||
"""Main"""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Adds a label to a pull request",
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog=usage)
|
||||
parser.add_argument("pr_number",
|
||||
help="The PR number being merged (e.g. 2345)")
|
||||
args = parser.parse_args()
|
||||
|
||||
print(args)
|
||||
|
||||
create_label(parser, args)
|
||||
|
||||
def create_label(parser, args):
|
||||
info("Checking if GitHub API token is available in `~/.elastic/github.token`")
|
||||
token = get_github_token()
|
||||
|
||||
if not token:
|
||||
info("GitHub API token not available.")
|
||||
else:
|
||||
# initialize github
|
||||
session = github_session(token)
|
||||
base = "https://api.github.com/repos/elastic/logstash"
|
||||
|
||||
# add labels
|
||||
labels = []
|
||||
|
||||
# get the version (vX.Y.Z) we are merging to
|
||||
version = get_version(os.getcwd())
|
||||
if version:
|
||||
labels.append(version)
|
||||
|
||||
session.post(
|
||||
base + "/issues/{}/labels".format(args.pr_number), json=labels)
|
||||
|
||||
def get_version(base_dir):
|
||||
with open(os.path.join(base_dir, "versions.yml"), "r") as f:
|
||||
for line in f:
|
||||
if line.startswith('logstash:'):
|
||||
return "v" + line.split(':')[-1].strip()
|
||||
|
||||
def get_github_token():
|
||||
try:
|
||||
token = open(expanduser("~/.elastic/github.token"), "r").read().strip()
|
||||
except:
|
||||
token = False
|
||||
return token
|
||||
|
||||
def github_session(token):
|
||||
session = requests.Session()
|
||||
session.headers.update({"Authorization": "token " + token})
|
||||
return session
|
||||
|
||||
def info(msg):
|
||||
print("\nINFO: {}".format(msg))
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
Loading…
Add table
Add a link
Reference in a new issue