Skip to content

Commit bd0bcad

Browse files
committed
update response codes + force unlock option
1 parent ad389ee commit bd0bcad

3 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/controllers/ControllerV1.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class ControllerV1 extends Controller {
3434
@Get()
3535
public async getState(
3636
@Request() request: HttpRequest,
37-
@Res() res: TsoaResponse<200 | 400 | 401 | 404, any>,
37+
@Res() res: TsoaResponse<200 | 401 | 403 | 404, any>,
3838
): Promise<any> {
3939
try {
4040
const identity = await this.githubService.getIdentity(request);
@@ -55,7 +55,7 @@ export class ControllerV1 extends Controller {
5555
@Query('ID') id: string,
5656
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
5757
@Body() state: any,
58-
@Res() res: TsoaResponse<200 | 400 | 401 | 404 | 409, void>,
58+
@Res() res: TsoaResponse<200 | 400 | 401 | 403 | 404 | 409, void>,
5959
): Promise<void> {
6060
try {
6161
const stateLockRequest = await this.stateService.getRequest(id);
@@ -75,7 +75,7 @@ export class ControllerV1 extends Controller {
7575
public async lockState(
7676
@Request() request: HttpRequest,
7777
@Body() lockRequest: StateLockRequest,
78-
@Res() res: TsoaResponse<200 | 400 | 401 | 404 | 409, boolean>,
78+
@Res() res: TsoaResponse<200 | 401 | 403 | 404 | 409, boolean>,
7979
): Promise<boolean> {
8080
try {
8181
const stateLockRequest = await this.stateService.saveRequest(lockRequest);
@@ -95,12 +95,13 @@ export class ControllerV1 extends Controller {
9595
public async unlockState(
9696
@Request() request: HttpRequest,
9797
@Body() lockRequest: StateLockRequest,
98-
@Res() res: TsoaResponse<200 | 400 | 401 | 404 | 409, boolean>,
98+
@Res() res: TsoaResponse<200 | 401 | 403 | 404 | 409, boolean>,
99+
@Query('force') force = false,
99100
): Promise<boolean> {
100101
try {
101102
const stateLockRequest = await this.stateService.getRequest(lockRequest.ID);
102103
const identity = await this.githubService.getIdentity(request, stateLockRequest);
103-
await this.stateService.unlockState(identity, stateLockRequest);
104+
await this.stateService.unlockState(identity, stateLockRequest, force);
104105
const response = res(200, true);
105106
return response;
106107
} catch (e) {

src/services/GithubService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class GithubService {
5656

5757
if (!password) {
5858
console.warn(`Missing password from authorization token`);
59-
throw new TerraformError(401);
59+
throw new TerraformError(403);
6060
}
6161

6262
if (username) {
@@ -70,7 +70,7 @@ export class GithubService {
7070
`Username must be in the format of \`[{owner}/{repository}][@{workspace}]\``,
7171
username,
7272
);
73-
throw new TerraformError(400);
73+
throw new TerraformError(403);
7474
}
7575

7676
if (repo.indexOf('@') !== -1) {
@@ -89,7 +89,7 @@ export class GithubService {
8989
} catch (e) {
9090
if (e instanceof Error) {
9191
console.warn(`Error inferring identity`, e);
92-
throw new TerraformError(401);
92+
throw new TerraformError(403);
9393
}
9494
throw e;
9595
}

src/services/StateService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export class StateService {
161161
public unlockState = async (
162162
identity: IdentityWithToken,
163163
stateLockRequest: StateLockRequest,
164+
force: boolean,
164165
): Promise<void> => {
165166
const lockedBy = crypto.createHash('sha256').update(identity.token, 'utf8').digest('base64');
166167

@@ -182,7 +183,7 @@ export class StateService {
182183

183184
const [stateLock] = stateLocks.Items;
184185

185-
if (stateLock.attrs.lockedBy !== lockedBy) {
186+
if (stateLock.attrs.lockedBy !== lockedBy && !force) {
186187
console.warn(
187188
`State is locked by ${identity.meta.name} for ${identity.owner}/${identity.repo} on workspace ${identity.workspace}.`,
188189
);

0 commit comments

Comments
 (0)