Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if milestone:
milestone_number = pr_data["milestone"].get("number", None)
else:
milestone_number = None
print("----------------------------------------")
# print('milestone data :', pr_data['milestone'])
print("----------------------------------------")
if not target_branch.strip():
milestone_title = pr_data["milestone"]["title"]
parts = milestone_title.split(".")
parts[-1] = "x"
infered_target_branch = ".".join(parts)
print("inferring branch....", infered_target_branch)
target_branch = infered_target_branch
keen.add_event("backport_infering_branch", {"infering_remove_x": 1})
if milestone_number:
milestone_number = int(milestone_number)
labels_names = []
try:
label_names = [l["name"] for l in pr_data["labels"]]
if not label_names and ("issue" in payload.keys()):
labels_names = [l["name"] for l in payload["issue"]["labels"]]
except KeyError:
print("Did not find labels|", pr_data)
# clone locally
# this process can take some time, regen token
atk = session.token()
# FORK it.
fork_epoch = time.time()
"Oops, something went wrong applying the patch... Please have a look at my logs.",
)
print(e.stderr)
print("----")
print(e.stdout)
print("----")
s_reason = "Unknown error line 491"
keen_stats()
return
except Exception as e:
session.post_comment(
comment_url, "Hum, I actually crashed, that should not have happened."
)
print("\n" + e.stderr.decode("utf8", "replace"), file=sys.stderr)
print("\n" + repo.git.status(), file=sys.stderr)
keen.add_event("error", {"git_crash": 1})
s_reason = "Unknown error line 501"
keen_stats()
return
# write the commit message
repo.git.commit("--amend", "-m", msg)
print("== PR #%i applied, with msg:" % prnumber)
print()
print(msg)
print("== ")
# Push the backported work
print("== Pushing work....:")
try:
with requests.Session() as s:
res = s.send(prepared)
return res
except:
import traceback
traceback.print_exc()
pool.submit(fn, self.request, self.config.forward_staging_url)
except:
print(red + "failure to forward")
import traceback
traceback.print_exc()
if "X-Hub-Signature" not in self.request.headers:
keen.add_event("attack", {"type": "no X-Hub-Signature"})
return self.error("WebHook not configured with secret")
if not verify_signature(
self.request.body,
self.request.headers["X-Hub-Signature"],
self.config.webhook_secret,
):
keen.add_event("attack", {"type": "wrong signature"})
return self.error(
"Cannot validate GitHub payload with provided WebHook secret"
)
payload = tornado.escape.json_decode(self.request.body)
org = payload.get("repository", {}).get("owner", {}).get("login")
if not org:
org = (
def __init__(self, commands, config):
keen.add_event("status", {"state": "starting"})
self.commands = commands
self.application = None
self.config = config
self.port = config.port
self.auth = Authenticator(
self.config.integration_id,
self.config.key,
self.config.personnal_account_token,
self.config.personnal_account_name,
)
self.auth._build_auth_id_mapping()
def keen_stats():
nonlocal s_slug
nonlocal s_clone_time
nonlocal s_success
nonlocal s_reason
nonlocal s_fork_time
nonlocal s_clean_time
nonlocal s_ff_time
keen.add_event(
"backport_stats",
{
"slug": s_slug,
"clone_time": s_clone_time,
"fork_time": s_fork_time,
"clean_time": s_clean_time,
"success": s_success,
"fast_forward_opt_time": s_ff_time,
"reason": s_reason,
},
def search(input, sender=None, postback=False):
if postback:
payload = json.loads(input)
intent = payload['intent']
entities = payload['entities']
else:
intent, entities = process_query(input)
# TODO: Needs to be refactored out
try:
keen.project_id = os.environ.get('KEEN_PROJECT_ID', config.KEEN_PROJECT_ID)
keen.write_key = os.environ.get('KEEN_WRITE_KEY', config.KEEN_WRITE_KEY)
keen.add_event('logs', {
'intent': intent,
'entities': entities,
'input': input,
'sender': sender,
'postback': postback
})
except:
pass # Could not stream data for analytics
if intent is not None:
if intent in src.__personalized__ and sender is not None:
r = requests.get('https://graph.facebook.com/v2.6/' + str(sender), params={
'fields': 'first_name',
'access_token': os.environ.get('ACCESS_TOKEN', config.ACCESS_TOKEN)
})
if entities is None:
entities = {}
pr = session.ghrequest("GET", pull_request["url"]).json()
pr_origin_org_repo = pr["head"]["repo"]["full_name"]
origin_repo_org = pr["head"]["user"]["login"]
allow_edit_from_maintainer = pr["maintainer_can_modify"]
# might want to just look at whether the commenter has permission over said branch.
# you _may_ have multiple contributors to a PR.
is_legitimate_author = (pr_author == user) and (pr_author == origin_repo_org)
if is_legitimate_author:
print(user, "is legitimate author of this PR, letting commands go through")
permission_level = session._get_permission(org, repo, user)
command_args = process_mentionning_comment(body, self.mention_bot_re)
for (command, arguments) in command_args:
print(" :: treating", command, arguments)
keen.add_event(
"dispatch",
{
"mention": {
"user": user,
"organisation": org,
"repository": "{}/{}".format(org, repo),
"command": command,
}
},
)
handler = self.actions.get(command.lower(), None)
command = command.lower()
def user_can(user, command, repo, org, session):
"""
callback to test whether the current user has custom permission set on said repository.
rate_remaining = response.headers.get("X-RateLimit-Limit", -1)
if rate_limit:
repo_name_list = [
k for k, v in self.idmap.items() if v == self.installation_id
]
repo_name = "no-repo"
if len(repo_name_list) == 1:
repo_name = repo_name_list[0]
elif len(repo_name_list) == 0:
repo_name = "no-matches"
else:
repo_name = "multiple-matches"
import keen
keen.add_event(
"gh-rate",
{
"limit": int(rate_limit),
"rate_remaining": int(rate_remaining),
"installation": repo_name,
},
)
return response