async triggerCircleCIRerun()

in functions/src/plugins/rerun-circleci.ts [44:92]


  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/v2/project/gh/${owner}/${repo}/pipeline?circle-token=${CIRCLE_CI_TOKEN}`;
    try {
      const response = await fetch(circleCiUrl, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          branch: `pull/${pullRequest.number}/head`,
        })
      });
      // 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);
      }
    } 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,
      }).catch(err => {
        throw err;
      });
    }
    await context.github.issues.removeLabel({
      name: config.triggerRerunLabel,
      number: pullRequest.number,
      owner: owner,
      repo: repo
    });
  }