Skip to content

Commit

Permalink
feat(github-actions): do not override g3 status if pointing to CL
Browse files Browse the repository at this point in the history
  • Loading branch information
devversion committed Oct 12, 2022
1 parent 455d625 commit 6da368a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
32 changes: 30 additions & 2 deletions github-actions/google-internal-tests/lib/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import * as core from '@actions/core';
import {context} from '@actions/github';
import {Octokit} from '@octokit/rest';
import {Octokit, RestEndpointMethodTypes} from '@octokit/rest';
import minimatch from 'minimatch';

const statusContext = 'google-internal-tests';

type GithubStatus =
RestEndpointMethodTypes['repos']['getCombinedStatusForRef']['response']['data']['statuses'][0];

async function main() {
if (context.repo.owner !== 'angular') {
core.info('Skipping Google Internal Tests action for non-Angular repos.');
Expand All @@ -26,6 +31,13 @@ async function main() {
const github = new Octokit({auth: githubToken});
const prNum = context.payload.pull_request!.number;
const prHeadSHA = context.payload.pull_request!.head!.sha;
const existingGoogleStatus = await findExistingTestStatus(github, prHeadSHA);

// If there is an existing status already pointing to an internal CL, we do not override
// the status. This can happen when e.g. a presubmit-tested PR is closed and reopened.
if (existingGoogleStatus && existingGoogleStatus.target_url?.startsWith('http://cl/')) {
return;
}

const files = await github.paginate(github.pulls.listFiles, {
...context.repo,
Expand Down Expand Up @@ -61,7 +73,7 @@ async function main() {
await github.repos.createCommitStatus({
...context.repo,
...(affectsGoogle ? waitingForG3Status : irrelevantToG3Status),
context: 'google-internal-tests',
context: statusContext,
sha: prHeadSHA,
});
}
Expand All @@ -80,6 +92,22 @@ function constructPatterns(rawPatterns: string): minimatch.IMinimatch[] {
return patterns;
}

async function findExistingTestStatus(
github: Octokit,
prHeadSHA: string,
): Promise<GithubStatus | null> {
const existingStatuses = await github.paginate(
github.repos.getCombinedStatusForRef,
{
...context.repo,
ref: prHeadSHA,
},
(r) => r.data.statuses as GithubStatus[],
);

return existingStatuses.find((s) => s.context === statusContext) ?? null;
}

main().catch((e: Error) => {
core.error(e);
core.setFailed(e.message);
Expand Down
15 changes: 14 additions & 1 deletion github-actions/google-internal-tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16574,7 +16574,9 @@ var core = __toESM(require_core());
var import_github = __toESM(require_github());
var import_rest = __toESM(require_dist_node20());
var import_minimatch = __toESM(require_minimatch());
var statusContext = "google-internal-tests";
async function main() {
var _a;
if (import_github.context.repo.owner !== "angular") {
core.info("Skipping Google Internal Tests action for non-Angular repos.");
return;
Expand All @@ -16591,6 +16593,10 @@ async function main() {
const github = new import_rest.Octokit({ auth: githubToken });
const prNum = import_github.context.payload.pull_request.number;
const prHeadSHA = import_github.context.payload.pull_request.head.sha;
const existingGoogleStatus = await findExistingTestStatus(github, prHeadSHA);
if (existingGoogleStatus && ((_a = existingGoogleStatus.target_url) == null ? void 0 : _a.startsWith("http://cl/"))) {
return;
}
const files = await github.paginate(github.pulls.listFiles, {
...import_github.context.repo,
pull_number: prNum
Expand All @@ -16616,7 +16622,7 @@ async function main() {
await github.repos.createCommitStatus({
...import_github.context.repo,
...affectsGoogle ? waitingForG3Status : irrelevantToG3Status,
context: "google-internal-tests",
context: statusContext,
sha: prHeadSHA
});
}
Expand All @@ -16632,6 +16638,13 @@ function constructPatterns(rawPatterns) {
}
return patterns;
}
async function findExistingTestStatus(github, prHeadSHA) {
const existingStatuses = await github.paginate(github.repos.getCombinedStatusForRef, {
...import_github.context.repo,
ref: prHeadSHA
}, (r) => r.data.statuses);
return existingStatuses.find((s) => s.context === statusContext) ?? null;
}
main().catch((e) => {
core.error(e);
core.setFailed(e.message);
Expand Down

0 comments on commit 6da368a

Please sign in to comment.