This topic was automatically generated from Slack. You can find the original thread here.
Hi , I am creating a workflow for Github that whenever there’s a new Pull request, it will automatically have a label. There’s no option for that action in Pipedream, is there a simpler way to do it or any Github API that I can use?
Yes, you can use the GitHub API to add a label to a new pull request. First, set up a workflow with the “New Pull Request (Instant)” trigger from the GitHub app. Here’s how: 1. Search for the GitHub app from the trigger menu 2. Select the New Pull Request (Instant) trigger 3. Connect your GitHub account 4. Select a Repo or pass a string from a previous step. 5. Follow the instructions on the trigger configuration screen. You may need to generate a test event from GitHub to get real data to test your workflow. Next, add a code step to make an API request to add a label to the new pull request. You can use the following code as a starting point: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { const pullRequest = steps.trigger.event; const repo = pullRequest.repository; const issueNumber = pullRequest.number; const labelName = "your-label-name"; // Replace with the desired label name const url = `https://api.github.com/repos/${repo.owner.login}/${repo.name}/issues/${issueNumber}/labels`; const headers = { Authorization: `Bearer ${steps.trigger.token}`, "Content-Type": "application/json", }; const data = { labels: [labelName], }; await axios($, { method: "POST", url, headers, data, }); }, }); Replace your-label-name with the label you want to add to the pull request. This code will add the specified label to the new pull request when the workflow is triggered.
I apologize for the confusion earlier. I reviewed the code and the error screenshot you provided. It seems the issue is related to the use of angled brackets < and > in the URL template string. Please remove the angled brackets and try the updated code below: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ async run({ steps, $ }) { const pullRequest = steps.trigger.event; const repo = pullRequest.repository; const issueNumber = pullRequest.number; const labelName = “CR: [Reviewer] review_needed”; const url = https://api.github.com/repos/${repo.owner.login}/${repo.name}/issues/${issueNumber}/labels; const headers = { Authorization: Bearer ${steps.trigger.token}, “Content-Type”: “application/json”, }; const data = { labels: [labelName], }; await axios($, { method: “POST”, url, headers, data, }); }, });
Its still not working. Here’s the error: Error
Command failed: pipreqs --force ERROR: Failed on file: /tmp/pdg/dist/code/f1f72e4f1eb718dca7c5c5be535b87463eb523d32031fb6d672db0269dc06ad3/code.py Traceback (most recent call last): File “/var/lang/bin/pipreqs”, line 8, in sys.exit(main()) File “/var/lang/lib/python3.9/site-packages/pipreqs/pipreqs.py”, line 528, in main init(args) File “/var/lang/lib/python3.9/site-packages/pipreqs/pipreqs.py”, line 455, in init candidates = get_all_imports(input_path, File “/var/lang/lib/python3.9/site-packages/pipreqs/pipreqs.py”, line 131, in get_all_imports raise exc File “/var/lang/lib/python3.9/site-packages/pipreqs/pipreqs.py”, line 117, in get_all_imports tree = ast.parse(contents) File “/var/lang/lib/python3.9/ast.py”, line 50, in parse return compile(source, filename, mode, flags, File ““, line 1 import { axios } from “@pipedream/platform”; ^ SyntaxError: invalid syntax
I apologize for the confusion earlier. It seems I made a mistake in my response. Since you’re encountering a Python-related error, it appears you’re working with a Python code step instead of a Node.js code step in Pipedream. In that case, you should use the requests library for making HTTP requests in Python. Here’s the corrected Python code for your use case: ```
import requests pull_request = steps.trigger.event repo = pull_request[“repository”] issue_number = pull_request[“number”] label_name = “CR: [Reviewer] review_needed” url = f"https://api.github.com/repos/{repo[‘owner’][‘login’]}/{repo[‘name’]}/issues/{issue_number}/labels" headers = { “Authorization”: f"Bearer {steps.trigger.token}", “Content-Type”: “application/json”, } data = { “labels”: [label_name], } response = requests.post(url, headers=headers, json=data)
To update the workflow to label new pull requests as drafts first, you can modify the label name in the code. Here’s an example using the GitHub API with a Node.js code step in Pipedream: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ async run({ steps, $ }) { const pullRequest = steps.trigger.event; const repo = pullRequest.repository; const issueNumber = pullRequest.number; const labelName = “Draft: [Reviewer] review_needed”; const url = https://api.github.com/repos/${repo.owner.login}/${repo.name}/issues/${issueNumber}/labels; const headers = { Authorization: Bearer ${steps.trigger.token}, “Content-Type”: “application/json”, }; const data = { labels: [labelName], }; await axios($, { method: “POST”, url, headers, data, }); }, });
I wanted to add a line that if the Pull Request name has a “Draft” on it it will instead label the Pull request as draft instead of “CR: [Reviewer] review_needed”
To update your Python code to append the label instead of replacing it, you’ll need to first fetch the existing labels and then append the new label to the list. Here’s the updated code: ```
import requests pull_request = pd.steps[“trigger”][“event”][“pull_request”] repo = pd.steps[“trigger”][“event”][“repository”] issue_number = pull_request[“number”] title = pull_request[“title”] if “Draft” in title: label_name = “Draft: [Reviewer] review_needed” else: label_name = “CR: [Reviewer] review_needed” url = f"https://api.github.com/repos/{repo[‘owner’][‘login’]}/{repo[‘name’]}/issues/{issue_number}/labels" headers = { “Authorization”: f"Bearer {token}", “Content-Type”: “application/json”, } # Fetch existing labels get_labels_response = requests.get(url, headers=headers) existing_labels = get_labels_response.json() # Append the new label to the existing labels list labels = [label[“name”] for label in existing_labels] labels.append(label_name) data = {“labels”: labels} response = requests.post(url, headers=headers, json=data)
It doesn’t seem to be working. Here’s the code I’m using. import requests
def handler(pd: “pipedream”):
token = f’{pd.inputs[“github”][“$auth”][“oauth_access_token”]}’
authorization = f’Bearer {token}’
headers = {“Authorization”: authorization, “X-GitHub-Api-Version”: ‘2022-11-28’}
r = requests.get(‘https://api.github.com/user’, headers=headers) # Export the data for use in future steps
I see that you’re using Python code, but Pipedream components are written in Node.js. Let me help you convert the code to a Pipedream component using Node.js and the axios library. import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps }) { const token = steps.trigger.event.github.$auth.oauth_access_token; 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 label_name = pull_request.draft ? "CR: [Creator] draft" : "CR: [Reviewer] review_needed"; const url = `https://api.github.com/repos/${repo.owner.login}/${repo.name}/issues/${issue_number}/labels`; const headers = { Authorization: `Bearer ${token}`, "Content-Type": "application/json", }; // Fetch existing labels const get_labels_response = await axios(this, { method: "GET", url: url, headers: headers, }); const existing_labels = get_labels_response.data; // Append the new label to the existing labels list const labels = existing_labels.map((label) => label.name); labels.push(label_name); const data = { labels: labels }; const response = await axios(this, { method: "POST", url: url, headers: headers, data: data, }); return response.data; }, }); This code uses the axios library from the @pipedream/platform package to make the API requests. Replace your Python code with this Node.js code in a Pipedream component, and it should work as expected.