-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 93035cd
Showing
7 changed files
with
474 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
name: Test | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
run-action: | ||
name: Run action | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Use Node.js 20.x | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 20.x | ||
cache: "npm" | ||
cache-dependency-path: package-lock.json | ||
|
||
- name: Install dependencies | ||
run: npm ci | ||
|
||
- name: Build | ||
run: npm run build | ||
|
||
- name: Run my action | ||
uses: ./ | ||
with: | ||
GITPOD_TOKEN: ${{ secrets.GITPOD_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: "Delete clean Gitpod workspaces" | ||
author: "Siddhant-K-code" | ||
description: "GitHub action designed to remove Gitpod workspaces that are not in a running state and do not contain any uncommitted or untracked file changes." | ||
inputs: | ||
GITPOD_TOKEN: | ||
description: "Gitpod Personal Access token" | ||
required: true | ||
outputs: | ||
success: | ||
description: "true|false based on if the script worked" | ||
runs: | ||
using: "node20" | ||
main: "src/main.js" | ||
branding: | ||
icon: "archive" | ||
color: "orange" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "delete-clean-workspaces", | ||
"version": "1.0.0", | ||
"description": "GitHub action designed to remove Gitpod workspaces that are not in a running state and do not contain any uncommitted or untracked file changes.", | ||
"main": "dist/main.js", | ||
"scripts": { | ||
"build": "npx tsc ./src/main.ts" | ||
}, | ||
"author": "Siddhant-K-code", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@actions/core": "^1.10.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.10.2", | ||
"axios": "^1.6.2", | ||
"typescript": "^5.3.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import axios from "axios"; | ||
import * as core from "@actions/core"; | ||
|
||
async function listWorkspaces(gitpodAccessToken: string) { | ||
try { | ||
const response = await axios.post( | ||
"https://api.gitpod.io/gitpod.experimental.v1.WorkspacesService/ListWorkspaces", | ||
{}, | ||
{ | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${gitpodAccessToken}`, | ||
}, | ||
} | ||
); | ||
|
||
core.debug("API Response: " + JSON.stringify(response.data)); | ||
|
||
const workspaces = response.data.result; // assuming the response is in the desired format | ||
const toDelete: string[] = []; | ||
|
||
if (!Array.isArray(workspaces)) { | ||
throw new Error("Expected an array of data"); | ||
} | ||
|
||
workspaces.forEach((workspace) => { | ||
if ( | ||
workspace.status.instance.status.phase === "PHASE_STOPPED" && | ||
!workspace.status.instance.status.gitStatus.totalUntrackedFiles && | ||
!workspace.status.instance.status.gitStatus.totalUncommittedFiles | ||
) { | ||
toDelete.push(workspace.status.instance.workspaceId); | ||
} | ||
}); | ||
|
||
return toDelete; | ||
} catch (error) { | ||
core.error(`Error in listWorkspaces: ${error}`); | ||
throw error; | ||
} | ||
} | ||
|
||
async function deleteWorkspace(workspaceId: string, gitpodAccessToken: string) { | ||
try { | ||
await axios.post( | ||
"https://api.gitpod.io/gitpod.experimental.v1.WorkspacesService/DeleteWorkspace", | ||
{ workspaceId }, | ||
{ | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${gitpodAccessToken}`, | ||
}, | ||
} | ||
); | ||
core.debug(`Deleted workspace: ${workspaceId}`); | ||
} catch (error) { | ||
core.error(`Error in deleteWorkspace: ${error}`); | ||
throw error; | ||
} | ||
} | ||
|
||
async function run() { | ||
try { | ||
const gitpodAccessToken = core.getInput("GITPOD_TOKEN", { required: true }); | ||
const deletedWorkspaces: string[] = []; | ||
|
||
if (!gitpodAccessToken) { | ||
throw new Error("Gitpod access token is required"); | ||
} | ||
|
||
const workspacesToDelete = await listWorkspaces(gitpodAccessToken); | ||
for (const workspaceId of workspacesToDelete) { | ||
// await deleteWorkspace(workspaceId, gitpodAccessToken); | ||
deletedWorkspaces.push(workspaceId); | ||
} | ||
|
||
if (deletedWorkspaces.length > 0) { | ||
core.summary | ||
.addHeading("Deleted Workspaces") | ||
.addList(deletedWorkspaces) | ||
.write(); | ||
} | ||
|
||
core.setOutput("success", "true"); | ||
} catch (error) { | ||
core.error((error as Error).message); | ||
core.setOutput("success", "false"); | ||
} | ||
} | ||
|
||
run(); |
Oops, something went wrong.