This repository was archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Add CircleCI workflow rerun when a specific label is added to a PR #41
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
ocombe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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