From 9a1caf7f6356bf393c4354db3d4b1157567b0256 Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Thu, 7 Sep 2023 18:19:03 -0400 Subject: [PATCH 1/8] Adds automation script to add issue label Signed-off-by: Darshit Chanpura --- .github-automation/copy-issue-labels-to-pr.py | 67 +++++++++++++++++++ .../workflows/apply-issue-labels-to-pr.yml | 18 +++++ 2 files changed, 85 insertions(+) create mode 100644 .github-automation/copy-issue-labels-to-pr.py create mode 100644 .github/workflows/apply-issue-labels-to-pr.yml diff --git a/.github-automation/copy-issue-labels-to-pr.py b/.github-automation/copy-issue-labels-to-pr.py new file mode 100644 index 0000000000000..2dd23bb278b53 --- /dev/null +++ b/.github-automation/copy-issue-labels-to-pr.py @@ -0,0 +1,67 @@ +#!/usr/bin/python3 +# SPDX-License-Identifier: Apache-2.0 + +import itertools +import json +import re +import sys +from github import Github + +# We don't want to copy all labels on linked issues; only those in this subset. +COPYABLE_LABELS = { + "enhancement", + "documentation" +} + +# Returns a pull request extracted from Github's event JSON. +def get_pr(event): + # --- Extract PR from event JSON --- + # `check_run`/`check_suite` does not include any direct reference to the PR + # that triggered it in its event JSON. We have to extrapolate it using the head + # SHA that *is* there. + + # Get head SHA from event JSON + pr_head_sha = event["check_suite"]["head_sha"] + + # Find the repo PR that matches the head SHA we found + return {pr.head.sha: pr for pr in repo.get_pulls()}[pr_head_sha] + +# Get all issue numbers related to a PR. +def get_related_issues(pr): + # Regex to pick out closing keywords. + regex = re.compile("(close[sd]?|fix|fixe[sd]?|resolve[sd]?)\s*:?\s+#(\d+)", re.I) + + # Extract all associated issues from closing keyword in PR + for verb, num in regex.findall(pr.body): + yield int(num) + + # Extract all associated issues from PR commit messages + for c in pr.get_commits(): + for verb, num in regex.findall(c.commit.message): + yield int(num) + +# Get inputs from shell +(token, repository, path) = sys.argv[1:4] + +# Initialize repo +repo = Github(token).get_repo(repository) + +# Open Github event JSON +with open(path) as f: + event = json.load(f) + +# Get the PR we're working on. +pr = get_pr(event) +pr_labels = {label.name for label in pr.labels} + +# Get every label on every linked issue. +issues = get_related_issues(pr) +issues_labels = [repo.get_issue(n).labels for n in issues] +issues_labels = {l.name for l in itertools.chain(*issues_labels)} + +# Find the set of all labels we want to copy that aren't already set on the PR. +unset_labels = COPYABLE_LABELS & issues_labels - pr_labels + +# If there are any labels we need to add, add them. +if len(unset_labels) > 0: + pr.set_labels(*list(unset_labels)) diff --git a/.github/workflows/apply-issue-labels-to-pr.yml b/.github/workflows/apply-issue-labels-to-pr.yml new file mode 100644 index 0000000000000..575e52427e9ff --- /dev/null +++ b/.github/workflows/apply-issue-labels-to-pr.yml @@ -0,0 +1,18 @@ +name: Apply issue labels to PR + +on: + pull_request_target: + types: [opened, reopened, synchronize, converted_to_draft, edited] + check_suite: + types: [ requested, rerequested, completed ] + +jobs: + mirror-labels-to-pr: +# if: github.repository == 'opensearch-project/OpenSearch' + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v3 + + - name: Run label script + run: ../../.github-automation/copy-issue-labels-to-pr.py ${{ secrets.GITHUB_TOKEN }} $GITHUB_REPOSITORY $GITHUB_EVENT_PATH From 37df75bc3f30451bdd50c57e0aaf8aa32aef80c9 Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Thu, 7 Sep 2023 18:53:06 -0400 Subject: [PATCH 2/8] Fixes script to pull the correct sha Signed-off-by: Darshit Chanpura --- .github/workflows/apply-issue-labels-to-pr.yml | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/.github/workflows/apply-issue-labels-to-pr.yml b/.github/workflows/apply-issue-labels-to-pr.yml index 575e52427e9ff..b993118582665 100644 --- a/.github/workflows/apply-issue-labels-to-pr.yml +++ b/.github/workflows/apply-issue-labels-to-pr.yml @@ -1,18 +1,15 @@ -name: Apply issue labels to PR +name: Apply all issue labels to PR on: + pull_request: pull_request_target: - types: [opened, reopened, synchronize, converted_to_draft, edited] - check_suite: - types: [ requested, rerequested, completed ] jobs: mirror-labels-to-pr: -# if: github.repository == 'opensearch-project/OpenSearch' + # if: github.repository == 'opensearch-project/OpenSearch' runs-on: ubuntu-latest steps: - - name: Checkout repo - uses: actions/checkout@v3 + - uses: actions/checkout@v3 - name: Run label script - run: ../../.github-automation/copy-issue-labels-to-pr.py ${{ secrets.GITHUB_TOKEN }} $GITHUB_REPOSITORY $GITHUB_EVENT_PATH + run: pip install PyGithub && .github/copy-issue-labels-to-pr.py ${{ secrets.GITHUB_TOKEN }} $GITHUB_REPOSITORY $GITHUB_EVENT_PATH From 60209355375efa216f14db35901b2171b1d9d2c4 Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Thu, 7 Sep 2023 18:55:29 -0400 Subject: [PATCH 3/8] Fixes command path Signed-off-by: Darshit Chanpura --- .github/workflows/apply-issue-labels-to-pr.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/apply-issue-labels-to-pr.yml b/.github/workflows/apply-issue-labels-to-pr.yml index b993118582665..34a4206c17034 100644 --- a/.github/workflows/apply-issue-labels-to-pr.yml +++ b/.github/workflows/apply-issue-labels-to-pr.yml @@ -12,4 +12,4 @@ jobs: - uses: actions/checkout@v3 - name: Run label script - run: pip install PyGithub && .github/copy-issue-labels-to-pr.py ${{ secrets.GITHUB_TOKEN }} $GITHUB_REPOSITORY $GITHUB_EVENT_PATH + run: pip install PyGithub && .github-automation/copy-issue-labels-to-pr.py ${{ secrets.GITHUB_TOKEN }} $GITHUB_REPOSITORY $GITHUB_EVENT_PATH From 378d7ff09932a5a58c962c83a57d48da9beb8d84 Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Thu, 7 Sep 2023 18:57:39 -0400 Subject: [PATCH 4/8] Makes script executable Signed-off-by: Darshit Chanpura --- .github-automation/copy-issue-labels-to-pr.py | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) mode change 100644 => 100755 .github-automation/copy-issue-labels-to-pr.py diff --git a/.github-automation/copy-issue-labels-to-pr.py b/.github-automation/copy-issue-labels-to-pr.py old mode 100644 new mode 100755 index 2dd23bb278b53..b9156c4bd5b1b --- a/.github-automation/copy-issue-labels-to-pr.py +++ b/.github-automation/copy-issue-labels-to-pr.py @@ -15,30 +15,30 @@ # Returns a pull request extracted from Github's event JSON. def get_pr(event): - # --- Extract PR from event JSON --- - # `check_run`/`check_suite` does not include any direct reference to the PR - # that triggered it in its event JSON. We have to extrapolate it using the head - # SHA that *is* there. + # --- Extract PR from event JSON --- + # `check_run`/`check_suite` does not include any direct reference to the PR + # that triggered it in its event JSON. We have to extrapolate it using the head + # SHA that *is* there. - # Get head SHA from event JSON - pr_head_sha = event["check_suite"]["head_sha"] + # Get head SHA from event JSON + pr_head_sha = event["pull_request"]["head"]["sha"] - # Find the repo PR that matches the head SHA we found - return {pr.head.sha: pr for pr in repo.get_pulls()}[pr_head_sha] + # Find the repo PR that matches the head SHA we found + return {pr.head.sha: pr for pr in repo.get_pulls()}[pr_head_sha] # Get all issue numbers related to a PR. def get_related_issues(pr): - # Regex to pick out closing keywords. - regex = re.compile("(close[sd]?|fix|fixe[sd]?|resolve[sd]?)\s*:?\s+#(\d+)", re.I) + # Regex to pick out closing keywords. + regex = re.compile("(close[sd]?|fix|fixe[sd]?|resolve[sd]?)\s*:?\s+#(\d+)", re.I) - # Extract all associated issues from closing keyword in PR - for verb, num in regex.findall(pr.body): - yield int(num) + # Extract all associated issues from closing keyword in PR + for verb, num in regex.findall(pr.body): + yield int(num) - # Extract all associated issues from PR commit messages - for c in pr.get_commits(): - for verb, num in regex.findall(c.commit.message): - yield int(num) + # Extract all associated issues from PR commit messages + for c in pr.get_commits(): + for verb, num in regex.findall(c.commit.message): + yield int(num) # Get inputs from shell (token, repository, path) = sys.argv[1:4] @@ -48,7 +48,7 @@ def get_related_issues(pr): # Open Github event JSON with open(path) as f: - event = json.load(f) + event = json.load(f) # Get the PR we're working on. pr = get_pr(event) @@ -64,4 +64,4 @@ def get_related_issues(pr): # If there are any labels we need to add, add them. if len(unset_labels) > 0: - pr.set_labels(*list(unset_labels)) + pr.set_labels(*list(unset_labels)) From 80088d3f93fa7be919fc14d116805eedfc33f849 Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Fri, 8 Sep 2023 11:17:16 -0400 Subject: [PATCH 5/8] Uses a preexisting workflow Signed-off-by: Darshit Chanpura --- .../workflows/apply-issue-labels-to-pr.yml | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/.github/workflows/apply-issue-labels-to-pr.yml b/.github/workflows/apply-issue-labels-to-pr.yml index 34a4206c17034..6160881cf1441 100644 --- a/.github/workflows/apply-issue-labels-to-pr.yml +++ b/.github/workflows/apply-issue-labels-to-pr.yml @@ -4,12 +4,22 @@ on: pull_request: pull_request_target: +#jobs: +# mirror-labels-to-pr: +# # if: github.repository == 'opensearch-project/OpenSearch' +# runs-on: ubuntu-latest +# steps: +# - uses: actions/checkout@v3 +# +# - name: Run label script +# run: pip install PyGithub && .github-automation/copy-issue-labels-to-pr.py ${{ secrets.GITHUB_TOKEN }} $GITHUB_REPOSITORY $GITHUB_EVENT_PATH + jobs: - mirror-labels-to-pr: - # if: github.repository == 'opensearch-project/OpenSearch' + copy-labels: runs-on: ubuntu-latest + name: Copy labels from linked issues steps: - - uses: actions/checkout@v3 - - - name: Run label script - run: pip install PyGithub && .github-automation/copy-issue-labels-to-pr.py ${{ secrets.GITHUB_TOKEN }} $GITHUB_REPOSITORY $GITHUB_EVENT_PATH + - name: copy-labels + uses: michalvankodev/copy-issue-labels@v1.2.1 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} From 2c0795f809b80aa3a3303884e71d13c668dea326 Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Fri, 8 Sep 2023 14:12:38 -0400 Subject: [PATCH 6/8] Uses a different pre-existing workflow Signed-off-by: Darshit Chanpura --- .github-automation/copy-issue-labels-to-pr.py | 67 ------------------- .../workflows/apply-issue-labels-to-pr.yml | 16 ++--- 2 files changed, 5 insertions(+), 78 deletions(-) delete mode 100755 .github-automation/copy-issue-labels-to-pr.py diff --git a/.github-automation/copy-issue-labels-to-pr.py b/.github-automation/copy-issue-labels-to-pr.py deleted file mode 100755 index b9156c4bd5b1b..0000000000000 --- a/.github-automation/copy-issue-labels-to-pr.py +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/python3 -# SPDX-License-Identifier: Apache-2.0 - -import itertools -import json -import re -import sys -from github import Github - -# We don't want to copy all labels on linked issues; only those in this subset. -COPYABLE_LABELS = { - "enhancement", - "documentation" -} - -# Returns a pull request extracted from Github's event JSON. -def get_pr(event): - # --- Extract PR from event JSON --- - # `check_run`/`check_suite` does not include any direct reference to the PR - # that triggered it in its event JSON. We have to extrapolate it using the head - # SHA that *is* there. - - # Get head SHA from event JSON - pr_head_sha = event["pull_request"]["head"]["sha"] - - # Find the repo PR that matches the head SHA we found - return {pr.head.sha: pr for pr in repo.get_pulls()}[pr_head_sha] - -# Get all issue numbers related to a PR. -def get_related_issues(pr): - # Regex to pick out closing keywords. - regex = re.compile("(close[sd]?|fix|fixe[sd]?|resolve[sd]?)\s*:?\s+#(\d+)", re.I) - - # Extract all associated issues from closing keyword in PR - for verb, num in regex.findall(pr.body): - yield int(num) - - # Extract all associated issues from PR commit messages - for c in pr.get_commits(): - for verb, num in regex.findall(c.commit.message): - yield int(num) - -# Get inputs from shell -(token, repository, path) = sys.argv[1:4] - -# Initialize repo -repo = Github(token).get_repo(repository) - -# Open Github event JSON -with open(path) as f: - event = json.load(f) - -# Get the PR we're working on. -pr = get_pr(event) -pr_labels = {label.name for label in pr.labels} - -# Get every label on every linked issue. -issues = get_related_issues(pr) -issues_labels = [repo.get_issue(n).labels for n in issues] -issues_labels = {l.name for l in itertools.chain(*issues_labels)} - -# Find the set of all labels we want to copy that aren't already set on the PR. -unset_labels = COPYABLE_LABELS & issues_labels - pr_labels - -# If there are any labels we need to add, add them. -if len(unset_labels) > 0: - pr.set_labels(*list(unset_labels)) diff --git a/.github/workflows/apply-issue-labels-to-pr.yml b/.github/workflows/apply-issue-labels-to-pr.yml index 6160881cf1441..dff104402aaf2 100644 --- a/.github/workflows/apply-issue-labels-to-pr.yml +++ b/.github/workflows/apply-issue-labels-to-pr.yml @@ -4,22 +4,16 @@ on: pull_request: pull_request_target: -#jobs: -# mirror-labels-to-pr: -# # if: github.repository == 'opensearch-project/OpenSearch' -# runs-on: ubuntu-latest -# steps: -# - uses: actions/checkout@v3 -# -# - name: Run label script -# run: pip install PyGithub && .github-automation/copy-issue-labels-to-pr.py ${{ secrets.GITHUB_TOKEN }} $GITHUB_REPOSITORY $GITHUB_EVENT_PATH - jobs: copy-labels: runs-on: ubuntu-latest name: Copy labels from linked issues steps: - name: copy-labels - uses: michalvankodev/copy-issue-labels@v1.2.1 + uses: DarshitChanpura/copy-issue-labels@v1.0.0 with: repo-token: ${{ secrets.GITHUB_TOKEN }} + labels-to-exclude: | + untriaged + triaged + From d0071e539ccb43aa859d01ab004cc169f1049b82 Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Fri, 8 Sep 2023 14:29:58 -0400 Subject: [PATCH 7/8] Limits the workflow run to OpenSearch core Signed-off-by: Darshit Chanpura --- .github/workflows/apply-issue-labels-to-pr.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/apply-issue-labels-to-pr.yml b/.github/workflows/apply-issue-labels-to-pr.yml index dff104402aaf2..42ec7f7b3a0e8 100644 --- a/.github/workflows/apply-issue-labels-to-pr.yml +++ b/.github/workflows/apply-issue-labels-to-pr.yml @@ -6,6 +6,7 @@ on: jobs: copy-labels: + if: github.repository == 'opensearch-project/OpenSearch' runs-on: ubuntu-latest name: Copy labels from linked issues steps: From ea636b44372f0193d89386ec6120cd895cd6d695 Mon Sep 17 00:00:00 2001 From: Darshit Chanpura Date: Fri, 8 Sep 2023 14:35:12 -0400 Subject: [PATCH 8/8] Changes workflow name Signed-off-by: Darshit Chanpura --- .github/workflows/apply-issue-labels-to-pr.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/apply-issue-labels-to-pr.yml b/.github/workflows/apply-issue-labels-to-pr.yml index 42ec7f7b3a0e8..6d864576c388f 100644 --- a/.github/workflows/apply-issue-labels-to-pr.yml +++ b/.github/workflows/apply-issue-labels-to-pr.yml @@ -1,4 +1,4 @@ -name: Apply all issue labels to PR +name: Copy issue labels to PR on: pull_request: