Skip to content

Commit 563fa41

Browse files
committed
matrix for commits + email
1 parent 8221a9f commit 563fa41

File tree

3 files changed

+79
-56
lines changed

3 files changed

+79
-56
lines changed

.github/workflows/commit-review.yml

Lines changed: 78 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,77 +30,99 @@ jobs:
3030
# content="${content//$'\r'/'%0D'}"
3131
# # end of optional handling for multi line json
3232
# echo "::set-output name=reviewersJSON::$content"
33+
commits:
34+
runs-on: ubuntu-latest
35+
outputs:
36+
commits: ${{ steps.load_commits.outputs.commits }}
37+
steps:
38+
- uses: actions/checkout@v3
39+
with:
40+
# checkout full tree
41+
fetch-depth: 0
42+
- name: Load Commits
43+
id: load_commits
44+
run: |
45+
COMMITS="$(git rev-list ${{ github.event.before }}..${{ github.event.after }})"
46+
FORMATTED=$(echo $COMMITS | sed 's/ /","/g')
47+
FORMATTED="[\"$FORMATTED\"]"
48+
echo "::set-output name=commits::$FORMATTED"
3349
3450
check:
3551
runs-on: ubuntu-latest
36-
# needs: parse
52+
needs: commits
3753
strategy:
3854
matrix:
3955
keyword: ["IMS myin-itex", "DynamoDB myin-itex"]
56+
commit: ${{ fromJSON(needs.commits.outputs.commits) }}
4057
steps:
4158
- uses: actions/checkout@v3
42-
with:
43-
# checkout full tree
44-
fetch-depth: 0
4559

4660
- name: Check commit
61+
id: check_commit
4762
run: |
48-
KEYWORD_SPLIT=(${{matrix.keyword}})
63+
KEYWORD_SPLIT=(${{ matrix.keyword }})
4964
KEYWORD="${KEYWORD_SPLIT[0]}"
5065
REVIEWERS="${KEYWORD_SPLIT[1]}"
66+
COMMIT=${{ matrix.commit }}
67+
68+
# github actions cannot create a PR to change workflow files
69+
if git diff -U0 $COMMIT~1 $COMMIT ':(exclude).github/' | grep -E "^\+.*$KEYWORD.*"; then
5170
52-
# For each new commit pushed
53-
for COMMIT in $(git rev-list ${{ github.event.before }}..${{ github.event.after }}); do
54-
echo "checking commit $COMMIT"
55-
56-
# github actions cannot create a PR to change workflow files
57-
if git diff -U0 $COMMIT~1 $COMMIT ':(exclude).github/' | grep -E "^\+.*$KEYWORD.*"; then
58-
echo "Contains keyword $KEYWORD"
59-
60-
PR_OUTPUT="$(gh api /repos/${{ github.repository }}/commits/$COMMIT/pulls)"
61-
if [[ $(echo $PR_OUTPUT | jq 'length') -eq 0 ]]; then
62-
echo "Creating PR for $COMMIT"
63-
64-
BASE_BRANCH="commit-review-$COMMIT-$KEYWORD-base"
65-
TARGET_BRANCH="commit-review-$COMMIT-$KEYWORD-target"
66-
67-
git checkout -b $BASE_BRANCH $COMMIT~1
68-
git push --set-upstream $REMOTE $BASE_BRANCH
69-
70-
git checkout -b $TARGET_BRANCH $COMMIT
71-
git push --set-upstream $REMOTE $TARGET_BRANCH
72-
73-
gh pr create --title "Review: \"$KEYWORD\" has been used here" \
74-
--body "Please check if this change is safe" \
75-
--reviewer $REVIEWERS \
76-
--base $BASE_BRANCH \
77-
--head $TARGET_BRANCH
78-
else
79-
echo "Commit has existing PR"
80-
81-
# Only consider merge commit
82-
PR_NUMBER=$(echo $PR_OUTPUT | jq ".[] | select(.merge_commit_sha | test(\"$COMMIT\")) | .number")
83-
if [[ ! -z $PR_NUMBER ]]; then
84-
echo "Checking PR $PR_NUMBER for reviewers"
85-
86-
EXPECTED_REVIEWERS=($(echo $REVIEWERS | sed 's/,/ /'))
87-
REVIEWS="$(gh api /repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews)"
88-
REVIEWED=$(echo $REVIEWS | jq '.[].user.login')
89-
90-
MISSING_REVIEWERS=""
91-
for REVIEWER in ${EXPECTED_REVIEWERS[@]}; do
92-
if [[ -z $(echo $REVIEWED | grep $REVIEWER) ]]; then
93-
MISSING_REVIEWERS="$MISSING_REVIEWERS $REVIEWER"
94-
fi
95-
done
96-
97-
if [[ ! -z $MISSING_REVIEWERS ]]; then
98-
# TODO: handle multiple cases
99-
echo "::set-output name=missingReviewers::$MISSING_REVIEWERS"
100-
echo "::set-output name=failedCommit::$COMMIT"
101-
echo $MISSING_REVIEWERS
71+
PR_OUTPUT="$(gh api /repos/${{ github.repository }}/commits/$COMMIT/pulls)"
72+
73+
if [[ $(echo $PR_OUTPUT | jq 'length') -eq 0 ]]; then
74+
echo "Creating PR for $COMMIT"
75+
76+
BASE_BRANCH="commit-review-$COMMIT-$KEYWORD-base"
77+
TARGET_BRANCH="commit-review-$COMMIT-$KEYWORD-target"
78+
79+
git checkout -b $BASE_BRANCH $COMMIT~1
80+
git push --set-upstream $REMOTE $BASE_BRANCH
81+
82+
git checkout -b $TARGET_BRANCH $COMMIT
83+
git push --set-upstream $REMOTE $TARGET_BRANCH
84+
85+
gh pr create --title "Review: \"$KEYWORD\" has been used here" \
86+
--body "Please check if this change is safe" \
87+
--reviewer $REVIEWERS \
88+
--base $BASE_BRANCH \
89+
--head $TARGET_BRANCH
90+
else
91+
# Only consider merge commit
92+
PR_NUMBER=$(echo $PR_OUTPUT | jq ".[] | select(.merge_commit_sha | test(\"$COMMIT\")) | .number")
93+
94+
if [[ ! -z $PR_NUMBER ]]; then
95+
echo "Checking PR $PR_NUMBER for reviewers"
96+
97+
EXPECTED_REVIEWERS=($(echo $REVIEWERS | sed 's/,/ /g'))
98+
REVIEWS="$(gh api /repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews)"
99+
REVIEWED=$(echo $REVIEWS | jq '.[].user.login')
100+
101+
MISSING_REVIEWERS=""
102+
for REVIEWER in ${EXPECTED_REVIEWERS[@]}; do
103+
if [[ -z $(echo $REVIEWED | grep $REVIEWER) ]]; then
104+
MISSING_REVIEWERS="$MISSING_REVIEWERS $REVIEWER"
102105
fi
106+
done
107+
108+
if [[ ! -z $MISSING_REVIEWERS ]]; then
109+
echo "::set-output name=missingReviewers::$MISSING_REVIEWERS"
103110
fi
104111
fi
105112
fi
106-
done
113+
fi
114+
- name: Send email
115+
uses: dawidd6/action-send-mail@v3
116+
if: ${{ steps.check_commit.outputs.missingReviewers }}
117+
env:
118+
KEYWORD: (${{ matrix.keyword }})
119+
COMMIT: ${{ matrix.commit }}
120+
with:
121+
server_address: smtp.gmail.com
122+
server_port: 465
123+
username: ${{ secrets.MAIL_USER }}
124+
password: ${{ secrets.MAIL_PASSWORD }}
125+
from: ${{ secrets.MAIL_USER }}
126+
subject: "Test"
127+
to: min.yin@it-experts.at
128+
body: $KEYWORD has been used in commit $COMMIT

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.secrets

set-secrets.sh

Whitespace-only changes.

0 commit comments

Comments
 (0)