Skip to content

Commit e4fbe01

Browse files
authored
Added GitHub actions to validate issue number in PR bodies (#1001)
* Added GitHub actions to validate issue number in PR bodies * Added requested changes * Added picture link to the invalid-comment body
1 parent c438640 commit e4fbe01

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3
2+
3+
RUN pip install --no-cache-dir requests
4+
5+
COPY . .
6+
7+
CMD [ "python", "/main.py"]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: "PR_has_a_valid_issue"
2+
3+
description: "Checks for a valid Issue number linked with the PR"
4+
5+
inputs:
6+
prbody:
7+
description: "The body of the PR to search for related issues"
8+
required: true
9+
10+
prurl:
11+
description: "URL of the Pull Request"
12+
required: true
13+
14+
outputs:
15+
valid:
16+
description: "code for a valid issue (0=Invalid, 1=Valid)"
17+
18+
runs:
19+
using: "docker"
20+
image: "Dockerfile"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os, re, requests
2+
3+
# Used pattern to search for Issues.
4+
pattern = "#\d+"
5+
6+
# PR body
7+
body = os.getenv("INPUT_PRBODY")
8+
# PR URL
9+
url = os.getenv("INPUT_PRURL")
10+
11+
issue_num = re.search(pattern, body)[0].replace("#", "")
12+
13+
# url list will be something like this
14+
# ['https:', '', 'api.github.com', 'repos', 'owner', 'repo-name']
15+
# Split URL using slashes
16+
url = url.split("/")[:-2]
17+
# Replace API URL with HTML URL
18+
url[2] = url[2].replace("api.", "")
19+
# Get rid of "repos" record
20+
url.pop(3)
21+
# Reattach URL pieces
22+
url = "/".join(url)
23+
# Add issue number
24+
url += "/issues/{}".format(issue_num)
25+
26+
# Is valid code
27+
valid_code = 0
28+
response = requests.get(url)
29+
# Check if not a 404 page
30+
if response.status_code == 200:
31+
print("status code is 200")
32+
# Check if not redirected to a pull request page
33+
if response.url == url:
34+
print("URLS are matched")
35+
# Check if Issue is Open not Closed
36+
text = response.text
37+
pattern_issue = "Status:\s(\w+)"
38+
if re.search(pattern_issue, text)[1] == "Open":
39+
valid_code = 1
40+
else:
41+
print("Couldn't find Open flag")
42+
else:
43+
print("Invalid Response Code obtained - error code: {}".format(response.status_code))
44+
45+
print("Valid flag is:", valid_code)
46+
47+
print(f"::set-output name=valid::{valid_code}")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: PR has a valid Issue?
2+
3+
on:
4+
pull_request:
5+
types: [ edited, synchronize, opened, reopened ]
6+
7+
jobs:
8+
checker:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
13+
- name: "Issue number validator"
14+
uses: ./.github/actions/PR_has_a_valid_issue/
15+
id: validator
16+
with:
17+
prbody: ${{ github.event.pull_request.body }}
18+
prurl: ${{ github.event.pull_request.url }}
19+
20+
- name: "Validation action on 1"
21+
if: ${{ steps.validator.outputs.valid == 1 }}
22+
run: |
23+
echo $Github_Token > mytoken.txt
24+
gh auth login --with-token < mytoken.txt
25+
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
26+
gh pr edit $PRNUM --remove-label "PR:No-Issue"
27+
rm mytoken.txt
28+
env:
29+
Github_Token: ${{ secrets.GITHUB_TOKEN }}
30+
PRNUM: ${{ github.event.pull_request.number }}
31+
32+
- name: "Validation action on 0"
33+
if: ${{ steps.validator.outputs.valid == 0 }}
34+
run: |
35+
echo $Github_Token > mytoken.txt
36+
gh auth login --with-token < mytoken.txt
37+
gh pr comment $PRNUM --body "PR is not linked to any issue, please make the corresponding changes in the body. The issue should look like [this](https://ibb.co/Bg9x53w). For help follow this [link](https://github.com/HarshCasper/Rotten-Scripts/blob/master/CONTRIBUTING.md)"
38+
gh pr edit $PRNUM --add-label "PR:No-Issue"
39+
rm mytoken.txt
40+
env:
41+
Github_Token: ${{ secrets.GITHUB_TOKEN }}
42+
PRNUM: ${{ github.event.pull_request.number }}

0 commit comments

Comments
 (0)