Skip to content

Commit 0554405

Browse files
[ISV-5194] Operator pipeline merge-pr parse the gh tool response
Custom error message
1 parent 41a9500 commit 0554405

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

ansible/roles/operator-pipeline/templates/openshift/pipelines/operator-hosted-pipeline.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,9 @@ spec:
12451245
- name: source
12461246
workspace: repository
12471247
subPath: src
1248+
- name: output
1249+
workspace: results
1250+
subPath: summary
12481251

12491252
# link pull request details to test results
12501253
- name: link-pull-request-with-merged-status

ansible/roles/operator-pipeline/templates/openshift/tasks/github-pipelinerun-summary.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ spec:
164164
fi
165165
fi
166166
167+
168+
if [[ -f "$(workspaces.output.path)/merge_error.txt" ]]; then
169+
MERGE_ERROR=$(cat "$(workspaces.output.path)/merge_error.txt")
170+
echo -e "\n## ❌ Not merged\n" >> $PR_NAME/comment.md
171+
echo -e "PR cannot be merged for the following reasons: \`$MERGE_ERROR\`" >> $PR_NAME/comment.md
172+
fi
173+
167174
echo -e "\n## Troubleshooting\n\nPlease refer to the [troubleshooting guide]($DOC_LINK)." >> $PR_NAME/comment.md
168175
echo -e "\nRun \`/pipeline restart $(params.pipeline_name)\` in case of pipeline failure to restart a pipeline." >> $PR_NAME/comment.md
169176

ansible/roles/operator-pipeline/templates/openshift/tasks/merge-pr.yml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ spec:
2121
default: token
2222
workspaces:
2323
- name: source
24+
- name: output
25+
description: Scratch space and storage for the comment and related data
2426
results:
2527
- name: bool_merge
2628
- name: pr_merged
@@ -62,12 +64,6 @@ spec:
6264
# in a single call upfront and process the result later
6365
gh pr view "$(params.git_pr_url)" --json isDraft,reviews >/tmp/pr.json
6466
65-
if [[ "$(jq -r ".isDraft" /tmp/pr.json)" == "true" ]] ; then
66-
echo "Skipping merge: PR is set as draft"
67-
echo -n "false" > "$(results.pr_merged.path)"
68-
exit 0
69-
fi
70-
7167
# Extract all reviews and return one line per reviewer containing three space separated fields:
7268
# $state $authorAssociation $author
7369
# where
@@ -84,21 +80,31 @@ spec:
8480
8581
# Do not merge if we do not have approval from the bot or any other repo member
8682
if ! grep "^APPROVED MEMBER " /tmp/reviews.txt ; then
87-
echo "Skipping merge: PR is not approved."
83+
echo -n "Skipping merge: PR is not approved." | tee "$(workspaces.output.path)/merge_error.txt"
8884
echo -n "false" > "$(results.pr_merged.path)"
8985
exit 0
9086
fi
9187
9288
# Squash and merge only if the head commit sha has not changed since
9389
# the start of the pipeline run
94-
gh pr merge "$(params.git_pr_url)" --squash --auto \
95-
--match-head-commit "$(params.git_head_commit)"
90+
MERGE_STDERR=$(gh pr merge "$(params.git_pr_url)" --squash --auto --match-head-commit "$(params.git_head_commit)" 2>&1 >/dev/null)
91+
MERGE_RESULT=$?
92+
MERGE_ERROR=$(echo "$MERGE_STDERR" | grep -oP '(?<=GraphQL: ).*')
9693
97-
if [[ $? -eq 0 ]] ; then
94+
if [[ $MERGE_RESULT -eq 0 ]] ; then
9895
echo "PR has been merged!"
9996
echo -n "true" > "$(results.pr_merged.path)"
10097
else
10198
echo "Cannot merge PR"
10299
echo -n "false" > "$(results.pr_merged.path)"
103-
exit 1
100+
# Pass details about merge error to summary comment task
101+
echo -n $MERGE_ERROR | tee "$(workspaces.output.path)/merge_error.txt"
102+
# Custom error message when other pipeline is running.
103+
if [[ "$MERGE_ERROR" == "Head branch was modified. Review and try the merge again. (mergePullRequest)" ]] ; then
104+
echo -n "Another pipeline is already in progress: please wait for the result of the new tests." | tee "$(workspaces.output.path)/merge_error.txt"
105+
fi
106+
# Fail pipeline if the error is not explicitly allowed.
107+
if [[ "$MERGE_ERROR" != "Pull Request is still a draft (mergePullRequest)" && "$MERGE_ERROR" != "Head branch was modified. Review and try the merge again. (mergePullRequest)" ]] ; then
108+
exit 1
109+
fi
104110
fi

ansible/roles/operator-pipeline/templates/openshift/tasks/read-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ spec:
2727
workspaces:
2828
- name: source
2929
- name: output
30-
description: Scratch space and storage for the comment and related date
30+
description: Scratch space and storage for the comment and related data
3131
steps:
3232
- name: read-config
3333
image: "$(params.pipeline_image)"

0 commit comments

Comments
 (0)