Skip to content
Closed
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
38 changes: 38 additions & 0 deletions lib/commits-webkit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";
const fetch = require('node-fetch');

const BASE_URL = 'https://commits.webkit.org';

// Only allow references to commits on WebKit's main branch, either as a
// canonical identifier (e.g. "324168@main") or as a legacy Subversion revision
// (e.g. "r266816"). References are parsed out of Bugzilla comments, so they
// are strictly validated before being used to build a URL.
const REF_PATTERN = /^(?:\d+@main|r\d+)$/;

/**
* Retrieves information about a commit in the WebKit repository.
*
* @param {string} ref A commit reference, e.g. "324168@main" or "r266816".
* @return {Promise<object|null>} The commit (with `hash`, `branch`,
* `identifier`, `revision` and `message` fields), or null if no such
* commit exists.
*/
async function get(ref) {
if (typeof ref !== 'string' || !REF_PATTERN.test(ref)) {
throw new Error(`Invalid WebKit commit reference: ${ref}`);
}
const response = await exports.fetch(`${BASE_URL}/${ref}/json`, {
headers: { "User-Agent": "wpt-pr-bot" },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should match everything else:

const response = await fetch(url, {headers: { "User-Agent": "ganesh" }});

"User-Agent": "ganesh",

"User-Agent": "ganesh"

Suggested change
headers: { "User-Agent": "wpt-pr-bot" },
headers: { "User-Agent": "ganesh" },

});
if (response.status === 404) {
return null;
}
if (!response.ok) {
throw new Error(`Unexpected status ${response.status} from ${BASE_URL} for ${ref}`);
}
return response.json();
}

exports.get = get;
// Exposed so that tests can stub out network access.
exports.fetch = fetch;
115 changes: 107 additions & 8 deletions lib/metadata/webkit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
"use strict";
var bugsWebkit = require('../bugs-webkit');
var bugsWebkit = require('../bugs-webkit'),
commitsWebkit = require('../commits-webkit'),
logger = require('../logger');

// Accounts that resolve a bug as FIXED when a reviewed patch lands in WebKit.
const WEBKIT_LANDING_BOTS = ["ews-feeder", "commit-queue"];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's actually store these by their actual usernames, for when we actually fix authentication:

Suggested change
const WEBKIT_LANDING_BOTS = ["ews-feeder", "commit-queue"];
const WEBKIT_LANDING_BOTS = ["ews-feeder@webkit.org", "commit-queue@webkit.org"];


// Matches the comment left by WebKit's landing bots, e.g.
// "Committed 324168@main (004e01c34435): <https://commits.webkit.org/324168@main>"
// "Committed r266816: <https://trac.webkit.org/changeset/266816>"
const LANDING_COMMENT_PATTERN = /Committed (\d+@main|r\d+)(?: \(([0-9a-f]{7,40})\))?/g;
Comment on lines +9 to +12

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an interesting inconsistency here, insofar as we allow any branch for SVN but only main for git.

The history of comments is something like:

https://bugs.webkit.org/show_bug.cgi?id=209237, commit-queue@webkit.org:

Patch

Clearing flags on attachment: 393868

Committed r258643: https://trac.webkit.org/changeset/258643

https://bugs.webkit.org/show_bug.cgi?id=208861, ews-feeder@webkit.org:

Committed r258210: https://trac.webkit.org/changeset/258210

All reviewed patches have been landed. Closing bug and clearing flags on attachment 393151 [details].

https://bugs.webkit.org/show_bug.cgi?id=223117, ews-feeder@webkit.org:

Committed r274424: https://commits.webkit.org/r274424

All reviewed patches have been landed. Closing bug and clearing flags on attachment 423037 [details].

Eight bugs, including https://bugs.webkit.org/show_bug.cgi?id=230351, ews-feeder@webkit.org:

Committed r286611 (244935@trunk): https://commits.webkit.org/244935@trunk

All reviewed patches have been landed. Closing bug and clearing flags on attachment 446169 [details].

And https://bugs.webkit.org/show_bug.cgi?id=247043 contains two examples, ews-feeder@webkit.org:

Committed 256627@main (8f70e6bf626c): https://commits.webkit.org/256627@main

Reviewed commits have been landed. Closing PR #6300 and removing active labels.

Plus:

Committed 252432.920@safari-7614-branch (768d89b728fc): https://commits.webkit.org/252432.920@safari-7614-branch

Reviewed commits have been landed. Closing PR #245 and removing active labels.

Though we can have things only on a branch on their own bug.

I might be tempted to use a regex more like:

Suggested change
// Matches the comment left by WebKit's landing bots, e.g.
// "Committed 324168@main (004e01c34435): <https://commits.webkit.org/324168@main>"
// "Committed r266816: <https://trac.webkit.org/changeset/266816>"
const LANDING_COMMENT_PATTERN = /Committed (\d+@main|r\d+)(?: \(([0-9a-f]{7,40})\))?/g;
// Matches the comment left by WebKit's landing bots, e.g.
// "Committed 324168@main (004e01c34435): <https://commits.webkit.org/324168@main>"
// "Committed 252432.920@safari-7614-branch (768d89b728fc): <https://commits.webkit.org/252432.920@safari-7614-branch>"
// "Committed r286611 (244935@trunk): <https://commits.webkit.org/244935@trunk>"
// "Committed r274424: <https://commits.webkit.org/r274424>"
// "Committed r266816: <https://trac.webkit.org/changeset/266816>"
const LANDING_COMMENT_PATTERN = /^Committed ([0-9]+(?:\.[0-9]+)*@[^\000-\040 ~^:?*[\\]+|r[0-9]+)[ :]/gm;


// bugs.webkit.org historically reported user identities (e.g. the `who` field
// of bug history) as full email addresses (e.g. "ews-feeder@webkit.org"), but
// now applies Bugzilla's email filtering for logged-out users, which reports
// only the local part of the address (e.g. "ews-feeder"). Accept either form.
//
// IMPORTANT: The local part alone is NOT a unique identity; anyone can register
// an account such as "ews-feeder@example.com", which is reported as
// "ews-feeder". This check is therefore only a cheap pre-filter, and a landing
// must additionally be verified against the WebKit repository (see `landed`).
function isWebKitLandingBot(who) {
if (typeof who !== "string") {
return false;
}
var at = who.indexOf("@");
if (at > -1) {
if (who.slice(at + 1) !== "webkit.org") {
return false;
}
who = who.slice(0, at);
}
return WEBKIT_LANDING_BOTS.includes(who);
}

exports.related = function(title) {
return title.indexOf("WebKit export of") > -1;
Expand All @@ -16,13 +49,81 @@ exports.verified = function(webkitIssue, wptPullRequestNumber) {
});
};

// Returns true if `commit` (as returned by commits-webkit) is a commit on
// WebKit's main branch whose message references the given WebKit bug.
function isLandedCommitForBug(commit, issue, abbreviatedHash) {
if (!commit || commit.branch !== "main" || typeof commit.message !== "string") {
return false;
}
if (abbreviatedHash && (typeof commit.hash !== "string" || !commit.hash.startsWith(abbreviatedHash))) {
return false;
}
// `issue` is always numeric (it is extracted with /\d+/ from the PR title),
// so it is safe to embed in the pattern. The negative lookahead prevents
// e.g. bug 12345 from matching a reference to bug 123456.
var bugURLPattern = new RegExp(
`(?:https?://bugs\\.webkit\\.org/show_bug\\.cgi\\?id=|https?://webkit\\.org/b/)${issue}(?!\\d)`);
Comment on lines +64 to +65

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't change

var titleWebkitIssueMatch = metadata.title.match(/.*http(?:s?):\/\/bugs.webkit.org\/show_bug.cgi\?id=(\d+).*/i);
and thus the https?://webkit\\.org/b/ alternation is pointless.

We also only have a single bug matching that, which suggests we might not need it: https://github.com/web-platform-tests/wpt/pulls?q=is%3Apr+webkit.org%2Fb%2F+in%3Atitle

return bugURLPattern.test(commit.message);
}

// Determines whether a change for the given WebKit bug has actually landed on
// WebKit's main branch.
//
// Bugzilla identities cannot be trusted (see `isWebKitLandingBot`), and nor can
// comment text, so this looks for the "Committed ..." comment left by the
// landing bot and then confirms with the WebKit repository itself (via
// commits.webkit.org) that the referenced commit exists on main and that its
// message references this bug. Only WebKit committers can land on main, so
// this cannot be spoofed by an arbitrary Bugzilla user.
//
// Errors talking to commits.webkit.org are logged and treated as "not landed";
// the pull request poller will try again later.
exports.landed = async function(issue) {
var body = await bugsWebkit.get("/rest/bug/:id/comment", { id: issue });
var bug = body && body.bugs && body.bugs[issue];
var comments = (bug && bug.comments) || [];
for (var comment of comments) {
if (!isWebKitLandingBot(comment.creator) || typeof comment.text !== "string") {
continue;
}
for (var match of comment.text.matchAll(LANDING_COMMENT_PATTERN)) {
var ref = match[1], abbreviatedHash = match[2];
try {
var commit = await commitsWebkit.get(ref);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to ensure the abbreviatedHash points at something suitable, and not just trust the canonical identifier, then we should pass it in here, rather than the canonical identifier?

if (isLandedCommitForBug(commit, issue, abbreviatedHash)) {
return true;
}
} catch (e) {
logger.warn({ err: e, webkitIssue: issue, ref: ref },
`Unable to verify WebKit commit ${ref} for bug ${issue}`);
}
}
}
return false;
};

function isLandingResolution(history_item, change) {
return change.field_name == "resolution" &&
change.added.includes("FIXED") &&
isWebKitLandingBot(history_item.who);
}

exports.flags = function(issue) {
var flags = {
reviewed: false,
inCommit: false
};
return bugsWebkit.get("/rest/bug/:id/history", { id: issue }).then(function (body) {
body.bugs[0].history.forEach(function(history_item) {
return bugsWebkit.get("/rest/bug/:id/history", { id: issue }).then(async function (body) {
var history = body.bugs[0].history;
// Only verify the landing (which requires additional network
// requests) if the history claims that a landing bot fixed the bug.
var claimsLanding = history.some(function(history_item) {
return history_item.changes.some(function(change) {
return isLandingResolution(history_item, change);
});
});
var landed = claimsLanding && await exports.landed(issue);
history.forEach(function(history_item) {
history_item.changes.forEach(function(change) {
if (change.field_name == "flagtypes.name") {
if (change.removed.includes("review+")) {
Expand All @@ -40,11 +141,9 @@ exports.flags = function(issue) {
flags.inCommit = true;
}
}
if (change.field_name == "resolution") {
if (change.added.includes("FIXED") && ["ews-feeder@webkit.org", "commit-queue@webkit.org"].includes(history_item.who)) {
flags.reviewed = true;
flags.inCommit = true;
}
if (landed && isLandingResolution(history_item, change)) {
flags.reviewed = true;
flags.inCommit = true;
}
});
});
Expand Down
74 changes: 74 additions & 0 deletions test/commits-webkit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

var assert = require('chai').assert,
sinon = require('sinon'),
commitsWebkit = require('../lib/commits-webkit');

suite('commits-webkit', function() {
var sandbox, fetch;

setup(function() {
sandbox = sinon.createSandbox();
fetch = sandbox.stub(commitsWebkit, 'fetch');
});

teardown(function() {
sandbox.restore();
});

function respond(status, body) {
fetch.resolves({
status: status,
ok: status >= 200 && status < 300,
json: async () => body,
});
}

test('retrieves a commit by git identifier', async function() {
var commit = { hash: 'abc', branch: 'main', identifier: '324168@main' };
respond(200, commit);
assert.deepEqual(await commitsWebkit.get('324168@main'), commit);
assert.equal(fetch.firstCall.args[0], 'https://commits.webkit.org/324168@main/json');
});

test('retrieves a commit by Subversion revision', async function() {
var commit = { hash: 'def', branch: 'main', revision: 266816 };
respond(200, commit);
assert.deepEqual(await commitsWebkit.get('r266816'), commit);
assert.equal(fetch.firstCall.args[0], 'https://commits.webkit.org/r266816/json');
});

test('returns null for a commit that does not exist', async function() {
respond(404, { message: 'No commit with the specified reference could be found' });
assert.isNull(await commitsWebkit.get('999999999@main'));
});

test('throws on other errors', async function() {
respond(503, {});
try {
await commitsWebkit.get('324168@main');
assert.fail('expected an error');
} catch (e) {
assert.include(e.message, '503');
}
});

[
'324168@safari-7620-branch',
'../324168@main',
'324168@main/../../evil',
'abcdef',
'',
undefined,
].forEach(function(ref) {
test(`rejects invalid reference ${JSON.stringify(ref)}`, async function() {
try {
await commitsWebkit.get(ref);
assert.fail('expected an error');
} catch (e) {
assert.include(e.message, 'Invalid WebKit commit reference');
}
assert.isFalse(fetch.called);
});
});
});
20 changes: 20 additions & 0 deletions test/get-metadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';
var assert = require('chai').assert,
sinon = require('sinon'),
bugsWebkit = require('../lib/bugs-webkit'),
commitsWebkit = require('../lib/commits-webkit'),
getMetadata = require('../lib/metadata');

suite('getMetadata', function() {
Expand Down Expand Up @@ -364,6 +366,24 @@ suite('getMetadata', function() {
});

test('retrieval and formatting of metadata for a WebKit related pull request', async function() {
// The bug was resolved FIXED by commit-queue, so the landing is
// verified against the bug's comments and the WebKit repository.
sandbox.stub(bugsWebkit, 'get').callThrough()
.withArgs('/rest/bug/:id/comment', { id: '201401' })
.resolves({ bugs: { '201401': { comments: [
{
creator: 'commit-queue@webkit.org',
text: 'Comment on attachment 381810\nPatch\n\nClearing flags on attachment: 381810\n\n' +
'Committed r251544: <https://trac.webkit.org/changeset/251544>',
},
] } } });
sandbox.stub(commitsWebkit, 'get').withArgs('r251544').resolves({
hash: '0000000000000000000000000000000000000000',
branch: 'main',
revision: 251544,
message: '[MSE][GStreamer] Fix video is not visible\n' +
'https://bugs.webkit.org/show_bug.cgi?id=201401\n\nReviewed by Philippe Normand.\n',
});
var expected = {
issue: 19538,
title: 'WebKit export of https://bugs.webkit.org/show_bug.cgi?id=201401',
Expand Down
Loading
Loading