Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions .github/scripts/announcement-detector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const github = require("./github");
const detect = require("./detector");
const publish = require("./reporter");

(async () => {

console.log("");
console.log("==========================================");
console.log("Developer Announcement Recommender");
console.log("==========================================");

const prNumber = github.event.pull_request.number;

//--------------------------------------------------
// Get PR
//--------------------------------------------------

const pr = await github.getPullRequest(prNumber);

if (pr.draft) {

console.log("Draft PR detected.");
console.log("Skipping analysis.");

return;

}

//--------------------------------------------------
// Get Files
//--------------------------------------------------

const files = await github.getFiles(prNumber);

console.log(`Analyzing PR #${prNumber}`);
console.log(`Files changed: ${files.length}`);

//--------------------------------------------------
// Run Detector
//--------------------------------------------------

const result = detect(pr, files);

console.log("");

console.log("Recommendation:");

console.log(
result.recommendation
? "Recommend Announcement"
: "No Recommendation"
);

console.log("");

if (result.signals.length) {

console.log("Detected Signals:");

result.signals.forEach(signal => {

console.log(` - ${signal.name}`);

});

}

//--------------------------------------------------
// Publish Comment
//--------------------------------------------------

await publish(
prNumber,
result
);

console.log("");
console.log("Finished.");
console.log("");

})();
99 changes: 99 additions & 0 deletions .github/scripts/detector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const RULES = require("./rules");

/**
* Run all announcement rules against a PR
*
* @param {Object} pr
* @param {Array} files
* @returns {{
* recommendation: boolean,
* signals: Array
* }}
*/
function detect(pr, files) {

const detectedSignals = [];

// Build searchable text once
const searchableText = [
pr.title || "",
pr.body || "",
...files.map(f => f.patch || "")
].join("\n");

for (const rule of RULES) {

let matched = false;

//--------------------------------------------------
// 1. File Status
//--------------------------------------------------

if (!matched && rule.detect.status) {

matched = files.some(file =>
rule.detect.status.includes(file.status)
);

}

//--------------------------------------------------
// 2. File Paths
//--------------------------------------------------

if (!matched && rule.detect.paths) {

matched = files.some(file =>
rule.detect.paths.some(regex =>
regex.test(file.filename)
)
);

}

//--------------------------------------------------
// 3. Keywords
//--------------------------------------------------

if (!matched && rule.detect.keywords) {

matched = rule.detect.keywords.some(regex =>
regex.test(searchableText)
);

}

//--------------------------------------------------
// Rule matched
//--------------------------------------------------

if (matched) {

detectedSignals.push({
id: rule.id,
name: rule.name,
description: rule.description,
priority: rule.priority
});

}

}

//--------------------------------------------------
// Recommendation
//--------------------------------------------------

const recommendation = detectedSignals.length > 0;

return {

recommendation,

signals: detectedSignals

};

}

module.exports = detect;
149 changes: 149 additions & 0 deletions .github/scripts/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
const fs = require("fs");

const token = process.env.GITHUB_TOKEN;

const event = JSON.parse(
fs.readFileSync(process.env.GITHUB_EVENT_PATH, "utf8")
);

const owner = event.repository.owner.login;
const repo = event.repository.name;

const API = `https://api.github.com/repos/${owner}/${repo}`;

const headers = {
Authorization: `Bearer ${token}`,
Accept: "application/vnd.github+json",
"Content-Type": "application/json"
};

// ======================================================
// Generic HTTP
// ======================================================

async function get(url) {

const response = await fetch(url, {
headers
});

if (!response.ok) {
throw new Error(
`GET ${url} failed (${response.status})`
);
}

return response.json();
}

async function post(url, body) {

const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify(body)
});

if (!response.ok) {

throw new Error(
`POST ${url} failed (${response.status})`
);

}

return response.json();

}

async function patch(url, body) {

const response = await fetch(url, {
method: "PATCH",
headers,
body: JSON.stringify(body)
});

if (!response.ok) {

throw new Error(
`PATCH ${url} failed (${response.status})`
);

}

return response.json();

}

// ======================================================
// Pull Request
// ======================================================

async function getPullRequest(number) {

return get(
`${API}/pulls/${number}`
);

}

async function getFiles(number) {

return get(
`${API}/pulls/${number}/files?per_page=100`
);

}
Comment on lines +91 to +97

async function getComments(number) {

return get(
`${API}/issues/${number}/comments`
);

}
Comment on lines +99 to +105

async function createComment(number, body) {

return post(
`${API}/issues/${number}/comments`,
{
body
}
);

}

async function updateComment(commentId, body) {

return patch(
`${API}/issues/comments/${commentId}`,
{
body
}
);

}

// ======================================================

module.exports = {

owner,

repo,

event,

getPullRequest,

getFiles,

getComments,

createComment,

updateComment

};
Loading
Loading