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
fix(rerun-ci): Fixes the rerun-ci tooling to request a rerun from CircleCI #71
Closed
Closed
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -50,25 +50,24 @@ export class RerunCircleCITask extends Task { | |||||
| 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/v2/project/gh/${owner}/${repo}/pipeline?circle-token=${CIRCLE_CI_TOKEN}`; | ||||||
| const id = this.getCircleCiWorkflowIdForPullRequest(context); | ||||||
| const url = `https://circleci.com/api/v2/workflow/${id}/rerun?circle-token=${CIRCLE_CI_TOKEN}`; | ||||||
| try { | ||||||
| const response = await fetch(circleCiUrl, { | ||||||
| const response = await (await fetch(url, { | ||||||
| method: 'POST', | ||||||
| headers: { | ||||||
| 'Content-Type': 'application/json', | ||||||
| }, | ||||||
| body: JSON.stringify({ | ||||||
| branch: `pull/${pullRequest.number}/head`, | ||||||
| // Always rerun only the steps which failed. | ||||||
| from_failed: true | ||||||
| }) | ||||||
| }); | ||||||
| // Properly handled failures in the CircleCI requests are returned with an HTTP response code | ||||||
| // of 200 and json response with a `:message` key mapping to the failure message. If | ||||||
| // `:message` is not defined, the API request was successful. | ||||||
| const errMessage = (await response.json())[':message']; | ||||||
| if (errMessage) { | ||||||
| throw Error(errMessage); | ||||||
| } | ||||||
| })).json(); | ||||||
|
|
||||||
| assertNoErrorsInCircleCiResponse(response); | ||||||
|
|
||||||
| } catch (err) { | ||||||
| this.logError(err); | ||||||
| const error: TypeError = err; | ||||||
| context.github.issues.createComment({ | ||||||
| body: `@${sender.login} the CircleCI rerun you requested failed. See details below: | ||||||
|
|
@@ -98,4 +97,73 @@ ${error.message} | |||||
| const repositoryConfig = await this.getAppConfig(context); | ||||||
| return repositoryConfig.rerunCircleCI; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Get the CircleCI workflow id of the first discovered CircleCI status in the statuses. Since | ||||||
| * only one workflow is run on each PR, all CircleCI statuses will track back to the same | ||||||
| * workflow id. | ||||||
| */ | ||||||
| private async getCircleCiWorkflowIdForPullRequest(context: Context) { | ||||||
| /** The target url of the discovered CircleCI status. */ | ||||||
| let targetUrl: string; | ||||||
| /** The pull request which triggered the bot action. */ | ||||||
| const pullRequest: Github.PullRequestsGetResponse = context.payload.pull_request; | ||||||
| /** The owner and repository name. */ | ||||||
| const {owner, repo} = context.repo(); | ||||||
| /** The list of statuses for the latest ref of the PR. */ | ||||||
| const {statuses} = (await context.github.repos.getCombinedStatusForRef({ | ||||||
| owner, repo, ref: pullRequest.head.ref | ||||||
| })).data; | ||||||
|
|
||||||
| for (const status of statuses) { | ||||||
| if (status.context.startsWith('ci/circleci:')) { | ||||||
| targetUrl = status.target_url; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (targetUrl === undefined) { | ||||||
| throw Error('No status for a CircleCI workflow was found on the pull request to be rerun.'); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * The matcher results of the regex to select the job ID of the job which the status represents. | ||||||
| */ | ||||||
| const jobIdMatcher = targetUrl.match(`https://circleci.com/gh/${owner}/${repo}/(\d+)\?`); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this string is converted to the RegExp you intend it to be converted 😁 You were probably going for something like:
Suggested change
|
||||||
|
|
||||||
| if (jobIdMatcher === null) { | ||||||
| throw Error('Unable to determine the job ID for the CircleCI job creating the status'); | ||||||
| } | ||||||
|
|
||||||
| /** The job ID. */ | ||||||
| const job = jobIdMatcher[0]; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't you mean:
Suggested change
|
||||||
| /** The full url of the API request to CircleCI. */ | ||||||
| const url = `https://circleci.com/api/v2/project/gh/${owner}/${repo}/job/${job}?circle-token=${CIRCLE_CI_TOKEN}`; | ||||||
| /** The API response from the CircleCI request. */ | ||||||
| const response = (await (await fetch(url, { | ||||||
| method: 'GET', | ||||||
| headers: { | ||||||
| 'Content-Type': 'application/json', | ||||||
| } | ||||||
| })).json()); | ||||||
|
|
||||||
| assertNoErrorsInCircleCiResponse(response); | ||||||
|
|
||||||
| return response.latest_workflow.id; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| /** | ||||||
| * Checks the provided response from CircleCI's API to determine if it is an error message. | ||||||
| * | ||||||
| * Properly handled failures in the CircleCI requests are returned with an HTTP response code of 200 | ||||||
| * and json response with a `:message` key mapping to the failure message. If `:message` is not | ||||||
| * defined, the API request was successful. | ||||||
| */ | ||||||
| function assertNoErrorsInCircleCiResponse(response: any) { | ||||||
| if (response[':message'] !== undefined) { | ||||||
| throw Error(response[':message']); | ||||||
| } | ||||||
| } | ||||||
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.
Super-nit: This could be simplified as: