(options: MutationOptions<OperationVariables>)
| 151 | } |
| 152 | |
| 153 | async mutate(options: MutationOptions<OperationVariables>) { |
| 154 | try { |
| 155 | const result = await this.client.mutate(options); |
| 156 | return result; |
| 157 | } catch (error) { |
| 158 | const errors = error.graphQLErrors; |
| 159 | if (Array.isArray(errors) && errors.some(e => e.message === 'Ref cannot be created.')) { |
| 160 | const refName = options?.variables?.createRefInput?.name || ''; |
| 161 | const branchName = trimStart(refName, 'refs/heads/'); |
| 162 | if (branchName) { |
| 163 | await throwOnConflictingBranches(branchName, name => this.getBranch(name), API_NAME); |
| 164 | } |
| 165 | } else if ( |
| 166 | Array.isArray(errors) && |
| 167 | errors.some(e => |
| 168 | new RegExp( |
| 169 | `A ref named "refs/heads/${CMS_BRANCH_PREFIX}/.+?" already exists in the repository.`, |
| 170 | ).test(e.message), |
| 171 | ) |
| 172 | ) { |
| 173 | const refName = options?.variables?.createRefInput?.name || ''; |
| 174 | const sha = options?.variables?.createRefInput?.oid || ''; |
| 175 | const branchName = trimStart(refName, 'refs/heads/'); |
| 176 | if (branchName && branchName.startsWith(`${CMS_BRANCH_PREFIX}/`) && sha) { |
| 177 | try { |
| 178 | // this can happen if the branch wasn't deleted when the PR was merged |
| 179 | // we backup the existing branch just in case an re-run the mutation |
| 180 | await this.backupBranch(branchName); |
| 181 | await this.deleteBranch(branchName); |
| 182 | const result = await this.client.mutate(options); |
| 183 | return result; |
| 184 | } catch (e) { |
| 185 | console.log(e); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | throw new APIError(error.message, 500, 'GitHub'); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | async hasWriteAccess() { |
| 194 | const { repoOwner: owner, repoName: name } = this; |
no test coverage detected