134 lines (131 loc) · 4.77 KB
/
add-to-redmine.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Adds a new issue to Redmine for the specified pull request.
#
# Example Usage:
# --------------
# name: My favorite workflow
# on:
# pull_request:
# types: [opened]
# jobs:
# add-to-redmine:
# if: ${{ github.actor == 'dependabot[bot]' }}
# permissions:
# contents: write
# pull-requests: write
# repository-projects: read
# uses: pangaeatech/.github/.github/workflows/add-to-redmine.yml@main
# with:
# pr_num: ${{github.event.pull_request.number}}
# pr_url: ${{github.event.pull_request.html_url}}
# pr_subject: ${{github.event.pull_request.title}}
# pr_body: ${{github.event.pull_request.body}}
# rm_url: "https://redmine.mycompany.com/"
# rm_project_id: "testproject"
# rm_tracker_id: 13
# rm_version_id: 10
# rm_field_id: 11
# draft_prefix: "DRAFT: "
# secrets:
# rm_key: ${{ secrets.REDMINE_API_KEY }}
name: Add To Redmine
on:
workflow_call:
inputs:
pr_num:
# The Pull Request Number
required: true
type: string
pr_url:
# The Pull Request URL
required: true
type: string
pr_subject:
# The Pull Request Subject
required: true
type: string
pr_body:
# The Pull Request Description (optional)
required: false
type: string
rm_url:
# The Redmine URL
required: true
type: string
rm_project_id:
# The Project's unique id
required: true
type: string
rm_tracker_id:
# The redmine tracker ID to create the issue in (optional)
required: false
type: number
rm_version_id:
# The redmine milestone ID to create the issue in (optional)
required: false
type: number
rm_field_id:
# The redmine custom field ID to store the PR number in (optional)
required: false
type: number
draft_prefix:
# If the Pull Request has this prefix (case insensitive), then it will be set to Draft upon being added successfully to Redmine (optional - defaults to "WIP " if not specified).
required: false
type: string
secrets:
rm_key:
# The REST API Key of the redmine user to post the issue as
# NOTE: This user MUST have permission to "Add issues" to the
# project specified by rm_project_id
required: true
jobs:
annotate:
runs-on: ubuntu-latest
steps:
- name: Add task to redmine
run: |
sudo apt-get install -y jq curl pandoc
SUBJ=$(echo "$PR_SUBJECT" | sed -e "s/^$PREFIX//i")
BODY=$(echo "Linked to [Pull Request $PR_NUM]($PR_URL)
$PR_BODY" | pandoc -f gfm -t html | pandoc -f html -t textile | LC_ALL=UTF-8 sed -e 's/<tt>/<code>/g' -e 's/<\/tt>/<\/code>/g' -e 's/<hr *\/>/----/g' -e "s/[\xF0-\xF7].../�/g");
jq --null-input \
--arg key "$RM_KEY" \
--arg subj "$SUBJ" \
--arg desc "$BODY" \
--arg proj "$RM_PROJECT_ID" \
--arg trac "$RM_TRACKER_ID" \
--arg vers "$RM_VERSION_ID" \
--arg prnum "$PR_NUM" \
--arg fld "$RM_FIELD_ID" \
'{key:$key,issue:{project_id:$proj,tracker_id:$trac,subject:$subj,description:$desc,fixed_version_id:$vers,custom_fields:[{value:$prnum,id:$fld}]}}' > body.json
curl -s -X POST -H "Content-Type: application/json" -d "@body.json" "$RM_URL/issues.json" > issue.txt
TaskId=$(cat issue.txt | jq '.issue.id')
if [ -z "$TaskId" ] || [ "$TaskId" == "null" ]; then
cat issue.txt
exit 1
else
gh pr comment "$PR_URL" -b "Added as [Redmine Task $TaskId]($RM_URL/issues/$TaskId)";
fi;
env:
PR_URL: ${{ inputs.pr_url }}
PR_NUM: ${{ Inputs.pr_num }}
PR_SUBJECT: ${{ inputs.pr_subject }}
PR_BODY: ${{ inputs.pr_body }}
RM_PROJECT_ID: ${{ inputs.rm_project_id }}
RM_TRACKER_ID: ${{ inputs.rm_tracker_id }}
RM_VERSION_ID: ${{ inputs.rm_version_id }}
RM_FIELD_ID: ${{ inputs.rm_field_id }}
RM_URL: ${{ inputs.rm_url }}
RM_KEY: ${{ secrets.rm_key }}
PREFIX: "${{ inputs.draft_prefix || 'WIP ' }}"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Set PR to draft and update subject
if: startsWith(inputs.pr_subject, inputs.draft_prefix || 'WIP ')
run: |
gh pr ready "$PR_URL" --undo
SUBJ=$(echo "$SUBJECT" | sed -e "s/^$PREFIX//i")
gh pr edit "$PR_URL" --title "$SUBJ"
env:
PR_URL: ${{ inputs.pr_url }}
SUBJECT: "${{ inputs.pr_subject }}"
PREFIX: "${{ inputs.draft_prefix || 'WIP ' }}"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}