Skip to content

Commit

Permalink
Add github action to file github issues based on errors detected in r…
Browse files Browse the repository at this point in the history
…eport

only for ill-formed and invalid w3c.json at this time. Part of #74
  • Loading branch information
dontcallmedom committed Sep 4, 2023
1 parent 71a88a0 commit 6f5bd46
Show file tree
Hide file tree
Showing 9 changed files with 561 additions and 5 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/clean-reports.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Clean reports when issues/PR are closed

on:
schedule:
- cron: '30 3 * * 3'
workflow_dispatch:
jobs:
clean:
runs-on: ubuntu-latest
steps:
- name: Checkout validate-repos
uses: actions/checkout@v3

- name: Setup node.js
uses: actions/setup-node@v2
with:
node-version: 18

- name: Install dependencies
run: |
npm ci
- name: Drop reports locally when related issues/PR are closed
run: node src/reporting/clean-reports.js
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Create PR to drop reports from repo if needed
uses: peter-evans/create-pull-request@v3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
title: Drop reports whose related issues have been closed
commit-message: "Drop reports whose related issues have been closed"
body: ${{ env.dropped_reports }}
assignees: dontcallmedom
branch: clean-reports
branch-suffix: timestamp
27 changes: 27 additions & 0 deletions .github/workflows/file-issue-for-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
on:
workflow_dispatch:

name: Submit draft issue reports from Strudy analysis
jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Setup node.js
uses: actions/setup-node@v2
with:
node-version: 18
- name: Checkout validate-repos
uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Configure git
run: |
git config user.name "w3c-validate-bot"
git config user.email "<>"
git remote set-url --push origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
- name: Run issue filer script
run: node reporting/file-issue-for-review.js
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}


27 changes: 27 additions & 0 deletions .github/workflows/submit-issue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
on:
workflow_dispatch:

name: Submit issue from validate-repos to relevant github repo
jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Setup node.js
uses: actions/setup-node@v2
with:
node-version: 18
- name: Checkout validate-repos
uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Configure git
run: |
git config user.name "w3c-validate-bot"
git config user.email "<>"
git remote set-url --push origin https://x-access-token:${{ secrets.ISSUE_REPORT_GH_TOKEN }}@github.com/$GITHUB_REPOSITORY
- name: Run issue submitter script
run: node src/reporting/submit-issue.js
env:
GH_TOKEN: ${{ secrets.ISSUE_REPORT_GH_TOKEN }}


Empty file added issue-reports/.gitkeep
Empty file.
68 changes: 63 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@octokit/core": "^3.6.0",
"@octokit/plugin-throttling": "^3.5.2",
"gray-matter": "^4.0.3",
"node-fetch": "^2.6.7",
"node-w3capi": "^2.1.0"
},
Expand Down
105 changes: 105 additions & 0 deletions reporting/clean-reports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Check GitHub issues and pull requests referenced by reports and create
* a pull request to drop reports that have been addressed.
*/

const core = require('@actions/core');
const octokit = require('../lib/octokit');
const fs = require('fs');
const path = require('path');
const matter = require('gray-matter');

const issueReportDir = "issue-reports";

/**
* Check GitHub issues and PR referenced by patch files and drop patch files
* that only reference closed issues and PR.
*
* @function
* @return {String} A GitHub flavored markdown string that describes what
* patches got dropped and why. To be used in a possible PR. Returns an
* empty string when there are no patches to drop.
*/
async function dropReportsWhenPossible () {
const rootDir = path.join(__dirname, '../../issues');

console.log('Gather reports files');
let reports = [];
const files = fs.readdirSync(rootDir);
for (const file of files) {
if (file.endsWith('.md')) {
const report = path.join(rootDir, file);
console.log(`- add "${report}"`);
reports.push({ name: report });
}
}

console.log();
console.log('Extract list of issues');
const issueUrl = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/(issues|pull)\/(\d+)$/;
for (const report of reports) {
let contents;
try {
contents = matter(await fs.promises.readFile(report.name, 'utf-8'));
} catch (e) {
console.error(`- "${report.name}" has invalid YAML`);
continue;
}
const tracked = contents.data.Tracked;
const m = tracked.match(issueUrl);
if (m) {
report.issue = {
owner: m[1],
repo: m[2],
number: parseInt(m[4], 10),
url: tracked
};
console.log(`- "${report.name}" linked to ${report.issue.url}`);
} else {
console.log(`- "${report.name}" not linked to any issue`);
}
}
reports = reports.filter(report => report.issue);

console.log();
console.log('Check status of GitHub issues/PR');
for (const { issue } of reports) {
const response = await octokit.issues.get({
owner: issue.owner,
repo: issue.repo,
issue_number: issue.number
});
issue.state = response?.data?.state ?? 'unknown';
console.log(`- [${issue.state}] ${issue.url}`);
}

console.log();
console.log('Drop reports when possible');
reports = reports.filter(report => report.issue.state === 'closed');
if (reports.length > 0) {
const res = [];
for (const report of reports) {
console.log(`- drop "${report.name}"`);
fs.unlinkSync(report.name);
res.push(`- \`${report.name}\` was linked to now closed: [${report.issue.owner}/${report.issue.repo}#${report.issue.number}](${report.issue.url})`);
}
return res.join('\n');
} else {
console.log('- No report to drop at this time');
return '';
}
}


dropReportsWhenPossible()
.then(res => {
core.exportVariable('dropped_reports', res);
console.log();
console.log('Set dropped_reports env variable');
console.log(res);
console.log('== The end ==');
})
.catch(err => {
console.error(err);
process.exit(1);
});
Loading

0 comments on commit 6f5bd46

Please sign in to comment.