Skip to content

Commit

Permalink
feat: add the core login for the action
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshCasper committed May 13, 2022
1 parent e70adea commit 318a824
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.7

RUN pip install --no-cache-dir requests

COPY . .

CMD [ "python", "/main.py"]
20 changes: 20 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Validate Issues over Pull Requests"
description: "Checks for a valid Issue number linked with the PR"
author: "Harsh Mishra"

inputs:
prbody:
description: "Body of the Pull Request to search for related issues"
required: true

prurl:
description: "URL of the Pull Request"
required: true

outputs:
valid:
description: "code for a valid issue (0=Invalid, 1=Valid)"

runs:
using: "docker"
image: "Dockerfile"
65 changes: 65 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import re

import requests


def main():
"""
Function to validate the Issue present over the Pull Request body.
"""

pattern = "#\d+"

# Get the PR body and the PR URL
body = os.getenv("INPUT_PRBODY")
url = os.getenv("INPUT_PRURL")
print(body)
print(url)

# Get the Issue number
try:
issue_num = re.search(pattern, body)[0].replace("#", "")
print(issue_num)
except:
issue_num = "No issue number"

# URL List will match: ['https:', '', 'api.github.com', 'repos', 'owner', 'repo-name']
url = url.split("/")[:-2]
url[2] = url[2].replace("api.", "")

# Get rid of the repos record
url.pop(3)

# Reattach the URL pieces
url = "/".join(url)

# Add the issue number to the URL
url += "/issues/{}".format(issue_num)
print(url)

# Check if its valid code
valid_code = 0
response = requests.get(url)
if response.status_code == 200:
if response.url == url:
# Check if issue is open or closed
text = response.text
pattern_issue = "Status:\s(\w+)"
if re.search(pattern_issue, text)[1] == "Open":
valid_code = 1
else:
print("Issue is closed")
else:
print(
"Invalid Response Code obtained - error code: {}".format(
response.status_code
)
)

print("Valid flag is:", valid_code)
print(f"::set-output name=valid::{valid_code}")


if __name__ == "__main__":
main()

0 comments on commit 318a824

Please sign in to comment.