Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.
Merged
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
15 changes: 14 additions & 1 deletion functions/src/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,22 @@ If you can't get the PR to a green state due to flakes or broken master, please
l1TriageLabels: [["comp: *"]],
// arrays of labels that determine if a PR has been fully triaged
l2TriageLabels: [["type: *", "effort*", "risk*", "comp: *"]]
}
},

rerunCircleCI: {
// set to true to disable
disabled: true,
// the label which when added triggers a rerun of the default CircleCI workflow.
triggerRerunLabel: 'Trigger CircleCI Rerun';
Copy link
Contributor

Choose a reason for hiding this comment

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

this default label is too long and misleading. We are not just rerunning the CircleCI workflow, we are rerunning it rebased against the latest HEAD of the target branch.

Maybe "Rerun CI against HEAD"?

Copy link
Member Author

Choose a reason for hiding this comment

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

My concern with just saying CI is that not all things that show up on CI will be rerun, g3 presubmits being an example.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll merge this for now, since this label will be part of the config on each repo anyway you can debate the name of the label on the PR that @josephperrott will make to update the config file

},
};

export interface AppConfig {
merge: MergeConfig;
triage: TriageConfig;
triagePR: TriagePRConfig;
size: SizeConfig;
rerunCircleCI: RerunCircleCIConfig;
}

export interface MergeConfig {
Expand Down Expand Up @@ -187,3 +195,8 @@ export interface SizeConfig {
export interface AdminConfig {
allowInit: boolean;
}

export interface RerunCircleCIConfig {
disabled: boolean;
triggerRerunLabel: string;
}
95 changes: 95 additions & 0 deletions functions/src/plugins/rerun-circleci.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {config} from 'firebase-functions';
import {Application, Context} from "probot";
import {Task} from "./task";
import {RerunCircleCIConfig} from "../default";
import Github from '@octokit/rest';
import fetch from "node-fetch";

let circleCIConfig = config().circleCI.token;

// Check if we are in Firebase or in development
if(!circleCIConfig) {
// Use dev config
circleCIConfig = require('../../private/circle-ci.json');
}

const CIRCLE_CI_TOKEN = circleCIConfig.token;

export class RerunCircleCITask extends Task {
constructor(robot: Application, db: FirebaseFirestore.Firestore) {
super(robot, db);

// Dispatch when a label is added to a pull request.
this.dispatch([
'pull_request.labeled',
], this.checkRerunCircleCI.bind(this));
}

/** Determines if a circle rerun should occur. */
async checkRerunCircleCI(context: Context): Promise<void> {
const config = await this.getConfig(context);
if (config.disabled) {
return;
}

if (context.payload.label) {
const label: Github.IssuesGetLabelResponse = context.payload.label;
if (label.name === config.triggerRerunLabel) {
await this.triggerCircleCIRerun(context);
}
}
}

/** Triggers a rerun of the default CircleCI workflow and then removed the triggering label. */
async triggerCircleCIRerun(context: Context) {
const config = await this.getConfig(context);
if (config.disabled) {
return;
}

const pullRequest: Github.PullRequestsGetResponse = context.payload.pull_request;
const sender: Github.PullRequestsGetResponseUser = context.payload.sender;
const {owner, repo} = context.repo();
const circleCiUrl = `https://circleci.com/api/v1.1/project/github/${owner}/${repo}/build?circle-token=${CIRCLE_CI_TOKEN}`;
try {
await fetch(circleCiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
revision: pullRequest.head.sha,
branch: `pull/${pullRequest.number}`,
})
});
} catch (err) {
const error: TypeError = err;
context.github.issues.createComment({
body: `@${sender.login} the CircleCI rerun you requested failed. See details below:

\`\`\`
${error.message}
\`\`\``,
number: pullRequest.number,
owner: owner,
repo: repo,
})

}
await context.github.issues.removeLabel({
name: config.triggerRerunLabel,
number: pullRequest.number,
owner: owner,
repo: repo
});
}

/**
* Gets the config for the merge plugin from Github or uses default if necessary
*/
async getConfig(context: Context): Promise<RerunCircleCIConfig> {
const repositoryConfig = await this.getAppConfig(context);
const config = repositoryConfig.rerunCircleCI;
return config;
}
}
4 changes: 4 additions & 0 deletions functions/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {MergeTask} from "./plugins/merge";
import {TriageTask} from "./plugins/triage";
import {SizeTask} from "./plugins/size";
import {TriagePRTask} from "./plugins/triagePR";
import {RerunCircleCITask} from "./plugins/rerun-circleci";


class Stream {
constructor(private store: FirebaseFirestore.Firestore) {
Expand Down Expand Up @@ -96,6 +98,7 @@ export interface Tasks {
triageTask: TriageTask;
triagePRTask: TriagePRTask;
sizeTask: SizeTask;
rerunCircleCiTask: RerunCircleCITask;
}

export function registerTasks(robot: Application, store: FirebaseFirestore.Firestore): Tasks {
Expand All @@ -106,6 +109,7 @@ export function registerTasks(robot: Application, store: FirebaseFirestore.Fires
triageTask: new TriageTask(robot, store),
triagePRTask: new TriagePRTask(robot, store),
sizeTask: new SizeTask(robot, store),
rerunCircleCiTask: new RerunCircleCITask(robot, store),
};
}

Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/angular-robot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,10 @@ triagePR:
- "effort*"
- "risk*"
- "comp: *"

# options for the rerun circleCI plugin
rerunCircleCI:
# set to true to disable
disabled: true
# the label which when added triggers a rerun of the default CircleCI workflow.
triggerRerunLabel: 'Trigger CircleCI Rerun'