Skip to content

Commit

Permalink
Merge pull request #147 from kdvalin/ci/issue-labelling
Browse files Browse the repository at this point in the history
Add Issue Labelling based on PR state
  • Loading branch information
kdvalin authored Jan 21, 2025
2 parents 4896de9 + 6178fc7 commit 1db4a10
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/issue_tagging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Update parent issue

permissions:
issues: write

env:
ISSUE_STATE: pr_inprogress
REMOVE_ISSUES: --remove-label pr_approved --remove-label pr_review --remove-label pr_inprogress
GH_TOKEN: ${{ github.token }}

on:
issue_comment:
pull_request_target:
types:
- review_requested
pull_request_review:
types:
- submitted
jobs:
update_parent_issue:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Get PR number on issue type
if: github.event_name == 'pull_request_review'
run: echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> "$GITHUB_ENV"

- name: Get PR number on PR event
if: github.event_name == 'pull_request_target'
run: echo "PR_NUMBER=${{ github.event.number }}" >> "$GITHUB_ENV"

- name: Get parent issues
shell: bash
run: >
echo PARENT_ISSUES=$(
./ci/get_parent_issue.sh
$PR_NUMBER
) >> $GITHUB_ENV
- name: Fail when unable to find a parent issue
if: env.PARENT_ISSUE == ''
run: echo "Could not find a parent issue" && exit 1

- name: Get PR states
run: >
echo PR_STATUS=pr_$(
gh pr view $PR_NUMBER --json reviewRequests,latestReviews |
python ./ci/determine_status.py
) >> $GITHUB_ENV
- name: Set parent issues state
run: >
for issue in $PARENT_ISSUES; do
echo "Updating $issue to $PR_STATUS" &&
gh issue edit $issue $REMOVE_ISSUES &&
gh issue edit $issue --add-label=$PR_STATUS
done
8 changes: 8 additions & 0 deletions ci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Workflows in this repository

## Update parent issue
The idea behind this workflow is to keep Jira tickets in sync with the current status of their GitHub issue. A flowchart for how this works can be seen below.

![flow chart for PR labelling workflow](images/pr_labelling.jpg)

This workflow does not work with forked repositories, since the `GITHUB_TOKEN` provided by GitHub runner will not have write access to the base repository unless the pull request originated from the base repository.
26 changes: 26 additions & 0 deletions ci/determine_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3

import json

def _main(file):
data = json.load(file)

for review in data['latestReviews']:
if review['state'] == "CHANGES_REQUESTED":
return "inprogress"

if len(data['reviewRequests']) > 0:
return "review"

if len(data['latestReviews']) > 0:
return "approved"

return "inprogress" #No PR Requests and no reviews

if __name__ == "__main__":
import sys
in_file = sys.stdin
if len(sys.argv) > 1:
in_file = open(sys.argv[1], 'r')
print(_main(in_file))
in_file.close()
9 changes: 9 additions & 0 deletions ci/get_parent_issue.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

mentioned_issues=$(gh pr view "$1" --json body | jq -r .body | grep -Eo '#[0-9]+' | sed -e 's/#//g')

for issue in $mentioned_issues; do
if gh issue view "$issue" --json id > /dev/null; then
echo -n "$issue "
fi
done
Binary file added ci/images/pr_labelling.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1db4a10

Please sign in to comment.