@webpieces/pr-gate 0.4.991 → 0.4.992

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pr-number-predictor.js","sourceRoot":"","sources":["../../../../../../../packages/tooling/pr-gate/src/scripts/workflow/pr-number-predictor.ts"],"names":[],"mappings":";;;;AAAA,8DAA6D;AAC7D,yCAA2D;AAE3D,mGAAmG;AACnG,4BAA4B;AAC5B,MAAa,WAAW;IACpB,MAAM,CAAS;IACf,GAAG,CAAS;IAEZ,YAAY,MAAc,EAAE,GAAW;QACnC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACnB,CAAC;CACJ;AARD,kCAQC;AAED;;;;GAIG;AACH,MAAM,aAAa,GACf,wFAAwF;IACxF,uFAAuF;IACvF,6FAA6F;IAC7F,+FAA+F,CAAC;AAEpG,0GAA0G;AAC1G,+EAA+E;AAC/E,MAAM,UAAU,GACZ,gGAAgG;IAChG,kHAAkH,CAAC;AAEvH;;;;;;;;;;;;GAYG;AAEI,IAAM,iBAAiB,GAAvB,MAAM,iBAAiB;IACG;IAA7B,YAA6B,SAA2B;QAA3B,cAAS,GAAT,SAAS,CAAkB;IAAG,CAAC;IAE5D,KAAK,CAAC,OAAO;QACT,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,EAAE;YAC1C,KAAK;YACL,SAAS;YACT,IAAI;YACJ,eAAe;YACf,IAAI;YACJ,aAAa;YACb,IAAI;YACJ,SAAS,aAAa,EAAE;YACxB,MAAM;YACN,UAAU;SACb,CAAC,CAAC;QACH,MAAM,KAAK,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAChE,MAAM,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC9B,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;QAC/B,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;YACvD,MAAM,GAAG,GAAG,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,sBAAsB,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,QAAQ,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAChI,OAAO,CAAC,MAAM,CAAC,KAAK,CAChB,8CAA8C,GAAG,2CAA2C;gBACxF,mEAAmE,CAC1E,CAAC;YACF,OAAO,IAAI,WAAW,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;QACxC,OAAO,IAAI,WAAW,CAAC,IAAI,EAAE,GAAG,OAAO,SAAS,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC;CACJ,CAAA;AA9BY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,sBAAU,EAAC,8BAAkB,CAAC,SAAS,CAAC;6CAEG,iCAAgB;GAD/C,iBAAiB,CA8B7B","sourcesContent":["import { NativeProcessApi } from '@webpieces/tooling-common';\nimport { injectable, bindingScopeValues } from 'inversify';\n\n// The number and web URL GitHub will most likely assign to the next PR. Both '' when no prediction\n// could be made. Data-only.\nexport class PredictedPr {\n number: string;\n url: string;\n\n constructor(number: string, url: string) {\n this.number = number;\n this.url = url;\n }\n}\n\n/*\n * Issues, PRs and discussions share ONE per-repo number counter, so the newest of the three, plus one, is\n * the number the next PR gets unless something else is created in the same few seconds. Each connection\n * is ordered explicitly (newest first) rather than relying on a connection's default order.\n */\nconst PREDICT_QUERY =\n 'query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { url ' +\n 'issues(first: 1, orderBy: {field: CREATED_AT, direction: DESC}) { nodes { number } } ' +\n 'pullRequests(first: 1, orderBy: {field: CREATED_AT, direction: DESC}) { nodes { number } } ' +\n 'discussions(first: 1, orderBy: {field: CREATED_AT, direction: DESC}) { nodes { number } } } }';\n\n// The newest number of the three (0 for an empty repo) and the repo's web URL, as one tab-separated line,\n// so nothing here parses JSON. A connection with no nodes contributes nothing.\nconst PREDICT_JQ =\n '\"\\\\(([.data.repository.issues.nodes[0].number, .data.repository.pullRequests.nodes[0].number, ' +\n '.data.repository.discussions.nodes[0].number] | map(select(. != null)) + [0]) | max)\\\\t\\\\(.data.repository.url)\"';\n\n/**\n * Predicts the number GitHub will assign to the PR about to be created (issue #1418), so its description\n * can carry its own link the FIRST time it goes up. Without it, creating a PR fires `opened` and then,\n * two seconds later, `edited` (the self-link back-fill) — and a consumer workflow with\n * `cancel-in-progress` cancels the `opened` run on every new PR.\n *\n * A number cannot be reserved, only predicted, so the caller always compares the prediction with the\n * number GitHub really assigned and back-fills on a miss — exactly today's behaviour, never worse.\n *\n * A failed query (network, permissions, a GitHub Enterprise Server without discussions) is NOT a failure\n * of the run: the prediction is simply absent, the caller creates the PR without its own link and\n * back-fills it afterwards. The warning says so, because the cost is one extra `edited` event.\n */\n@injectable(bindingScopeValues.Singleton)\nexport class PrNumberPredictor {\n constructor(private readonly processes: NativeProcessApi) {}\n\n async predict(): Promise<PredictedPr> {\n const result = await this.processes.run('gh', [\n 'api',\n 'graphql',\n '-F',\n 'owner={owner}',\n '-F',\n 'name={repo}',\n '-f',\n `query=${PREDICT_QUERY}`,\n '--jq',\n PREDICT_JQ,\n ]);\n const parts = result.ok ? result.stdout.trim().split('\\t') : [];\n const latest = parts[0] ?? '';\n const repoUrl = parts[1] ?? '';\n if (!/^\\d+$/.test(latest) || !repoUrl.startsWith('http')) {\n const why = result.ok ? `unexpected answer '${result.stdout.trim()}'` : (result.stderr.trim() || `exit ${String(result.code)}`);\n process.stderr.write(\n `⚠️ Could not predict the new PR's number (${why}). Creating it without its own link and\\n` +\n \" back-filling the link afterwards (one extra 'edited' event).\\n\",\n );\n return new PredictedPr('', '');\n }\n const next = String(Number(latest) + 1);\n return new PredictedPr(next, `${repoUrl}/pull/${next}`);\n }\n}\n"]}