Skip to content

Commit

Permalink
Merge pull request #50 from iamsobanjaved/iamsobanjaved/upgrade-prs-a…
Browse files Browse the repository at this point in the history
…uto-merge

build: add a workflow to merge upgrade PRs
  • Loading branch information
Feanil Patel authored Feb 5, 2023
2 parents 93040a1 + 0c3c7de commit a65e26e
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
90 changes: 90 additions & 0 deletions .github/workflows/merge-python-requirements-upgrade-prs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
name: Merge Python Requirements Upgrade PRs

on:
schedule:
# will run the job at 10 AM every day (UTC),
# https://crontab.guru/#0_10_*_*_*
- cron: "0 10 * * *"

jobs:
get_list_of_prs:
name: Get list of PRs
runs-on: ubuntu-20.04
outputs:
urls: ${{ steps.generate_urls.outputs.urls }}
steps:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.8'

- name: Checkout repo
uses: actions/checkout@v3

- name: Install dependencies
run: pip install requests

- name: Get urls of all eligible prs
env:
GIT_TOKEN: ${{ secrets.requirements_bot_github_token }}
id: generate_urls
run: |
echo "urls=$(python scripts/get_ready_to_merge_prs.py)" >> $GITHUB_OUTPUT
merge_pr:
runs-on: ubuntu-20.04
needs:
- get_list_of_prs
strategy:
fail-fast: false
matrix:
url: ${{ fromJSON(needs.get_list_of_prs.outputs.urls) }}

steps:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.8'

- name: Checkout tools repo
uses: actions/checkout@v3
with:
repository: openedx/tubular
path: tubular

- name: Setup tubular
run: |
cd tubular
pip install -e .
- name: Extract parameters from URL
run: |
url="${{ matrix.url }}"
# Extract the parameters
org_name=$(echo $url | awk -F/ '{print $4}')
repo_name=$(echo $url | awk -F/ '{print $5}')
pr_number=$(echo $url | awk -F/ '{print $7}')
# store for next steps
echo "org_name=${org_name}" >> $GITHUB_ENV
echo "repo_name=${repo_name}" >> $GITHUB_ENV
echo "pr_number=${pr_number}" >> $GITHUB_ENV
#print out
echo "Organization: $org_name"
echo "Repo: $repo_name"
echo "PR: $pr_number"
- name: Check test status of pr
env:
GIT_TOKEN: ${{ secrets.requirements_bot_github_token }}
run: |
check_pr_tests_status.py --org "$org_name" --repo "$repo_name" --pr_number "$pr_number" \
--exclude-contexts 'Renovate|[Cc]odecov|Dependabot|edx-platform-ci' --all-checks
- name: Merge pull request
env:
GIT_TOKEN: ${{ secrets.requirements_bot_github_token }}
run: |
merge_pull_request --org "${{ env.org_name }}" --repo "${{ env.repo_name }}" --pr_number
81 changes: 81 additions & 0 deletions scripts/get_ready_to_merge_prs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#! /usr/bin/env python3

"""
Command-line script to get open prs of both organizations with label 'Ready to merge'
"""
import json
import logging
import os
import sys

import requests


LOG = logging.getLogger(__file__)

GIT_API_URL = 'https://api.github.com/search/issues?per_page=100'
ORGS = ['edx', 'openedx']


def get_ready_to_merge_prs():
"""
get a list of all prs which are open and have a label "Ready to merge" in organization.
Args:
org (str):
token (str):
Returns:
list of all prs.
"""
token = os.environ.get('GIT_TOKEN')
if not token:
LOG.error('GIT_TOKEN is missing from environment variables')
sys.exit(1)
urls = []
for org in ORGS:
urls.extend(get_github_api_response(org, token))
print(json.dumps(urls))
return urls


def get_github_api_response(org, token):
"""
get github pull requests
https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests
"""
params = f'q=is:pr is:open label:"Ready to merge" org:{org}'
headers = {
'Accept': "application/vnd.github.antiope-preview+json",
'Authorization': "bearer {token}".format(token=token),
}
data = []

try:
resp = requests.get(GIT_API_URL, params=params, headers=headers)
if resp.status_code == 200:
data = resp.json()
data = [item['html_url'] for item in data['items']]
else:
LOG.error(
'api return status code {code} and error {con}'.format(code=resp.status_code, con=resp.content)
)
sys.exit(1)

except Exception as err:
LOG.error('Github api throws error: {con}'.format(con=str(err)))
sys.exit(1)

return data


def parse_urls(data):
"""
parse data to return only org, repo and pull request number
"""
raw_data = data.replace('https://github.com/', '').split('/')
return raw_data[0], raw_data[1], raw_data[3]


if __name__ == "__main__":
get_ready_to_merge_prs()

0 comments on commit a65e26e

Please sign in to comment.