Skip to content

Commit 65a3752

Browse files
feat: move related issues (#59)
* feat: added function to get the pr by node id * feat: added function to get related issues by pr owner, repo and number * feat: added status field handling for related issues * feat: added action input field move_related_issues * docs: added documentation for move_related_issues * test: added test cases
1 parent 3e53796 commit 65a3752

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Since the issues and pull requests from this repository are also managed by this
5151
| `status_value` | false | The status value to set. Must be one of the values defined in your project board **Status field settings**. If left unspecified, new items are added without an explicit status, and existing items are left alone. |
5252
| `operation_mode` | false | The operation mode to use. Must be one of `custom_field`, `status`. Defaults to: `status` |
5353
| `custom_field_values` | false | Provides the possibility to change custom fields. To be applied the **operation_mode** must be set to `custom_field`. For the json definition refer to [JSON-Definition](#JSON-Definition) |
54+
| `move_related_issues` | false | If set to `true` and the operation mode is set to `status`, the automation will also move related issues to the same column. This is useful if you want to move an issue to the same column as its related pull request. Defaults to: `false` |
5455

5556
## Getting started
5657

action.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ inputs:
5252
The value must be an array of objects.
5353
Example: '[{\"name\": \"Priority\",\"type\": \"text\",\"value\": \"uuid1\"},{\"name\": \"Severity\",\"type\": \"text\",\"value\": \"uuid2\"},{\"name\": \"Number\",\"type\": \"number\",\"value\": \"100\"},{\"name\": \"Date\",\"type\": \"date\",\"value\": \"2022-01-28T20:02:27.306+01:00\"},{\"name\": \"Single Select\",\"type\": \"single_select\",\"value\": \"Option 1\"},{\"name\": \"Iteration\",\"type\": \"iteration\",\"value\": \"Iteration 1\"}]'
5454
required: false
55+
move_related_issues:
56+
description: |
57+
Specifies whether to move related issues.
58+
The value must be of type boolean.
59+
The value can be one of the following:
60+
- true
61+
- false
62+
required: false
63+
default: "false"
5564
branding:
5665
icon: 'terminal'
5766
color: 'blue'
@@ -98,6 +107,11 @@ runs:
98107
script: |
99108
core.setFailed('operation mode is set to custom_field but custom_field_values is not set.')
100109
110+
- name: Export MOVE_RELATED_ISSUES
111+
if: inputs.move_related_issues != ''
112+
shell: bash
113+
run: echo "MOVE_RELATED_ISSUES=${{ inputs.move_related_issues }}" >> $GITHUB_ENV
114+
101115
# execute organization script
102116
- name: Execute Status Organization automation
103117
if: inputs.organization != '' && inputs.operation_mode == 'status'

entrypoint.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ fi
1010
# export DEBUG_MODE=true
1111
DEBUG_MODE_ENABLED="${DEBUG_MODE:-false}"
1212

13+
# MOVE_RELATED_ISSUES provides a way to enable the move related issues
14+
# This will move all related issues to the same column as the pull request
15+
MOVE_RELATED_ISSUES="${MOVE_RELATED_ISSUES:-false}"
16+
1317
# ENTRYPOINT_MODE described the mode this application should run with
1418
# supported ENTRYPOINT_MODEs are:
1519
# - organization
@@ -96,6 +100,21 @@ function updateFieldScope() {
96100
STATUS_FIELD_VALUE_ID=$(extractFieldNodeSingleSelectSettingValue "Status" "$RESOURCE_NODE_VALUE")
97101
response=$(updateSingleSelectField "$PROJECT_UUID" "$PROJECT_ITEM_UUID" "$STATUS_FIELD_ID" "$STATUS_FIELD_VALUE_ID")
98102
log="$log\n$response"
103+
104+
if [ "$MOVE_RELATED_ISSUES" = "true" ]; then
105+
prdata=$(getPullRequestByNodeID $RESOURCE_NODE_ID)
106+
PR_REPO_ISSUE_NUMBER=$(echo $prdata | jq .data.node.number)
107+
PR_REPO_NAME=$(echo $prdata | jq .data.node.repository.name | sed -e 's+"++g')
108+
PR_REPO_OWNER=$(echo $prdata | jq .data.node.repository.owner.login | sed -e 's+"++g')
109+
110+
# get linked issue ids
111+
linked_issue_ids=$(getRelatedPullRequestIssueIDs $PR_REPO_OWNER $PR_REPO_NAME $PR_REPO_ISSUE_NUMBER)
112+
for issue_id in $linked_issue_ids; do
113+
project_item_id=$(getItemID $PROJECT_UUID $issue_id)
114+
response=$(updateSingleSelectField "$PROJECT_UUID" "$project_item_id" "$STATUS_FIELD_ID" "$STATUS_FIELD_VALUE_ID")
115+
log="$log\nUpdating referenced issue id: [ $issue_id ]\n$response"
116+
done
117+
fi
99118
;;
100119
custom_field)
101120
x=0 # counter

gh_api_global.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,48 @@ function updateDateField() {
232232
}
233233
}
234234
}' -f project=$PROJECT_ID -f item=$ITEM_ID -f fieldid=$FIELD_ID -f fieldValue="$FIELD_VALUE" | sed -e "s+\"++g"
235+
}
236+
237+
# getPullRequestByNodeID returns the pull request data by node id
238+
# $1: node id
239+
function getPullRequestByNodeID() {
240+
local NODE_ID=$1
241+
gh api graphql -f query='
242+
query($nodeId: ID!) {
243+
node(id: $nodeId) {
244+
... on PullRequest {
245+
number
246+
repository {
247+
name
248+
owner {
249+
login
250+
}
251+
}
252+
}
253+
}
254+
}' -F nodeId=$NODE_ID
255+
}
256+
257+
258+
# getRelatedPullRequestIssueIDs returns a list of issue ids that are related to the pull request
259+
# $1: owner
260+
# $2: repo
261+
# $3: pull request number
262+
function getRelatedPullRequestIssueIDs() {
263+
local OWNER=$1
264+
local REPO=$2
265+
local PULL_REQUEST_NUMBER=$3
266+
gh api graphql -f query='
267+
query($owner: String!, $repo: String!, $number: Int!) {
268+
repository(owner: $owner, name: $repo) {
269+
pullRequest(number: $number) {
270+
id
271+
closingIssuesReferences(first: 100) {
272+
nodes {
273+
id
274+
}
275+
}
276+
}
277+
}
278+
}' -f owner=$OWNER -f repo=$REPO -F number=$(($PULL_REQUEST_NUMBER)) | jq .data.repository.pullRequest.closingIssuesReferences.nodes[].id | sed -e 's+"++g'
235279
}

test_related_issues.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
echo "User based"
4+
export MOVE_RELATED_ISSUES="true"
5+
./entrypoint.sh user status leonsteinhaeuser 5 PR_kwDOGWypss5KPRmJ Todo
6+
./entrypoint.sh user status leonsteinhaeuser 5 PR_kwDOGWypss5KPRmJ Done
7+
8+
echo "Organisation based"
9+
export MOVE_RELATED_ISSUES="true"
10+
./entrypoint.sh "organization" "status" "${MY_ORG}" "1" "PR_kwDOGoqhi85KS6Ox" "Todo"
11+
./entrypoint.sh "organization" "status" "${MY_ORG}" "1" "PR_kwDOGoqhi85KS6Ox" "Done"

0 commit comments

Comments
 (0)