Skip to content

Commit

Permalink
added close pr and comment on pr functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
14Richa committed Jun 28, 2023
1 parent 65bea9f commit 0b074bb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 43 deletions.
75 changes: 32 additions & 43 deletions .github/scripts/verify-maintainers-changes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,71 @@ const fs = require("fs");

const prAuthor = process.argv[2];

// Check if the PR is made by an async-bot
if (prAuthor.includes("[async-bot]")) {
console.log("Changes made by an async-bot. Verification not necessary.");
process.exit(0);
}

// Fetch the Maintainers.yaml file before changes
execSync("git fetch origin main");
execSync("git checkout FETCH_HEAD -- Maintainers.yaml");
const maintainersBefore = yaml.parse(
fs.readFileSync("Maintainers.yaml", "utf8")
);

// Reset to the current state
const maintainersBefore = yaml.parse(fs.readFileSync("Maintainers.yaml", "utf8"));

execSync("git checkout HEAD Maintainers.yaml");
const maintainersAfter = yaml.parse(
fs.readFileSync("Maintainers.yaml", "utf8")
);
const maintainersAfter = yaml.parse(fs.readFileSync("Maintainers.yaml", "utf8"));

// Check if any new maintainer objects are added
const beforeSet = new Set(
maintainersBefore.map((maintainer) => maintainer.name)
);
const beforeSet = new Set(maintainersBefore.map((maintainer) => maintainer.name));
const afterSet = new Set(maintainersAfter.map((maintainer) => maintainer.name));

let isCriticalChangeDetected = false;

for (const maintainer of maintainersAfter) {
if (!beforeSet.has(maintainer.name)) {
console.error(
`::error::Critical changes have been made to Maintainers.yaml by ${prAuthor}. A new maintainer object has been added. Please review.`
);
process.exit(1);
console.error(`::error::Critical changes have been made to Maintainers.yaml by ${prAuthor}. A new maintainer object has been added. Please review.`);
isCriticalChangeDetected = true;
break;
}
}

// Check if any maintainer objects are removed
for (const maintainer of maintainersBefore) {
if (!afterSet.has(maintainer.name)) {
console.error(
`::error::Critical changes have been made to Maintainers.yaml by ${prAuthor}. A maintainer object has been removed. Please review.`
);
process.exit(1);
if (!isCriticalChangeDetected) {
for (const maintainer of maintainersBefore) {
if (!afterSet.has(maintainer.name)) {
console.error(`::error::Critical changes have been made to Maintainers.yaml by ${prAuthor}. A maintainer object has been removed. Please review.`);
isCriticalChangeDetected = true;
break;
}
}
}

// Check for critical attribute changes
for (let i = 0; i < maintainersAfter.length; i++) {
const maintainerBefore = maintainersBefore.find(
(m) => m.name === maintainersAfter[i].name
);
const maintainerAfter = maintainersAfter[i];
if (!isCriticalChangeDetected) {
for (let i = 0; i < maintainersAfter.length; i++) {
const maintainerBefore = maintainersBefore.find(m => m.name === maintainersAfter[i].name);
const maintainerAfter = maintainersAfter[i];

if (
maintainerBefore &&
(maintainerBefore.github !== maintainerAfter.github ||
!arraysAreEqual(maintainerBefore.repos, maintainerAfter.repos))
) {
console.error(
`::error::Critical changes have been made to Maintainers.yaml by ${prAuthor}. Changes to critical attributes detected. Please review.`
);
process.exit(1);
if (maintainerBefore && (maintainerBefore.github !== maintainerAfter.github || !arraysAreEqual(maintainerBefore.repos, maintainerAfter.repos))) {
console.error(`::error::Critical changes have been made to Maintainers.yaml by ${prAuthor}. Changes to critical attributes detected. Please review.`);
isCriticalChangeDetected = true;
break;
}
}

if (!isCriticalChangeDetected) {
console.log("No critical changes detected.");
}
}

console.log("No critical changes detected.");
if (isCriticalChangeDetected) {
console.log("::set-output name=isCriticalChange::true");
}

// Helper function to check if two arrays are equal
function arraysAreEqual(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}

for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}

return true;
}
24 changes: 24 additions & 0 deletions .github/workflows/verify-maintainers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,28 @@ jobs:
run: npm install yaml

- name: Verify changes
id: verify
run: node .github/scripts/verify-maintainers-changes.js ${{ github.event.pull_request.user.login }}
env:
PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Close PR and Comment
if: steps.verify.outputs.isCriticalChange == 'true'
uses: actions/github-script@v5
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.rest.pulls.update({
owner: 'asyncapi',
repo: 'community',
pull_number: ${{ github.event.pull_request.number }},
state: 'closed'
});
github.rest.issues.createComment({
owner: 'asyncapi',
repo: 'community',
issue_number: ${{ github.event.pull_request.number }},
body: 'Critical changes have been made. This PR needs to be reviewed.'
});

0 comments on commit 0b074bb

Please sign in to comment.