How to Correct Errors in the Pull Request Labeling Code for GitHub?

This topic was automatically generated from Slack. You can find the original thread here.

I need help in correcting this code:

import requests

def handler(pd):
token = pd.inputs[‘github’][‘$auth’][‘oauth_access_token’]
authorization = f’Bearer {token}’
headers = {“Authorization”: authorization, “X-GitHub-Api-Version”: ‘2022-11-28’}

pull_request = pd.steps[‘trigger’][‘event’][‘pull_request’]
repo = pd.steps[‘trigger’][‘event’][‘repository’]
issue_number = pull_request[“number”]
title = pull_request[“title”]


url = f”https://api.github.com/repos/{repo['owner']['login']}/{repo['name']}/issues/{issue_number}/labels”

_# Fetch existing labels_
get_labels_response = requests.get(url, headers=headers)
existing_labels = get_labels_response.json()

_# Remove the old label if it exists_
_#old_labels = [“CR: [Creator] draft”, “CR: [Reviewer] review_needed”]_
labels = [label[“name”] **for** label **in** existing_labels **if** **not** label[“name”].startswith(“CR:“)]
label_name = get_label_name(pd.steps)
_# Append the new label to the existing labels list_
labels.append(label_name)

data = {“labels”: labels}

_# Update the labels using a PUT request_
response = requests.put(url, headers=headers, json=data)

**return** response.json()

def get_label_name(steps):
review = steps[‘trigger’][‘event’][‘state’]
conditionMet = review[‘state’] == ‘changes_requested’

**if** steps[‘trigger’][‘event’][‘pull_request’][“draft”]:
    label_name = “CR: [Creator] draft”
**elif** conditionMet:
    label_name = ‘CR: [Creator] need_to_address_comments’
**elif**:
    label_name = ‘CR: [Oncall] ready_to_be_merged’
**else**:
    label_name = “CR: [Reviewer] review_needed”

return {
‘label’: label
}

I see that you’re trying to update the labels of a GitHub pull request based on certain conditions. To achieve this, you can use the following Pipedream component code: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { const token = steps.trigger.event.auth.oauth_access_token; const authorization = `Bearer ${token}`; const headers = { Authorization: authorization, "X-GitHub-Api-Version": "2022-11-28", }; const pull_request = steps.trigger.event.pull_request; const repo = steps.trigger.event.repository; const issue_number = pull_request.number; const title = pull_request.title; const url = `https://api.github.com/repos/${repo.owner.login}/${repo.name}/issues/${issue_number}/labels`; // Fetch existing labels const get_labels_response = await axios($, { method: "GET", url: url, headers: headers, }); const existing_labels = get_labels_response; // Remove the old label if it exists const labels = existing_labels.filter(label => !label.name.startsWith("CR:")); const label_name = get_label_name(steps); labels.push(label_name); const data = { labels: labels }; // Update the labels using a PUT request const response = await axios($, { method: "PUT", url: url, headers: headers, data: data, }); return response; }, }); function get_label_name(steps) { const review = steps.trigger.event.state; const conditionMet = review.state === "changes_requested"; let label_name; if (steps.trigger.event.pull_request.draft) { label_name = "CR: [Creator] draft"; } else if (conditionMet) { label_name = "CR: [Creator] need_to_address_comments"; } else if (/** missing condition **/) { label_name = "CR: [Oncall] ready_to_be_merged"; } else { label_name = "CR: [Reviewer] review_needed"; } return label_name; } Please note that there’s a missing condition in the get_label_name function. You should replace /** missing condition **/ with the appropriate condition to determine when the label should be "CR: [Onc

can you convert the suggested code into Python?

I can help you convert the suggested Pipedream component code into Python. However, please note that Pipedream primarily uses Node.js for its components. If you still want to proceed with Python, here’s the equivalent Python code for the provided Pipedream component: import requests def update_labels(token, pull_request, repo, issue_number, title): authorization = f"Bearer {token}" headers = { "Authorization": authorization, "X-GitHub-Api-Version": "2022-11-28", } url = f"https://api.github.com/repos/{repo['owner']['login']}/{repo['name']}/issues/{issue_number}/labels" # Fetch existing labels get_labels_response = requests.get(url, headers=headers) existing_labels = get_labels_response.json() # Remove the old label if it exists labels = [label for label in existing_labels if not label['name'].startswith("CR:")] label_name = get_label_name(pull_request) labels.append(label_name) data = {"labels": labels} # Update the labels using a PUT request response = requests.put(url, headers=headers, json=data) return response.json() def get_label_name(pull_request): review = pull_request['state'] condition_met = review['state'] == "changes_requested" if pull_request['draft']: label_name = "CR: [Creator] draft" elif condition_met: label_name = "CR: [Creator] need_to_address_comments" elif False: # Replace with the missing condition label_name = "CR: [Oncall] ready_to_be_merged" else: label_name = "CR: [Reviewer] review_needed" return label_name Please note that there’s a missing condition in the get_label_name function. You should replace False with the appropriate condition to determine when the label should be “CR: [Oncall] ready_to_be_merged”.

for this part:
elif False: # Replace with the missing condition
label_name = “CR: [Oncall] ready_to_be_merged”
else:
label_name = “CR: [Reviewer] review_needed”

I need these labels whenever someone submitted a review changes for the Pull Request.

I understand that you want to set the label “CR: [Oncall] ready_to_be_merged” when someone submits a review with changes for the Pull Request. You can update the missing condition in the get_label_name function like this: ```
def get_label_name(pull_request): review = pull_request[‘state’] condition_met = review[‘state’] == “changes_requested” if pull_request[‘draft’]: label_name = “CR: [Creator] draft” elif condition_met: label_name = “CR: [Creator] need_to_address_comments” elif review[‘state’] == “approved”: # This is the missing condition label_name = “CR: [Oncall] ready_to_be_merged” else: label_name = “CR: [Reviewer] review_needed” return label_name