Skip to content
Open
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
175 changes: 75 additions & 100 deletions .github/workflows/check-protected-classes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,119 +13,94 @@
# See the License for the specific language governing permissions and
# limitations under the License.

name: Protected Classes
name: Wire format report

# pull_request_target for the write token needed to comment/label the PR. The fork's changes are
# read through the API as data and never checked out or executed, so no allow-unsafe-pr-checkout and
# no working copy of the PR code on the runner. Do not add a step that builds or runs the PR code.
on: pull_request_target
on:
workflow_run:
workflows: ['Code Style, Abandoned Tests, Javadocs']
types: [completed]

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions: {}

jobs:
check-protected-classes:
report:
if: github.event.workflow_run.event == 'pull_request'
runs-on: ubuntu-latest
name: Rolling Upgrade check
concurrency:
group: wire-report-${{ github.event.workflow_run.head_repository.id }}-${{ github.event.workflow_run.head_branch }}
queue: max
permissions:
issues: write
actions: read
pull-requests: write
checks: write
steps:
- name: Detect protected class changes and report
- name: Report the wire comparison
uses: actions/github-script@v8
with:
script: |
const ORDER_USE = '@Order(';
const ORDER_IMPORT = 'import org.apache.ignite.internal.Order;';
const ORDER_PKG = 'package org.apache.ignite.internal;';
const MARKER = '<!-- ignite-rolling-upgrade-check -->';
const { owner, repo } = context.repo;
const pr = context.payload.pull_request;
const baseSha = pr.base.sha;
const headSha = pr.head.sha;

// Changed files are read from the API as data; the fork's code is never checked out or executed.
const files = await github.paginate(github.rest.pulls.listFiles, {
owner, repo, pull_number: pr.number, per_page: 100,
});

// Reproduce `git diff --no-renames --diff-filter=ADM`: a rename is a delete(old)+add(new) pair.
// A protected class carries the @Order annotation; an added file is defined by the head revision,
// a deleted/modified one by the base.
const revisions = [];
for (const f of files) {
if (f.status === 'added' || f.status === 'copied')
revisions.push([f.filename, headSha]);
else if (f.status === 'removed' || f.status === 'modified' || f.status === 'changed')
revisions.push([f.filename, baseSha]);
else if (f.status === 'renamed') {
revisions.push([f.previous_filename, baseSha]);
revisions.push([f.filename, headSha]);
}
const label = 'compatibility';

const run = context.payload.workflow_run;
const repo = context.repo;
const {actions, issues, pulls} = github.rest;
const only = items => items.length === 1 ? items[0] : undefined;
const list = (method, params) => github.paginate(method, {...repo, ...params, per_page: 100});

function findStepConclusion(job, name) {
return only(job?.steps?.filter(step => step.name === name && step.status === 'completed') ?? [])?.conclusion;
}

const isProtected = async (path, ref) => {
let meta;
try {
({ data: meta } = await github.rest.repos.getContent({ owner, repo, path, ref }));
} catch (e) {
if (e.status === 404) return false;
throw e;
}
if (Array.isArray(meta) || meta.type !== 'file') return false;
let content = meta.content ? Buffer.from(meta.content, 'base64').toString('utf8') : '';
if (!content && meta.sha) {
const { data: blob } = await github.rest.git.getBlob({ owner, repo, file_sha: meta.sha });
content = Buffer.from(blob.content, blob.encoding).toString('utf8');
}
const lines = content.split(/\r?\n/);

return content.includes(ORDER_USE)
&& (lines.includes(ORDER_IMPORT) || lines.includes(ORDER_PKG));
};

const hits = [];
for (const [path, ref] of revisions) {
if (path.endsWith('.java')
&& !path.startsWith('modules/core/src/test/resources/codegen/')
&& await isProtected(path, ref)) hits.push(path);
async function findPullRequestForRun(run) {
if (!run.head_repository) return;
if (run.pull_requests?.length) return only(run.pull_requests);

const head = `${run.head_repository.owner.login}:${run.head_branch}`;
const candidates = await list(pulls.list, {head, state: 'all'});
const target = only(candidates.filter(pr => pr.head.repo?.id === run.head_repository.id
&& pr.created_at <= run.created_at && (!pr.closed_at || pr.closed_at >= run.created_at)));
if (!target || target.state !== 'open' || target.head.sha !== run.head_sha) return;

const {node} = await github.graphql(`query($id: ID!, $since: DateTime!) {
node(id: $id) { ... on PullRequest {
timelineItems(first: 1, since: $since, itemTypes: [BASE_REF_CHANGED_EVENT]) { nodes { __typename } }
} }
}`, {id: target.node_id, since: run.created_at});
if (node?.timelineItems.nodes.length === 0) return target;
}

if (hits.length === 0) return;

// File names come from the fork; render them as inert inline code so they cannot inject
// markdown (backticks, @mentions, links) into content posted under the bot's write token.
const safe = f => '`' + String(f).replace(/[`\r\n]/g, '') + '`';
const list = hits.map(f => '- ' + safe(f)).join('\n');
const summary = [
'This PR modifies protected classes (with **Order** annotation).',
'Changes to these classes can break rolling upgrade compatibility.',
'',
'**Affected files:**',
list,
].join('\n');
const body = MARKER + '\n## Possible compatibility issues. Please, check rolling upgrade cases\n\n' + summary + '\n';

const comments = await github.paginate(github.rest.issues.listComments, {
owner, repo, issue_number: pr.number,
});
const existing = comments.find(c => c.body.includes(MARKER));
if (existing) {
await github.rest.issues.deleteComment({ owner, repo, comment_id: existing.id });
const jobs = await list(actions.listJobsForWorkflowRun, {run_id: run.id, filter: 'latest'});
if (jobs.some(job => job.run_attempt > run.run_attempt))
return core.info('A newer attempt has started.');

const headJobs = jobs.filter(job => job.head_sha === run.head_sha);
const wire = only(headJobs.filter(job => job.name === 'Compare wire format descriptions'));
const java = only(headJobs.filter(job => job.name === 'Check java code on JDK 17'));
const upload = findStepConclusion(wire, 'Upload wire diff');
if (wire?.conclusion !== 'success' || findStepConclusion(wire, 'Compare descriptions and publish summary') !== 'success'
|| findStepConclusion(java, 'Check all wire format descriptions are collected') !== 'success'
|| !['success', 'skipped'].includes(upload))
return core.info('The wire comparison or completeness check could not be verified.');

const target = await findPullRequestForRun(run);
if (!target)
return core.info('Could not identify the PR and its target branch.');

const issue = {...repo, issue_number: target.number};
const {data: pr} = await pulls.get({...repo, pull_number: target.number});
if (pr.state !== 'open' || pr.head.repo?.id !== run.head_repository.id || pr.head.ref !== run.head_branch
|| pr.head.sha !== run.head_sha || pr.base.ref !== target.base.ref)
return core.info('The PR has closed or changed since the comparison.');

const labeled = pr.labels.some(l => l.name === label);
if (upload === 'skipped') {
if (labeled)
await issues.removeLabel({...issue, name: label});

return;
}
await github.rest.issues.createComment({ owner, repo, issue_number: pr.number, body });

await github.rest.issues.addLabels({
owner, repo, issue_number: pr.number, labels: ['compatibility'],
});

await github.rest.checks.create({
owner, repo,
name: 'Rolling upgrade compatibility',
head_sha: headSha,
status: 'completed',
conclusion: 'neutral',
output: { title: 'Possible compatibility issues', summary },
});

if (!labeled)
await issues.addLabels({...issue, labels: [label]});

const url = `${run.html_url}/attempts/${wire.run_attempt}#summary-${wire.id}`;
const body = `Wire format changed. Review the [wire diff in Job Summary](${url}) for rolling upgrade compatibility.`;
await issues.createComment({...issue, body});