Skip to content

Commit

Permalink
feat: move related issues (#59)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
leonsteinhaeuser authored Feb 23, 2023
1 parent 3e53796 commit 65a3752
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Since the issues and pull requests from this repository are also managed by this
| `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. |
| `operation_mode` | false | The operation mode to use. Must be one of `custom_field`, `status`. Defaults to: `status` |
| `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) |
| `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` |

## Getting started

Expand Down
14 changes: 14 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ inputs:
The value must be an array of objects.
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\"}]'
required: false
move_related_issues:
description: |
Specifies whether to move related issues.
The value must be of type boolean.
The value can be one of the following:
- true
- false
required: false
default: "false"
branding:
icon: 'terminal'
color: 'blue'
Expand Down Expand Up @@ -98,6 +107,11 @@ runs:
script: |
core.setFailed('operation mode is set to custom_field but custom_field_values is not set.')
- name: Export MOVE_RELATED_ISSUES
if: inputs.move_related_issues != ''
shell: bash
run: echo "MOVE_RELATED_ISSUES=${{ inputs.move_related_issues }}" >> $GITHUB_ENV

# execute organization script
- name: Execute Status Organization automation
if: inputs.organization != '' && inputs.operation_mode == 'status'
Expand Down
19 changes: 19 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ fi
# export DEBUG_MODE=true
DEBUG_MODE_ENABLED="${DEBUG_MODE:-false}"

# MOVE_RELATED_ISSUES provides a way to enable the move related issues
# This will move all related issues to the same column as the pull request
MOVE_RELATED_ISSUES="${MOVE_RELATED_ISSUES:-false}"

# ENTRYPOINT_MODE described the mode this application should run with
# supported ENTRYPOINT_MODEs are:
# - organization
Expand Down Expand Up @@ -96,6 +100,21 @@ function updateFieldScope() {
STATUS_FIELD_VALUE_ID=$(extractFieldNodeSingleSelectSettingValue "Status" "$RESOURCE_NODE_VALUE")
response=$(updateSingleSelectField "$PROJECT_UUID" "$PROJECT_ITEM_UUID" "$STATUS_FIELD_ID" "$STATUS_FIELD_VALUE_ID")
log="$log\n$response"

if [ "$MOVE_RELATED_ISSUES" = "true" ]; then
prdata=$(getPullRequestByNodeID $RESOURCE_NODE_ID)
PR_REPO_ISSUE_NUMBER=$(echo $prdata | jq .data.node.number)
PR_REPO_NAME=$(echo $prdata | jq .data.node.repository.name | sed -e 's+"++g')
PR_REPO_OWNER=$(echo $prdata | jq .data.node.repository.owner.login | sed -e 's+"++g')

# get linked issue ids
linked_issue_ids=$(getRelatedPullRequestIssueIDs $PR_REPO_OWNER $PR_REPO_NAME $PR_REPO_ISSUE_NUMBER)
for issue_id in $linked_issue_ids; do
project_item_id=$(getItemID $PROJECT_UUID $issue_id)
response=$(updateSingleSelectField "$PROJECT_UUID" "$project_item_id" "$STATUS_FIELD_ID" "$STATUS_FIELD_VALUE_ID")
log="$log\nUpdating referenced issue id: [ $issue_id ]\n$response"
done
fi
;;
custom_field)
x=0 # counter
Expand Down
44 changes: 44 additions & 0 deletions gh_api_global.sh
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,48 @@ function updateDateField() {
}
}
}' -f project=$PROJECT_ID -f item=$ITEM_ID -f fieldid=$FIELD_ID -f fieldValue="$FIELD_VALUE" | sed -e "s+\"++g"
}

# getPullRequestByNodeID returns the pull request data by node id
# $1: node id
function getPullRequestByNodeID() {
local NODE_ID=$1
gh api graphql -f query='
query($nodeId: ID!) {
node(id: $nodeId) {
... on PullRequest {
number
repository {
name
owner {
login
}
}
}
}
}' -F nodeId=$NODE_ID
}


# getRelatedPullRequestIssueIDs returns a list of issue ids that are related to the pull request
# $1: owner
# $2: repo
# $3: pull request number
function getRelatedPullRequestIssueIDs() {
local OWNER=$1
local REPO=$2
local PULL_REQUEST_NUMBER=$3
gh api graphql -f query='
query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $number) {
id
closingIssuesReferences(first: 100) {
nodes {
id
}
}
}
}
}' -f owner=$OWNER -f repo=$REPO -F number=$(($PULL_REQUEST_NUMBER)) | jq .data.repository.pullRequest.closingIssuesReferences.nodes[].id | sed -e 's+"++g'
}
11 changes: 11 additions & 0 deletions test_related_issues.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

echo "User based"
export MOVE_RELATED_ISSUES="true"
./entrypoint.sh user status leonsteinhaeuser 5 PR_kwDOGWypss5KPRmJ Todo
./entrypoint.sh user status leonsteinhaeuser 5 PR_kwDOGWypss5KPRmJ Done

echo "Organisation based"
export MOVE_RELATED_ISSUES="true"
./entrypoint.sh "organization" "status" "${MY_ORG}" "1" "PR_kwDOGoqhi85KS6Ox" "Todo"
./entrypoint.sh "organization" "status" "${MY_ORG}" "1" "PR_kwDOGoqhi85KS6Ox" "Done"

0 comments on commit 65a3752

Please sign in to comment.