Skip to content

Commit

Permalink
Update labeler workflow (#9)
Browse files Browse the repository at this point in the history
* Update labeler workflow

Signed-off-by: RitheeshBaradwaj <[email protected]>

* Remove the usage of action/bin

Signed-off-by: RitheeshBaradwaj <[email protected]>

* use latest action/github-script and enable lint checking for PR

* remove using set-env

* fix pylint workflow to skip the steps if nothing to commit

* fix setting en variables

* fix build

* add py script to find matching labels

* fix typos and update validation checks

* fix issues with lint and label workflows

* set env var correctly

* fix py error

* fix build

* fix build

* fix build

* fix build

* fix build

* fix build

* fix build

* fix build

* fix build

* fix build

* fix build

* fix build

* fix build

* generate pylint reports

* fix build

* fix build

* refactor label workflow

* fix workflows

* fix build

* fix build

* fix build

* fix build

* fix build

* Fix Pylint errors with Black

* fix build now

* Fix Pylint errors with Black

* fix

* Check environment

Signed-off-by: RitheeshBaradwaj <[email protected]>

* Fix Pylint errors with Black

* fix

* fix

* fix

* fix

* fix

* Fix Pylint errors with Black

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* Read secret variable for labels workflow

* Revert pylint workflow

* Fix Pylint errors with Black

* Remove extra line

Signed-off-by: RitheeshBaradwaj <[email protected]>

* fix commenting issue

* fix commenting issue

* fix commenting issue

* fix commenting issue

* fix commenting issue

* fix commenting issue

* fix commenting issue

* fix commenting issue

---------

Signed-off-by: RitheeshBaradwaj <[email protected]>
Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
RitheeshBaradwaj and actions-user authored Feb 5, 2023
1 parent 7d250ac commit 7e77cac
Show file tree
Hide file tree
Showing 21 changed files with 334 additions and 117 deletions.
73 changes: 73 additions & 0 deletions .github/add_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python3
import os
import sys
import requests

BRANCH_FORMATS = ["feature", "bug", "chore", "release", "documentation"]

# pull request details
PR_HEAD_REF = os.getenv("BRANCH_NAME") or os.getenv("GITHUB_HEAD_REF")
PR_TITLE = os.getenv("GITHUB_PULL_REQUEST_TITLE") or os.getenv("PR_TITLE") or ""
PR_BODY = os.getenv("GITHUB_PULL_REQUEST_BODY") or os.getenv("PR_BODY")
OWNER = os.getenv("GITHUB_REPOSITORY_OWNER") or os.getenv("OWNER")
REPO = os.getenv("GITHUB_REPOSITORY") or os.getenv("REPO")
PULL_REQUEST_NUMBER = os.getenv("GITHUB_PULL_REQUEST_NUMBER") or os.getenv("PR_NUMBER")
PR_TITLE = PR_TITLE.lower()
ACCESS_TOKEN = os.environ.get("MIMASA_ADD_LABELS") or os.environ.get("GITHUB_ACCESS_TOKEN")


def check_environment():
health_check = (
PR_HEAD_REF is not None
and PR_TITLE is not None
and OWNER is not None
and REPO is not None
and PULL_REQUEST_NUMBER is not None
)
return health_check


def get_labels_to_add():
"""Determine labels to add based on branch format, PR title, and PR body"""
labels_to_add = []
for label in BRANCH_FORMATS:
branch_format = label + "/"
if PR_HEAD_REF.startswith(branch_format) or " {label} " in PR_TITLE:
labels_to_add.append(label)

return list(set(labels_to_add))


def add_labels(labels_to_add):
"""Make API request to add labels to pull request"""
# Define the API endpoint for adding labels to a pull request
url = f"https://api.github.com/repos/{OWNER}/{REPO}/issues/{PULL_REQUEST_NUMBER}/labels"

# Set up the headers for the API request
headers = {
"Authorization": f"Token {ACCESS_TOKEN}",
"Accept": "application/vnd.github+json",
"Content-Type": "application/json",
}

# Make the API request to add labels to the pull request
labels_data = {"labels": labels_to_add}
response = requests.post(url, headers=headers, json=labels_data)

# Check if the request was successful
if response.status_code == 200:
print("Labels added successfully!")
else:
print(f"Failed to add labels: {response.text}")


def main():
final_labels = get_labels_to_add()
add_labels(final_labels)


if __name__ == "__main__":
if not check_environment:
sys.exit(0)

main()
2 changes: 2 additions & 0 deletions get_release_notes.py → .github/get_release_notes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import sys


def main(target_release):
# Open the input file and the output file
with open("CHANGELOG.md", "r") as input_file, open("release_notes.md", "w", encoding="utf-8") as output_file:
Expand All @@ -19,6 +20,7 @@ def main(target_release):
if in_target_release:
output_file.write(line)


if __name__ == "__main__":
target_release = sys.argv[1]
main(target_release)
5 changes: 5 additions & 0 deletions .github/labels_config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
feat feat
bug bug
documentation documentation
chore chore
release release
101 changes: 98 additions & 3 deletions .github/workflows/label.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,112 @@
name: Labeler
on: [pull_request, issues, issue_comment]

on:
issues:
types: [opened, edited]
pull_request:
types: [opened, edited, synchronize]

jobs:
label:
show-environment:
name: "Show environment variables"
runs-on: ubuntu-latest

steps:
- name: "Show environment variables"
run: |
env
add-label:
name: "Add labels to pull request & issues"
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
pull-requests: write

steps:
- uses: actions/labeler@v4
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.10.x

- name: Install dependencies
run: |
pip install requests
- name: Add label based on modified paths
uses: actions/labeler@v4
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
sync-labels: true

- name: Set branch name
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
branch_name="${{ github.event.pull_request.head.ref }}"
else
branch_name="${{ github.ref }}"
fi
echo "BRANCH_NAME=${branch_name}" >> $GITHUB_ENV
- name: Add label based on branch or PR title
if: github.event.pull_request
id: add_labels
env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_HEAD_REF: ${{ env.BRANCH_NAME }}
GITHUB_PULL_REQUEST_TITLE: ${{ github.event.pull_request.title }}
GITHUB_PULL_REQUEST_BODY: ${{ github.event.pull_request.body }}
GITHUB_PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }}
run: |
python .github/add_labels.py
# - name: Add label based on branch or PR title
# if: steps.add_labels.outputs.exit_code == 0 && github.event.pull_request
# uses: actions-ecosystem/action-add-labels@v1
# with:
# labels: ${{ env.LABELS }}
# echo "BRANCH_NAME=$(echo "${{ github.ref }}" | sed "s/refs\/heads\///")" >> $GITHUB_ENV
# echo "PR_TITLE=${{ github.event.pull_request.title }}" >> $GITHUB_ENV
# echo "PR_BODY=${{ github.event.pull_request.body }}" >> $GITHUB_ENV
# echo "OWNER=${{ github.repository_owner }}" >> $GITHUB_ENV
# echo "REPO=${{ github.repository }}" >> $GITHUB_ENV
# echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV

- name: Find suitable labels for Issues
if: github.event.issue
id: add_labels_for_issues
run: |
echo "ISSUE_TITLE=${{ github.event.issue.title }}" >> $GITHUB_ENV
echo "ISSUE_BODY=${{ github.event.issue.body }}" >> $GITHUB_ENV
echo "LABELS=$(
if [[ "${{ env.ISSUE_TITLE }}" =~ 'bug' ]] ||
[[ "${{ env.ISSUE_BODY }}" =~ 'bug' ]]; then
echo 'bug,'
else
echo ''
fi
if [[ "${{ env.ISSUE_TITLE }}" =~ 'enhancement' ]] ||
[[ "${{ env.ISSUE_BODY }}" =~ 'enhancement' ]]; then
echo 'enhancement,'
else
echo ''
fi
if [[ "${{ env.ISSUE_TITLE }}" =~ 'question' ]] ||
[[ "${{ env.ISSUE_BODY }}" =~ 'question' ]]; then
echo 'question'
else
echo ''
fi
)" >> $GITHUB_ENV
- name: Add labels for Issues
if: steps.add_labels_for_issues.outputs.exit_code == 0 && github.event.issue
uses: actions-ecosystem/action-add-labels@v1
with:
labels: "${{ env.LABELS }}"
71 changes: 48 additions & 23 deletions .github/workflows/pylint-auto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@ on:
push:
branches:
- '!main'
pull_request:

env:
NO_COMMIT: false

jobs:
fix_pylint_errors:
runs-on: ubuntu-latest

steps:
- name: Set branch name
run: |
if [ "${{ github.event_name }}" == "pull_request" ]; then
branch_name="${{ github.event.pull_request.head.ref }}"
else
branch_name="${{ github.ref }}"
fi
echo "BRANCH_NAME=${branch_name}" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
ref: ${{ env.BRANCH_NAME }}

- name: Set up Python
uses: actions/setup-python@v2
Expand All @@ -22,59 +35,71 @@ jobs:

- name: Install dependencies
run: |
pip install black pylint
pip install black pylint pylint-json2html
- name: Run Black
run: |
black .
- name: Check for Pylint errors
run: |
pylint --rcfile=.pylintrc --exit-zero --persistent=n $(git ls-files '*.py')
mkdir _pylint_reports
pylint --rcfile=.pylintrc --exit-zero --persistent=n --output-format=json:_pylint_reports/pylint_report.json,colorized --reports=yes $(git ls-files '*.py')
pylint-json2html -o _pylint_reports/pylint_report.html -e utf-8 _pylint_reports/pylint_report.json
- name: Check if changes are present
run: |
if [ -z "$(git status --porcelain)" ]; then
echo "No changes to commit."
exit 0
fi
- name: Upload pylint reports after fixing errors
uses: actions/upload-artifact@v2
with:
name: pylint_reports
path: _pylint_reports

- name: Download artifacts
uses: actions/download-artifact@v2
with:
name: pylint_reports

- name: Commit changes
id: commit_changes
run: |
git config --global user.email "[email protected]"
git config --global user.name "GitHub Action"
git add .
if [ -z "$(git diff-index --cached --quiet HEAD --)" ]; then
if [ -z "$(git status --porcelain)" ]; then
echo "No changes to commit."
echo "NO_COMMIT=true" >> $GITHUB_ENV
exit 0
fi
git commit -m "Fix Pylint errors with Black"
- name: Push changes
id: push_changes
if: ${{ env.NO_COMMIT != 'true' }}
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}
branch: ${{ env.BRANCH_NAME }}

- name: Comment on Pull Request
if: success()
if: (${{ env.NO_COMMIT }} != 'true') && (${{ github.event_name }} == 'pull_request')
uses: actions/github-script@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
script: |
const result = await github.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: context.sha,
});
if (result.data.length) {
const pullRequest = result.data[0];
await github.issues.createComment({
if (process.env.NO_COMMIT !== 'true') {
const result = await github.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
body: 'Linting errors observed and fixed 🎉. Please pull the latest changes!!!',
head: context.sha,
});
if (result.data.length) {
const pullRequest = result.data[0];
await github.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
body: 'Linting errors observed and fixed 🎉. Please pull the latest changes!!',
});
}
}
4 changes: 2 additions & 2 deletions .github/workflows/python-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ jobs:
if: ${{ github.ref == 'refs/heads/main' }}
uses: actions/upload-artifact@v2
with:
name: mimasa
name: mimasa-build
path: dist

- name: Download artifacts
if: ${{ github.ref == 'refs/heads/main' }}
uses: actions/download-artifact@v2
with:
name: mimasa
name: mimasa-build
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
- name: Get Changelog
run: |
version=$(echo "$(echo $GITHUB_REF | cut -d '/' -f 3 | cut -d 'v' -f 2)")
python get_release_notes.py $version
python .github/get_release_notes.py $version
- name: Create Release
id: create_release
Expand Down
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/


# Project Specific
data/datasets/**
data/models/**
Expand All @@ -363,3 +362,12 @@ dist/**

release_notes.txt
release_notes.md

pylint_report.html
pylint_report.json
_pylint_reports/**
**pylint_report**

mimasa-v*
Mimasa-*
mimasa-*
Loading

0 comments on commit 7e77cac

Please sign in to comment.