-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) |