* `startBuild` returns a Build object * https://docs.aws.amazon.com/codebuild/latest/APIReference/API_Build.html * @param {string} ciContentsResponse - The response from the GitHub repository contents API for buildspec.yml * @param {GitHubPullRequestWebhookPayload|GitHubPushWebhookPayload} event
(ciContentsResponse, event)
| 108 | * @param {GitHubPullRequestWebhookPayload|GitHubPushWebhookPayload} event |
| 109 | */ |
| 110 | async function triggerBuild(ciContentsResponse, event) { |
| 111 | console.log('Starting CodeBuild run'); |
| 112 | |
| 113 | // ciContentsResponse is the JSON body response from a Contents API request |
| 114 | // to GitHub for a buildspec.yml file in the project that triggered the |
| 115 | // webhook event. It contains a base 64 encoded string which is the contents |
| 116 | // of that file. |
| 117 | /** @type {ReposGetContentResponseData} */ |
| 118 | const ghData = JSON.parse(ciContentsResponse); |
| 119 | // TODO Not sure why it thinks ghData.content doesn't exist |
| 120 | // @ts-ignore |
| 121 | const buildspec = Buffer.from(ghData.content, 'base64').toString('utf8'); |
| 122 | |
| 123 | // Only trigger builds for repositories where the buildspec appears to be |
| 124 | // be designed for use with CI. Look for an `PRX_` string as a test. |
| 125 | if (!buildspec.includes('PRX_')) { |
| 126 | console.log('Skipping unsupported buildspec.yml'); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | const commitRef = eventIsPullRequest(event) |
| 131 | ? event.pull_request.head.sha |
| 132 | : event.after; |
| 133 | |
| 134 | const environmentVariables = [ |
| 135 | { |
| 136 | name: 'PRX_REPO', |
| 137 | value: event.repository.full_name, // e.g. PRX/MyApp |
| 138 | }, |
| 139 | { |
| 140 | name: 'PRX_COMMIT', |
| 141 | value: commitRef, |
| 142 | }, |
| 143 | ]; |
| 144 | |
| 145 | if (eventIsPullRequest(event)) { |
| 146 | // Pull requests are test-only builds. The pull request number is not |
| 147 | // used by the build process, but needs to be passed along to the |
| 148 | // callback for setting the GitHub status. |
| 149 | |
| 150 | event.repository.clone_url; |
| 151 | |
| 152 | const num = event.pull_request.number; |
| 153 | const title = event.pull_request.title; |
| 154 | const branch = event.pull_request.head.ref; |
| 155 | const baseBranch = event.pull_request.base.ref; |
| 156 | const author = event.pull_request.user.login; |
| 157 | const action = event.action; |
| 158 | environmentVariables.push({ name: 'PRX_GITHUB_PR', value: `${num}` }); |
| 159 | environmentVariables.push({ name: 'PRX_GITHUB_PR_TITLE', value: title }); |
| 160 | environmentVariables.push({ |
| 161 | name: 'PRX_GITHUB_PR_BASE_BRANCH', |
| 162 | value: baseBranch, |
| 163 | }); |
| 164 | environmentVariables.push({ name: 'PRX_GITHUB_PR_AUTHOR', value: author }); |
| 165 | environmentVariables.push({ name: 'PRX_GITHUB_ACTION', value: action }); |
| 166 | environmentVariables.push({ name: 'PRX_BRANCH', value: branch }); |
| 167 |
no test coverage detected