Skip to content

Commit

Permalink
align with GH actions requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
timur27 committed Feb 27, 2025
1 parent a78de27 commit fa50d0f
Showing 1 changed file with 99 additions and 77 deletions.
176 changes: 99 additions & 77 deletions .github/workflows/feature-pair-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

Expand All @@ -19,87 +19,109 @@ jobs:
node-version: '16'

- name: Install dependencies
run: npm install @actions/core @actions/github
run: npm install @octokit/rest

- name: Check feature pairs
id: check-pairs
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const path = require('path');
// Load feature pairs configuration
const pairsConfig = JSON.parse(fs.readFileSync('.github/feature-pairs.json', 'utf8'));
// Get the list of files changed in this PR
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
const changedFiles = files.map(file => file.filename);
// Check each pair of features
let commentBody = '';
for (const pair of pairsConfig.pairs) {
const pathsWithChanges = [];
const pathsWithoutChanges = [];
- name: Create script
run: |
cat > check-feature-pairs.js << 'EOL'
const fs = require('fs');
const { Octokit } = require('@octokit/rest');
async function run() {
try {
// Initialize Octokit
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN
});
// Parse GitHub context from environment variables
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
const pullNumber = parseInt(process.env.PR_NUMBER, 10);
// Load feature pairs configuration
const pairsConfig = JSON.parse(fs.readFileSync('.github/feature-pairs.json', 'utf8'));
// Check which paths in the pair have changes
for (const featurePath of pair.paths) {
const hasChanges = changedFiles.some(file => file.startsWith(featurePath));
if (hasChanges) {
pathsWithChanges.push(featurePath);
} else {
pathsWithoutChanges.push(featurePath);
}
}
// Get the list of files changed in this PR
const { data: files } = await octokit.pulls.listFiles({
owner,
repo,
pull_number: pullNumber
});
// If some paths have changes but others don't, add to the comment
if (pathsWithChanges.length > 0 && pathsWithoutChanges.length > 0) {
commentBody += `## ${pair.name}\n\n`;
commentBody += `${pair.description}\n\n`;
commentBody += `- ✅ Changes detected in: \`${pathsWithChanges.join('`, `')}\`\n`;
commentBody += `- ❓ No changes detected in: \`${pathsWithoutChanges.join('`, `')}\`\n\n`;
commentBody += `Please verify if the changes in \`${pathsWithChanges.join('`, `')}\` should also be applied to \`${pathsWithoutChanges.join('`, `')}\`.\n\n`;
}
}
// If we have something to comment about, post a comment on the PR
if (commentBody) {
commentBody = `# Feature Pair Check\n\nThis PR contains changes to one part of a feature pair but not to its counterpart(s).\n\n${commentBody}`;
const changedFiles = files.map(file => file.filename);
// Check if we've already commented on this PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
});
// Check each pair of features
let commentBody = '';
const botComment = comments.find(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes('Feature Pair Check')
);
for (const pair of pairsConfig.pairs) {
const pathsWithChanges = [];
const pathsWithoutChanges = [];
// Check which paths in the pair have changes
for (const featurePath of pair.paths) {
const hasChanges = changedFiles.some(file => file.startsWith(featurePath));
if (hasChanges) {
pathsWithChanges.push(featurePath);
} else {
pathsWithoutChanges.push(featurePath);
}
}
// If some paths have changes but others don't, add to the comment
if (pathsWithChanges.length > 0 && pathsWithoutChanges.length > 0) {
commentBody += `## ${pair.name}\n\n`;
commentBody += `${pair.description}\n\n`;
commentBody += `- ✅ Changes detected in: \`${pathsWithChanges.join('`, `')}\`\n`;
commentBody += `- ❓ No changes detected in: \`${pathsWithoutChanges.join('`, `')}\`\n\n`;
commentBody += `Please verify if the changes in \`${pathsWithChanges.join('`, `')}\` should also be applied to \`${pathsWithoutChanges.join('`, `')}\`.\n\n`;
}
}
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: commentBody
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: commentBody
// If we have something to comment about, post a comment on the PR
if (commentBody) {
commentBody = `# Feature Pair Check\n\nThis PR contains changes to one part of a feature pair but not to its counterpart(s).\n\n${commentBody}`;
// Check if we've already commented on this PR
const { data: comments } = await octokit.issues.listComments({
owner,
repo,
issue_number: pullNumber
});
const botComment = comments.find(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes('Feature Pair Check')
);
if (botComment) {
// Update existing comment
await octokit.issues.updateComment({
owner,
repo,
comment_id: botComment.id,
body: commentBody
});
} else {
// Create new comment
await octokit.issues.createComment({
owner,
repo,
issue_number: pullNumber,
body: commentBody
});
}
}
}
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
run();
EOL
- name: Run feature pair check
run: node check-feature-pairs.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}

0 comments on commit fa50d0f

Please sign in to comment.