How to Automatically Add a Label to New Pull Requests in GitHub Using Pipedream or GitHub API?

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.

: timely :point_up_2:

Hi , I’m still learning Python and I’m not sure what to edit or update in the code provided above. Can you check what I did here:

import { axios } from@Storytell-ai/storytell”;

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,
});

},
});

I noticed a couple of issues in the code you provided. First, you should import axios from @pipedream/platform, not from @Storytell-ai/storytell. Second, use regular double quotes instead of curly quotes for strings. Here’s the corrected code: ```
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, }); }, });

still having errors here: Image 2023-09-05 at 8.19.44 PM

I apologize for the confusion earlier. I reviewed the screenshot you provided and noticed that you’re using the wrong import statement. Please update the import statement to: import { axios } from "@pipedream/platform"; Here’s the corrected code: ```
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, }); }, });

Still having the same error. Here’s the code I used

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)