Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from Ant0wan/key
Browse files Browse the repository at this point in the history
test
  • Loading branch information
Ant0wan authored May 20, 2023
2 parents 6dc0e4e + 920e1bc commit 110baac
Show file tree
Hide file tree
Showing 10 changed files with 397 additions and 31 deletions.
23 changes: 11 additions & 12 deletions .github/workflows/test_action.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
name: Test Action
on: [push]
name: Test
on:
push:

permissions:
pull-requests: write

jobs:
test:
runs-on: ubuntu-latest
name: Display the generated PR description
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Generate pr description
- name: Generate a PR Description
id: pr
uses: ./ # Uses an action in the root directory
# or use a released GitHub Action
# uses: shipyard/github-action/fetch-shipyard-env@1.0.0
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GH_TOKEN: ${{ secrets.GH_TOKEN }}
#- name: Print generated pr description
# run: echo "${{ steps.pr.outputs.text }}"
uses: ./
with:
api-key: ${{ secrets.OPENAI_API_KEY }}
github-token: ${{ secrets.GITHUB_TOKEN }}
11 changes: 4 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,20 @@ inputs:
github-token:
description: 'Github Token'
required: true
default: ""
#outputs:
# text:
# description: 'Text in the pull request description'
# value: ${{ steps.pr.outputs.text }}
runs:
using: 'composite'
steps:
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Upgrade Pip
run: pip install --upgrade pip
shell: bash
- name: Install Dependencies
run: pip install -r requirements.txt
shell: bash
- name: Generate description
id: pr
run: python src/main.py
shell: bash
env:
Expand All @@ -53,4 +50,4 @@ runs:
INPUT_HEADER: ${{ inputs.header }}
INPUT_MODEL: ${{ inputs.model }}
OPENAI_API_KEY: ${{ inputs.api-key }}
GH_TOKEN: ${{ inputs.github-token }}
GITHUB_TOKEN: ${{ inputs.github-token }}
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ preflights:
- INPUT_HEADER
- INPUT_MODEL
- OPENAI_API_KEY
- GH_TOKEN
- GITHUB_TOKEN
logs:
logging: 'configuration/logging.conf'
profile: action # choose(dev|action)
29 changes: 25 additions & 4 deletions src/GitHub/outputs.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
"""
This module provides functions for setting outputs in GitHub Actions or logging to the terminal in CLI mode.
The module includes a function, 'set_action_outputs', that sets the GitHub Action outputs if running as a
GitHub Action. If running in CLI mode, the function logs the outputs to the terminal. Note that if the CLI
mode is used within a GitHub Actions workflow, it will be treated the same as GitHub Actions mode.
Example usage:
--------------
output_pairs = {
'output_key1': 'output_value1',
'output_key2': 'output_value2'
}
set_action_outputs(output_pairs)
"""
import os
import logging


def set_action_outputs(output_pairs):
"""Sets the GitHub Action outputs if running as a GitHub Action,
and otherwise logs these to terminal if running in CLI mode. Note
"""
Sets the GitHub Action outputs if running as a GitHub Action,
and otherwise logs these to the terminal if running in CLI mode. Note
that if the CLI mode is used within a GitHub Actions
workflow, it will be treated the same as GitHub Actions mode.
Keyword arguments:
output_pairs - Dictionary of outputs with values
Args:
output_pairs (dict): Dictionary of outputs with values.
Returns:
None
"""
if "GITHUB_OUTPUT" in os.environ:
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
Expand Down
120 changes: 120 additions & 0 deletions src/GitHub/pullrequest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,80 @@
"""
This module provides a class for interacting with GitHub Pull Requests.
The module includes a class, 'PullRequest', that can be used to work with GitHub Pull Requests. The 'PullRequest'
class requires a GitHub token for authentication. The class provides methods for retrieving the repository URL,
fetching the repository, getting the current branch, retrieving open pull requests for the branch, getting the
diff of a pull request, and updating the description of a pull request.
Example usage:
--------------
github_token = "your_github_token"
pull_request = PullRequest(github_token)
# Print information about the pull request
print(pull_request)
# Get the diff of the pull request
diff = pull_request.diff()
# Update the description of the pull request
new_description = "Updated pull request description"
pull_request.update_description(new_description)
"""
import requests
import subprocess
import logging
from github import Github


class PullRequest:
"""
A class for interacting with GitHub Pull Requests.
The 'PullRequest' class requires a GitHub token for authentication. It provides methods for retrieving the
repository URL, fetching the repository, getting the current branch, retrieving open pull requests for the
branch, getting the diff of a pull request, and updating the description of a pull request.
Example usage:
--------------
github_token = "your_github_token"
pull_request = PullRequest(github_token)
# Print information about the pull request
print(pull_request)
# Get the diff of the pull request
diff = pull_request.diff()
# Update the description of the pull request
new_description = "Updated pull request description"
pull_request.update_description(new_description)
"""

def __init__(self, github_token: str):
"""
Initialize the PullRequest object with the provided GitHub token.
Args:
github_token (str): The GitHub token for authentication.
Returns:
None
"""
self.__g = Github(github_token)
self.__url = self._repository_url()
self.__repository = self._repository(self.__g, self.__url)
self.__branch = self._branch()
self.__pulls = self._pulls(self.__repository, self.__branch)

def __str__(self):
"""
Return a string representation of the PullRequest object.
Returns:
str: A string representation of the PullRequest object.
"""
return f"Github object: {self.__g}, \
Url: {self.__url}, \
Repository: {self.__repository}, \
Expand All @@ -22,24 +83,65 @@ def __str__(self):

@staticmethod
def _repository_url():
"""
Get the URL of the repository.
Returns:
str: The URL of the repository.
Raises:
subprocess.CalledProcessError: If the 'git' command fails.
"""
remote = subprocess.check_output(
['git', 'config', '--get', 'remote.origin.url']).decode().strip()
return remote.replace(".git", "")

@staticmethod
def _repository(g, url):
"""
Fetch the repository object from GitHub.
Args:
g (Github): The GitHub object for authentication.
url (str): The URL of the repository.
Returns:
Repository: The fetched repository object.
Raises:
IndexError: If the URL is not valid.
"""
parts = url.split('/')
owner = parts[-2]
repo = parts[-1].rstrip('.git')
return g.get_repo(f'{owner}/{repo}')

@staticmethod
def _branch():
"""
Get the current branch name.
Returns:
str: The current branch name.
Raises:
subprocess.CalledProcessError: If the 'git' command fails.
"""
return subprocess.check_output(
['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip().decode('utf-8')

@staticmethod
def _pulls(repo, branch):
"""
Retrieve open pull requests for the specified branch.
Args:
repo (Repository): The repository object.
branch (str): The branch name.
Returns:
PullRequest: The first open pull request for the branch, or None if no open pull requests exist.
"""
pull_requests = repo.get_pulls(
state='open', head=f'{repo.owner.login}:{branch}')
if pull_requests.totalCount == 0:
Expand All @@ -49,9 +151,27 @@ def _pulls(repo, branch):
return pr

def diff(self):
"""
Get the diff of the pull request.
Returns:
str: The diff of the pull request.
Raises:
requests.exceptions.RequestException: If the request to retrieve the diff fails.
"""
diff_url = self.__pulls.diff_url
response = requests.get(diff_url)
return response.text

def update_description(self, new_description):
"""
Update the description of the pull request.
Args:
new_description (str): The new description for the pull request.
Returns:
None
"""
self.__pulls.edit(body=new_description)
Loading

0 comments on commit 110baac

Please sign in to comment.