|  | 
|  | 1 | +import {OctokitResponse} from '@octokit/types' | 
|  | 2 | +import axios, {AxiosResponse} from 'axios' | 
|  | 3 | +import cachedProperty from './cached-property' | 
|  | 4 | +import * as types from './types' | 
|  | 5 | + | 
|  | 6 | + | 
|  | 7 | +class RetestCommand { | 
|  | 8 | +  public env: types.Env | 
|  | 9 | +  public name = '' | 
|  | 10 | + | 
|  | 11 | +  constructor(env: types.Env) { | 
|  | 12 | +    this.env = env | 
|  | 13 | +  } | 
|  | 14 | + | 
|  | 15 | +  getPR = async (): Promise<types.PR | void> => { | 
|  | 16 | +    if (!this.env.pr || !this.env.pr.url) { | 
|  | 17 | +      return | 
|  | 18 | +    } | 
|  | 19 | +    const response: OctokitResponse<any> = await this.env.octokit.request(this.env.pr.url) | 
|  | 20 | +    const data = response.data | 
|  | 21 | +    if (!data) return | 
|  | 22 | +    return { | 
|  | 23 | +      number: data.number, | 
|  | 24 | +      branch: data.head.ref, | 
|  | 25 | +      commit: data.head.sha, | 
|  | 26 | +    } | 
|  | 27 | +  } | 
|  | 28 | + | 
|  | 29 | +  @cachedProperty | 
|  | 30 | +  get isRetest(): boolean { | 
|  | 31 | +    let isRetest = false | 
|  | 32 | +    this.env.comment.body.split('\r\n').forEach((line: string) => { | 
|  | 33 | +      if (line.startsWith('/retest')) { | 
|  | 34 | +        if (!line.startsWith('/retest envoy')) { | 
|  | 35 | +          isRetest = true | 
|  | 36 | +          return | 
|  | 37 | +        } | 
|  | 38 | +      } | 
|  | 39 | +    }) | 
|  | 40 | +    return isRetest | 
|  | 41 | +  } | 
|  | 42 | + | 
|  | 43 | +  retest = async (): Promise<number> => { | 
|  | 44 | +    if (!this.env) { | 
|  | 45 | +      console.error(`Failed parsing env`) | 
|  | 46 | +      return 0 | 
|  | 47 | +    } | 
|  | 48 | +    if (!this.isRetest) return 0 | 
|  | 49 | +    const pr = await this.getPR() | 
|  | 50 | +    if (!pr) { | 
|  | 51 | +      return 0 | 
|  | 52 | +    } | 
|  | 53 | +    const retestables = await this.getRetestables(pr) | 
|  | 54 | +    if (Object.keys(retestables).length === 0) { | 
|  | 55 | +      return 0 | 
|  | 56 | +    } | 
|  | 57 | +    await this.retestRuns(pr, retestables) | 
|  | 58 | +    return Object.keys(retestables).length | 
|  | 59 | +  } | 
|  | 60 | + | 
|  | 61 | +  retestExternal = async (check: types.Retest): Promise<any | void> => { | 
|  | 62 | +    let response: AxiosResponse | 
|  | 63 | +    if (check.method == 'patch') { | 
|  | 64 | +      try { | 
|  | 65 | +        response = await axios.patch(check.url, {}, check.config) | 
|  | 66 | +        /* eslint-disable  prettier/prettier */ | 
|  | 67 | +      } catch (error: any) { | 
|  | 68 | +        if (!axios.isAxiosError(error) || !error.response) { | 
|  | 69 | +          console.error('No response received') | 
|  | 70 | +          return | 
|  | 71 | +        } | 
|  | 72 | +        console.error(`External API call failed: ${check.url}`) | 
|  | 73 | +        console.error(error.response.status) | 
|  | 74 | +        console.error(error.response.data) | 
|  | 75 | +        return | 
|  | 76 | +      } | 
|  | 77 | +    } else { | 
|  | 78 | +      return | 
|  | 79 | +    } | 
|  | 80 | +    return response.data | 
|  | 81 | +  } | 
|  | 82 | + | 
|  | 83 | +  retestOctokit = async (check: types.Retest): Promise<void> => { | 
|  | 84 | +    const rerunURL = `POST ${check.url}/rerun-failed-jobs` | 
|  | 85 | +    const rerunResponse = await this.env.octokit.request(rerunURL) | 
|  | 86 | +    if ([200, 201].includes(rerunResponse.status)) { | 
|  | 87 | +      console.debug(`Retry success: (${check.name})`) | 
|  | 88 | +    } else { | 
|  | 89 | +      console.error(`Retry failed: (${check.name}) ... ${rerunResponse.status}`) | 
|  | 90 | +    } | 
|  | 91 | +  } | 
|  | 92 | + | 
|  | 93 | +  retestRuns = async (pr: types.PR, retestables: Array<types.Retest>): Promise<void> => { | 
|  | 94 | +    console.debug(`Running /retest command for PR #${pr.number}`) | 
|  | 95 | +    console.debug(`PR branch: ${pr.branch}`) | 
|  | 96 | +    console.debug(`Latest PR commit: ${pr.commit}`) | 
|  | 97 | +    for (const check of retestables) { | 
|  | 98 | +      console.debug(`Retesting failed job: ${check.name}`) | 
|  | 99 | +      if (!check.octokit) { | 
|  | 100 | +        await this.retestExternal(check) | 
|  | 101 | +      } else { | 
|  | 102 | +        await this.retestOctokit(check) | 
|  | 103 | +      } | 
|  | 104 | +    } | 
|  | 105 | +  } | 
|  | 106 | + | 
|  | 107 | +  getRetestables = async (_: types.PR): Promise<Array<types.Retest>> => { | 
|  | 108 | +    return [] | 
|  | 109 | +  } | 
|  | 110 | + | 
|  | 111 | +  listWorkflowRunsForPR = async (pr: types.PR): Promise<types.WorkflowRunsType['data']['workflow_runs'] | void> => { | 
|  | 112 | +    const response: types.WorkflowRunsType = await this.env.octokit.actions.listWorkflowRunsForRepo({ | 
|  | 113 | +      owner: this.env.owner, | 
|  | 114 | +      repo: this.env.repo, | 
|  | 115 | +      branch: pr.branch, | 
|  | 116 | +    }) | 
|  | 117 | + | 
|  | 118 | +    const workflowRuns = response.data | 
|  | 119 | +    if (!workflowRuns) return | 
|  | 120 | + | 
|  | 121 | +    const runs = workflowRuns.workflow_runs | 
|  | 122 | +    if (!runs) return | 
|  | 123 | + | 
|  | 124 | +    return runs.filter((run: any) => { | 
|  | 125 | +      return run.head_sha === pr.commit | 
|  | 126 | +    }) | 
|  | 127 | +  } | 
|  | 128 | +} | 
|  | 129 | + | 
|  | 130 | +export default RetestCommand | 
0 commit comments