Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Cache verified CLA coverage when CLA Assistant has no result

Manav Rathi0fbe0db6ba33
diff --git a/.github/checks/cla/test.mjs b/.github/checks/cla/test.mjs
new file mode 100644
index 0000000000..4c9390dd8d
--- /dev/null
+++ b/.github/checks/cla/test.mjs
@@ -0,0 +1,197 @@
+import assert from "node:assert/strict";
+import { execFileSync } from "node:child_process";
+import { join } from "node:path";
+import test from "node:test";
+
+const source = execFileSync(
+ "ruby",
+ [
+ "-ryaml",
+ "-e",
+ 'puts YAML.safe_load(File.read(ARGV.fetch(0)), aliases: true).fetch("jobs").fetch("cla").fetch("steps").first.fetch("with").fetch("script")',
+ join(import.meta.dirname, "../../workflows/cla.yml"),
+ ],
+ { encoding: "utf8" },
+);
+const evaluate = new Function(
+ "github",
+ "context",
+ `return (async () => {${source}})()`,
+);
+const pr = {
+ number: 42,
+ state: "open",
+ user: { id: 24503581 },
+ base: { ref: "main", sha: "base" },
+ head: { sha: "head", repo: { full_name: "contributor/ente" } },
+};
+const commit = {
+ author: { id: 24503581 },
+ committer: { id: 24503581 },
+ commit: { message: "Update photos" },
+};
+const upstream = (state) => ({
+ context: "license/cla",
+ state,
+ description: "CLA Assistant result",
+ target_url: "https://cla-assistant.io/ente/ente?pullRequest=42",
+});
+
+async function run({
+ eventName = "pull_request_target",
+ payload = { pull_request: pr },
+ pull = pr,
+ peers = [pr],
+ commits = [commit],
+ total = commits.length,
+ statuses = [],
+ arriving,
+} = {}) {
+ const writes = [];
+ const reads = [];
+ let canonical = statuses;
+ const github = {
+ rest: {
+ pulls: {
+ get: async (args) => {
+ reads.push(["pull", args]);
+ return { data: pull };
+ },
+ list: async () => {
+ reads.push(["peers"]);
+ return { data: peers };
+ },
+ },
+ repos: {
+ compareCommitsWithBasehead: async (args) => {
+ reads.push(["compare", args]);
+ if (arriving) canonical = [[arriving]];
+ return { data: { commits, total_commits: total } };
+ },
+ getCombinedStatusForRef: async (args) => {
+ reads.push(["statuses", args]);
+ return {
+ data: { statuses: canonical[args.page - 1] ?? [] },
+ };
+ },
+ createCommitStatus: async (args) => {
+ writes.push(args);
+ },
+ },
+ },
+ async paginate(method, args, map) {
+ if (!map) return (await method(args)).data;
+ const result = [];
+ for (let page = 1; ; page++) {
+ const items = map(await method({ ...args, page }));
+ if (!items.length) return result;
+ result.push(...items);
+ }
+ },
+ };
+ await evaluate(github, {
+ repo: { owner: "ente", repo: "ente" },
+ eventName,
+ payload,
+ });
+ return { writes, reads };
+}
+
+test("canonical results override cached coverage and are read across pages", async () => {
+ for (const state of ["success", "pending", "failure", "error"]) {
+ const { writes, reads } = await run({
+ statuses: [
+ [{ context: "CLA", state: "success" }],
+ [upstream(state)],
+ ],
+ });
+ assert.deepEqual(writes, [
+ {
+ owner: "ente",
+ repo: "ente",
+ sha: "head",
+ ...upstream(state),
+ context: "CLA",
+ },
+ ]);
+ assert.equal(
+ reads.some(([name]) => name === "compare"),
+ false,
+ );
+ }
+});
+
+test("missing canonical results can use complete cached coverage at fixed commit SHAs", async () => {
+ const { writes, reads } = await run({
+ commits: [commit, { ...commit, committer: { id: 19864447 } }],
+ });
+ assert.equal(writes[0].state, "success");
+ assert.deepEqual(reads.find(([name]) => name === "compare")[1], {
+ owner: "ente",
+ repo: "ente",
+ basehead: "base...head",
+ });
+});
+
+test("uncertain contributor coverage waits for CLA Assistant", async () => {
+ for (const options of [
+ { pull: { ...pr, user: { id: 1 } } },
+ { commits: [{ ...commit, author: null }] },
+ { commits: [{ ...commit, author: { id: 1 } }] },
+ {
+ commits: [
+ {
+ ...commit,
+ commit: {
+ message:
+ "Fix\n\nCo-authored-by: Other <[email protected]>",
+ },
+ },
+ ],
+ },
+ { commits: [] },
+ { total: 2 },
+ { peers: [pr, { ...pr, number: 43 }] },
+ { peers: [{ ...pr, number: 43 }] },
+ ]) {
+ const { writes } = await run(options);
+ assert.equal(writes[0].state, "pending");
+ }
+});
+
+test("a canonical result arriving during the cache lookup wins", async () => {
+ const { writes } = await run({ arriving: upstream("pending") });
+ assert.equal(writes[0].state, "pending");
+ assert.equal(writes[0].description, upstream("pending").description);
+});
+
+test("status events replace earlier cache approval with the latest canonical result", async () => {
+ const { writes, reads } = await run({
+ eventName: "status",
+ payload: { sha: "fork-head", state: "success" },
+ statuses: [[{ context: "CLA", state: "success" }, upstream("pending")]],
+ });
+ assert.equal(writes[0].sha, "fork-head");
+ assert.equal(writes[0].state, "pending");
+ assert.ok(reads.every(([name]) => name === "statuses"));
+});
+
+test("manual dispatch evaluates the requested PR's existing head", async () => {
+ const { writes, reads } = await run({
+ eventName: "workflow_dispatch",
+ payload: { inputs: { pull_request: "42" } },
+ });
+ assert.equal(reads[0][1].pull_number, 42);
+ assert.equal(writes[0].sha, "head");
+ assert.equal(writes[0].state, "success");
+});
+
+test("closed and non-main PRs are outside the cache", async () => {
+ for (const pull of [
+ { ...pr, state: "closed" },
+ { ...pr, base: { ref: "release/test", sha: "base" } },
+ ]) {
+ const { writes } = await run({ pull });
+ assert.deepEqual(writes, []);
+ }
+});
diff --git a/.github/checks/run.sh b/.github/checks/run.sh
index 7365fff8d9..d82e38d0fe 100755
--- a/.github/checks/run.sh
+++ b/.github/checks/run.sh
@@ -10,6 +10,7 @@ node .github/checks/documentation-comments/test.mjs
ruby .github/checks/workflow-paths/check.rb
ruby .github/checks/workflow-paths/test.rb
ruby .github/checks/workflow-security/test.rb
+node --test .github/checks/cla/test.mjs
node --test .github/checks/pr-approval/test.mjs
node .github/checks/rust-ci-paths/check.mjs
node --test .github/checks/rust-ci-paths/test.mjs
diff --git a/.github/workflows/cla.yml b/.github/workflows/cla.yml
new file mode 100644
index 0000000000..d8a2b00ed1
--- /dev/null
+++ b/.github/workflows/cla.yml
@@ -0,0 +1,94 @@
+name: CLA
+
+on:
+ pull_request_target:
+ branches: [main]
+ types: [opened, synchronize, reopened, edited]
+ status:
+ workflow_dispatch:
+ inputs:
+ pull_request:
+ description: PR number
+ type: number
+ required: true
+
+permissions:
+ contents: read
+ pull-requests: read
+ statuses: write
+
+jobs:
+ cla:
+ name: Evaluate CLA
+ if: >-
+ github.repository == 'ente/ente' &&
+ (github.event_name != 'status' || github.event.context == 'license/cla')
+ runs-on: ubuntu-latest
+ concurrency:
+ group: cla
+ queue: max
+ steps:
+ - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
+ with:
+ script: |
+ const coveredUsers = new Map([
+ [254676, "ua741"],
+ [1161789, "vishnukvmd"],
+ [2698932, "anandbaburajan"],
+ [24503581, "mnvr"],
+ [41898282, "github-actions[bot]"],
+ [58779643, "crowdin-bot"],
+ [64626875, "AswinAsok"],
+ [77285023, "ashilkn"],
+ [81471280, "laurenspriem"],
+ [113718372, "setalp"],
+ [118678415, "r4khul"],
+ [118880285, "Jishnuraj9"],
+ [146618155, "AmanRajSinghMourya"],
+ [157287863, "vijayadithyanpj"],
+ [231175576, "neeraj-pilot"],
+ [236760864, "Shanthy17"],
+ [239996999, "laurens-pilot"],
+ [281837475, "aman-pilot"],
+ [290690209, "ente-ci[bot]"],
+ [293300083, "ashil-pilot"],
+ [327484853, "ente-approver[bot]"],
+ [341794418, "ente-agent[bot]"],
+ ]);
+ const repo = context.repo;
+ const pr = context.eventName === "status" ? null : (await github.rest.pulls.get({
+ ...repo,
+ pull_number: context.payload.pull_request?.number ?? Number(context.payload.inputs.pull_request),
+ })).data;
+ if (pr && (pr.state !== "open" || pr.base.ref !== "main")) return;
+ const sha = pr?.head.sha ?? context.payload.sha;
+ const readCLA = async () => (await github.paginate(
+ github.rest.repos.getCombinedStatusForRef,
+ { ...repo, ref: sha, per_page: 100 },
+ ({ data }) => data.statuses,
+ )).find(({ context }) => context === "license/cla");
+ let cla = await readCLA();
+ let covered = false;
+ if (!cla && pr && coveredUsers.has(pr.user.id)) {
+ const { data } = await github.rest.repos.compareCommitsWithBasehead({
+ ...repo,
+ basehead: `${pr.base.sha}...${sha}`,
+ });
+ const peers = (await github.paginate(github.rest.pulls.list, {
+ ...repo, state: "open", base: "main", per_page: 100,
+ })).filter(pull => pull.head.sha === sha);
+ covered = peers.length === 1 && peers[0].number === pr.number &&
+ data.commits.length > 0 && data.commits.length === data.total_commits &&
+ data.commits.every(({ author, commit }) =>
+ coveredUsers.has(author?.id) &&
+ !/co-authored-by/i.test(commit.message));
+ cla = await readCLA();
+ }
+ await github.rest.repos.createCommitStatus({
+ ...repo, sha, context: "CLA",
+ state: cla?.state ?? (covered ? "success" : "pending"),
+ description: cla ? cla.description : covered
+ ? "Verified CLA coverage is cached."
+ : "Waiting for CLA Assistant to report a result.",
+ target_url: cla ? cla.target_url : `https://cla-assistant.io/${repo.owner}/${repo.repo}`,
+ });