Skip to content

Commit fd3d6bc

Browse files
authored
Merge pull request ceph#63714 from Naveenaidu/wip-naveen-update-config-diff-script
.github/workflow/diff-ceph-config.yml: only detect the configuration changes that were made in the PR
2 parents 5b676c6 + 9d9adb8 commit fd3d6bc

File tree

4 files changed

+168
-75
lines changed

4 files changed

+168
-75
lines changed

.github/workflows/diff-ceph-config.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,30 @@ jobs:
2020
- name: checkout ceph.git
2121
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 #v4.2.2
2222
with:
23+
ref: ${{ github.event.pull_request.head.sha }}
2324
path: ceph
2425
sparse-checkout: |
2526
src/script
2627
src/common/options
2728
.github/workflows
2829
30+
- name: 'Get common ancestor between PR and ceph upstream main branch'
31+
id: get_common_ancestor
32+
env:
33+
branch_pr: origin/${{ github.event.pull_request.head.ref }}
34+
refspec_pr: +${{ github.event.pull_request.head.sha }}:remotes/origin/${{ github.event.pull_request.head.ref }}
35+
working-directory: ceph
36+
run: |
37+
# Fetch enough history to find a common ancestor commit (aka merge-base):
38+
git fetch origin ${{ env.refspec_pr }} --depth=$(( ${{ github.event.pull_request.commits }} + 1 )) \
39+
--no-tags --prune --no-recurse-submodules
40+
41+
# This should get the oldest commit in the local fetched history (the commit in ceph upstream from which PR branched from):
42+
COMMON_ANCESTOR=$( git rev-list --first-parent --max-parents=0 --max-count=1 ${{ env.branch_pr }} )
43+
COMMON_ANCESTOR_SHA=$( git log --format=%H "${COMMON_ANCESTOR}" )
44+
45+
echo "COMMON_ANCESTOR_SHA=${COMMON_ANCESTOR_SHA}" >> $GITHUB_ENV
46+
2947
- name: Setup Python
3048
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 #v5.6.0
3149
with:
@@ -41,12 +59,14 @@ jobs:
4159
env:
4260
REF_REPO: ${{ github.event.pull_request.base.repo.clone_url }}
4361
REF_BRANCH: ${{ github.event.pull_request.base.ref }}
62+
REF_COMMIT_SHA: ${{ env.COMMON_ANCESTOR_SHA }}
4463
REMOTE_REPO: ${{ github.event.pull_request.head.repo.clone_url }}
4564
REMOTE_BRANCH: ${{ github.event.pull_request.head.ref }}
65+
REMOTE_COMMIT_SHA: ${{ github.event.pull_request.head.sha }}
4666
run: |
4767
{
4868
echo 'DIFF_JSON<<EOF'
49-
python3 ./src/script/config-diff/config_diff.py diff-branch-remote-repo --ref-branch $REF_BRANCH --remote-repo $REMOTE_REPO --cmp-branch $REMOTE_BRANCH --format=posix-diff --skip-clone
69+
python3 ./src/script/config-diff/config_diff.py diff-branch-remote-repo --ref-branch $REF_BRANCH --ref-commit-sha $REF_COMMIT_SHA --remote-repo $REMOTE_REPO --cmp-branch $REMOTE_BRANCH --cmp-commit-sha $REMOTE_COMMIT_SHA --format=posix-diff --skip-clone
5070
echo EOF
5171
} >> "$GITHUB_OUTPUT"
5272
working-directory: ceph

.github/workflows/scripts/config-diff-post-comment.js

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
module.exports = async ({ github, context, core, configDiff }) => {
22
try {
3-
// Do not create comment if there are no configuration changes
4-
if (!configDiff) {
5-
console.log("No changes detected. Skipping comment creation.");
6-
return;
3+
const { owner, repo } = context.repo;
4+
const issueNumber = context.payload.pull_request.number;
5+
6+
// List all the comments
7+
const comments = await github.paginate(
8+
github.rest.issues.listComments, {
9+
owner,
10+
repo,
11+
issue_number: issueNumber,
12+
per_page: 100,
13+
}
14+
);
15+
16+
const existingComment = comments.find(comment => comment.body.includes("### Config Diff Tool Output"));
17+
18+
// Do not create comment if there are no configuration changes
19+
if (!configDiff) {
20+
core.info("No changes detected. Skipping comment creation.");
21+
22+
if (existingComment){
23+
// Remove any outdated configuration diff comments
24+
core.info("Existing config diff comment found. Deleting it...");
25+
await github.rest.issues.deleteComment({
26+
comment_id: existingComment.id,
27+
owner,
28+
repo,
29+
});
730
}
831

32+
return;
33+
}
34+
935
const commentBody = `
1036
### Config Diff Tool Output
1137
@@ -19,21 +45,15 @@ ${configDiff}
1945
The above configuration changes are found in the PR. Please update the relevant release documentation if necessary.
2046
`;
2147

22-
core.summary.addRaw(commentBody);
23-
await core.summary.write()
24-
25-
const { owner, repo } = context.repo;
26-
const issueNumber = context.payload.pull_request.number;
27-
2848
// List all files in the pull request
2949
core.info("Fetching list of files changed in the pull request...");
3050
const files = await github.paginate(
3151
github.rest.pulls.listFiles,
3252
{
33-
owner,
34-
repo,
35-
pull_number: issueNumber,
36-
per_page: 100,
53+
owner,
54+
repo,
55+
pull_number: issueNumber,
56+
per_page: 100,
3757
}
3858
);
3959

@@ -42,33 +62,35 @@ The above configuration changes are found in the PR. Please update the relevant
4262
// Only annotate the `yaml.in` files present in `src/common/options` folder
4363
if (file.filename.endsWith(".yaml.in") && file.filename.startsWith("src/common/options/")) {
4464
core.info(`Annotating file: ${file.filename}`);
65+
// Show annotations only at the start of the file
4566
core.notice(
46-
`Configuration changes detected in ${file.filename}. Please update the relevant release documentation if necessary.`,
67+
`Configuration changes detected in ${file.filename}. Please update the release documentation if necessary.`,
4768
{
48-
title: "Configuration Change Detected",
49-
file: file.filename,
50-
startLine: 1,
51-
endLine: 1,
69+
title: "Configuration Change Detected",
70+
file: file.filename,
71+
startLine: 1,
72+
endLine: 1,
5273
}
5374
);
5475
}
55-
});
76+
});
5677

57-
58-
// List all the comments
59-
const comments = await github.paginate(
60-
github.rest.issues.listComments, {
78+
core.summary.addRaw(commentBody);
79+
await core.summary.write()
80+
81+
if (existingComment) {
82+
// There might have been new configuration changes made after posting
83+
// the first comment. Hence replace the old comment with the new updated
84+
// changes
85+
core.info("A config diff comment already exists, updating it...");
86+
87+
// Update the existing comment
88+
await github.rest.issues.updateComment ({
89+
comment_id: existingComment.id,
6190
owner,
6291
repo,
63-
issue_number: issueNumber,
64-
per_page: 100,
65-
}
66-
);
67-
68-
const existingComment = comments.find(comment => comment.body.includes("### Config Diff Tool Output"));
69-
70-
if (existingComment) {
71-
core.info("A config diff comment already exists, deleting it...");
92+
body: commentBody,
93+
});
7294
} else {
7395
core.info("Creating a new config diff comment...");
7496
// Create a new comment
@@ -80,7 +102,7 @@ The above configuration changes are found in the PR. Please update the relevant
80102
});
81103

82104
}
83-
105+
84106
// Set the status as FAILED if any configuration changes are detected
85107
core.setFailed("Configuration Changes Detected, Update release documents - if necessary");
86108
} catch (error) {

src/script/config-diff/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ python3 config_diff.py <mode> [options]
5252
- `--cmp-branch`: The branch to compare.
5353
- `--remote-repo`: The remote repository URL for the branch to compare.
5454
- `--ref-repo`: (Optional) The repository URL for the reference branch. Defaults to the Ceph upstream repository.
55+
- `--ref-commit-sha`: (Optional) The commit sha for the reference branch that is used for reference
56+
- `--cmp-commit-sha`: (Optional) The commit sha of the comparing branch
5557
- `--skip-clone`: (Optional) Skips cloning repositories for diff. **Note**: When using this flag, the script must be run from a valid Ceph upstream repository or a forked repository that has access to the branches present in the upstream repository or already contains those branches.
5658
- `--format`: (Optional) Specify the output format for the configuration diff. Options are `json` or `posix-diff`. Default is `json`.
5759

0 commit comments

Comments
 (0)