-
Notifications
You must be signed in to change notification settings - Fork 33
132 lines (119 loc) · 4.78 KB
/
changelog-verify.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
# This runs the pre-commit hook logic to check whether the relevent
# CHANGELOG.md files have been updated for both the root directory
# and/or modules edited if they have been changed.
name: Validate Changelogs
on:
pull_request:
types: [opened, reopened, synchronize]
paths-ignore:
- ".github/**"
- ".githooks/**"
- "docs/**"
- "bin/**"
jobs:
changedfiles:
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
all: ${{ steps.changes.outputs.all}}
steps:
# Make sure we have some code to diff.
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get changed files
id: changes
# Set outputs using the command.
run: echo "all=$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | xargs)" >> $GITHUB_OUTPUT
validate:
runs-on: ubuntu-latest
# require the first job to have ran
needs: changedfiles
# Map a step output to a job output
outputs:
all: ${{ steps.validate.outputs.all }}
# only run if there are changed files
if: ${{needs.changedfiles.outputs.all}}
steps:
- uses: actions/checkout@v3
- name: Verify changelogs
id: validate
# Set outputs using the command.
run: |
bash ./.githooks/pre-receive "${{needs.changedfiles.outputs.all}}" 2>&1 | tee output.txt
VALIDATION_OUTPUT=$(cat output.txt)
echo "all=$(echo "$VALIDATION_OUTPUT" | jq -sR .)" >> $GITHUB_OUTPUT
if [[ "$VALIDATION_OUTPUT" == *"failed"* ]]; then
exit 1
fi
review:
runs-on: ubuntu-latest
# require the validate job to have ran
needs: validate
if: ${{ contains(github.event.pull_request.labels.*.name, 'cl validate') }}
steps:
- name: Verify changelogs Failed - Create review comment
if: ${{ contains(needs.validate.outputs.all, 'failed') }}
uses: actions/github-script@v5
with:
script: |
const result = `<!-- validate_changelogs_review -->The changelog validation failed with the following output:
\`\`\`
${{ fromJSON(needs.validate.outputs.all) }}
\`\`\`
Please update the relevant CHANGELOG.md files and ensure they follow the correct format.`;
await github.rest.pulls.createReview({
pull_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: result,
event: "REQUEST_CHANGES",
});
console.log("❌ Check failed, review comment created");
process.exit(1);
- name: Verify changelogs OK - Dismiss review comments
env:
OUTPUT: ${{ needs.validate.outputs.all }}
if: ${{ !contains(needs.validate.outputs.all, 'failed') }}
uses: actions/github-script@v5
with:
script: |
const prNumber = context.payload.pull_request.number;
const uniqueIdentifier = "<!-- validate_changelogs_review -->";
const perPage = 100;
let page = 1;
let hasMore = true;
const matchingReviews = [];
// List all the reviews on the pull request
while (hasMore) {
const { data: reviews } = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
per_page: perPage,
page: page,
});
console.log(`Processing page ${page} with each page having ${perPage} reviews.`);
console.log("reviews count", reviews.length);
const filteredReviews = reviews.filter(
(review) => review.body.includes(uniqueIdentifier) && review.state !== "DISMISSED"
);
console.log("filteredReviews count", filteredReviews.length);
matchingReviews.push(...filteredReviews);
hasMore = reviews.length === perPage;
page += 1;
}
console.log(`Found ${matchingReviews.length} reviews.`);
// Dismiss all reviews containing the unique identifier
for (const review of matchingReviews) {
console.log(`Dismissing review ${review.id}...`);
await github.rest.pulls.dismissReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
review_id: review.id,
message: "The check succeeded, dismissing the review comment.",
});
}
console.log("✅ Check passed, dismissed all review comments");