8000 refactor action.yml · coder/start-workspace-action@4707086 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4707086

Browse files
committed
refactor action.yml
1 parent 0dfc1ed commit 4707086

File tree

1 file changed

+22
-119
lines changed

1 file changed

+22
-119
lines changed

action.yml

Lines changed: 22 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ inputs:
1010
required: true
1111
github-username:
1212
description: 'GitHub username of the user for whom the workspace is being started'
13-
required: true
13+
required: false
14+
coder-username:
15+
description: 'Coder username'
16+
required: false
1417
coder-url:
1518
description: 'Coder URL'
1619
required: true
@@ -37,145 +40,45 @@ runs:
3740
script: |
3841
const issueNumber = Number('${{ inputs.github-issue-number }}');
3942
const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
40-
await github.rest.issues.createComment({
43+
const comment = await github.rest.issues.createComment({
4144
owner: context.repo.owner,
4245
repo: context.repo.repo,
4346
issue_number: issueNumber,
4447
body: `Starting a Coder workspace. You can track the progress [here](${runUrl}).`
4548
});
49+
core.setOutput('comment_id', comment.data.id);
50+
core.setOutput('run_url', runUrl);
4651
47-
- name: Check if Coder CLI is available
48-
id: check-coder
52+
- name: Start workspace
4953
shell: bash
50-
run: |
51-
if command -v coder &> /dev/null; then
52-
echo "Coder CLI is already installed"
53-
echo "coder_installed=true" >> $GITHUB_OUTPUT
54-
else
55-
echo "Coder CLI is not installed"
56-
echo "coder_installed=false" >> $GITHUB_OUTPUT
57-
fi
58-
59-
- name: Install Coder CLI
60-
if: steps.check-coder.outputs.coder_installed != 'true'
61-
uses: coder/setup-action@4a607a8113d4e676e2d7c34caa20a814bc88bfda
62-
with:
63-
access_url: ${{ inputs.coder-url }}
64-
coder_session_token: ${{ inputs.coder-token }}
65-
66-
- name: Get GitHub username and id
67-
id: github-user
68-
uses: actions/github-script@v6
6954
env:
7055
GITHUB_USERNAME: ${{ inputs.github-username }}
71-
with:
72-
github-token: ${{ inputs.github-token }}
73-
script: |
74-
const githubUsername = (process.env.GITHUB_USERNAME ?? '').trim();
75-
const githubUser = await github.rest.users.getByUsername({
76-
username: githubUsername
77-
});
78-
core.setOutput('github_username', githubUsername);
79-
core.setOutput('github_user_id', githubUser.data.id);
80-
81-
- name: Get Coder Users List
82-
id: coder-user-list
83-
shell: bash
84-
run: |
85-
{
86-
echo "cmd_output<<EOF"
87-
coder users list \
88-
--url '${{ inputs.coder-url }}' \
89-
--token '${{ inputs.coder-token }}' \
90-
--github-user-id '${{ steps.github-user.outputs.github_user_id }}' \
91-
--column username
92-
echo "EOF"
93-
} >> $GITHUB_OUTPUT
94-
95-
- name: Get Coder User
96-
id: coder-user
97-
uses: actions/github-script@v6
98-
env:
99-
CODER_USERS_LIST_OUTPUT: ${{ steps.coder-user-list.outputs.cmd_output }}
100-
with:
101-
github-token: ${{ inputs.github-token }}
102-
script: |
103-
const listOutput = (process.env.CODER_USERS_LIST_OUTPUT ?? '').trim();
104-
105-
const lines = listOutput.split('\n')
106-
if (lines.length < 2) {
107-
console.log('No Coder username mapping found for GitHub user')
108-
return
109-
}
110-
if (lines.length > 2) {
111-
console.log('Multiple Coder usernames found for GitHub user. Using the first one.')
112-
}
113-
const coderUsername = lines[1].trim()
114-
core.setOutput('coder_username', coderUsername)
115-
core.setOutput('has_mapping', String(coderUsername.length > 0))
116-
117-
- name: No user mapping comment
118-
if: steps.coder-user.outputs.has_mapping != 'true'
119-
uses: actions/github-script@v6
120-
with:
121-
github-token: ${{ inputs.github-token }}
122-
script: |
123-
const issueNumber = Number('${{ inputs.github-issue-number }}');
124-
const githubUsername = '${{ steps.github-user.outputs.github_username }}';
125-
const externalAuthPage = '${{ inputs.coder-url }}/settings/external-auth';
126-
const errMsg = `No matching Coder user found for GitHub user \`${githubUsername}\`. Please connect your GitHub account with Coder: ${externalAuthPage}`;
127-
require('fs').appendFileSync(process.env["GITHUB_ENV"], `ERROR_MSG=${errMsg}\n`);
128-
core.setFailed(errMsg)
129-
130-
- name: Create parameters file
131-
shell: bash
132-
run: |
133-
mkdir -p /tmp/coder
134-
echo '${{ inputs.parameters }}' > /tmp/coder/parameters.yml
135-
echo "PARAMETERS_FILE=/tmp/coder/parameters.yml" >> $GITHUB_ENV
136-
137-
- name: Start Coder workspace
138-
id: start-workspace
139-
shell: bash
140-
env:
56+
CODER_USERNAME: ${{ inputs.coder-username }}
14157
CODER_URL: ${{ inputs.coder-url }}
14258
CODER_TOKEN: ${{ inputs.coder-token }}
143-
TEMPLATE_NAME: ${{ inputs.template-name }}
14459
WORKSPACE_NAME: ${{ inputs.workspace-name }}
60+
GITHUB_STATUS_COMMENT_ID: ${{ steps.initial-comment.outputs.comment_id }}
61+
GITHUB_REPO_OWNER: ${{ context.repo.owner }}
62+
GITHUB_REPO_NAME: ${{ context.repo.repo }}
63+
GITHUB_TOKEN: ${{ inputs.github-token }}
64+
GITHUB_WORKFLOW_RUN_URL: ${{ steps.initial-comment.outputs.run_url }}
65+
TEMPLATE_NAME: ${{ inputs.template-name }}
66+
WORKSPACE_PARAMETERS: ${{ inputs.parameters }}
14567
run: |
146-
set -o pipefail
147-
set -o errexit
148-
149-
bash -c "yes '' || true" | coder --url '${{ inputs.coder-url }}' --token '${{ inputs.coder-token }}' create --yes --template '${{ inputs.template-name }}' '${{ inputs.workspace-name }}' --rich-parameter-file $PARAMETERS_FILE
150-
WORKSPACE_URL="${{ inputs.coder-url }}/${{ steps.coder-user.outputs.coder_username }}/${{ inputs.workspace-name }}"
151-
echo "workspace_url=${WORKSPACE_URL}" >> $GITHUB_OUTPUT
152-
153-
- name: Comment success
154-
uses: actions/github-script@v6
155-
with:
156-
github-token: ${{ inputs.github-token }}
157-
script: |
158-
const issueNumber = Number('${{ inputs.github-issue-number }}');
159-
const workspaceUrl = '${{ steps.start-workspace.outputs.workspace_url }}';
160-
await github.rest.issues.createComment({
161-
owner: context.repo.owner,
162-
repo: context.repo.repo,
163-
issue_number: issueNumber,
164-
body: `✅ Workspace has been successfully started! You can access it [here](${workspaceUrl}).`
165-
});
68+
node "${{ github.action_path }}/dist/index.js"
16669
16770
- name: Comment failure
16871
if: always() && (failure() || cancelled())
16972
uses: actions/github-script@v6
17073
with:
17174
github-token: ${{ inputs.github-token }}
17275
script: |
173-
const issueNumber = Number('${{ inputs.github-issue-number }}');
174-
const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
76+
const commentId = Number('${{ steps.initial-comment.outputs.comment_id }}');
77+
const runUrl = '${{ steps.initial-comment.outputs.run_url }}';
17578
const errorMsg = process.env["ERROR_MSG"];
176-
await github.rest.issues.createComment({
79+
await github.rest.issues.updateComment({
17780
owner: context.repo.owner,
17881
repo: context.repo.repo,
179-
issue_number: issueNumber,
180-
body: "❌ " + (errorMsg ?? `Failed to start workspace. Please check the [action logs](${runUrl}) for details.`)
82+
comment_id: commentId,
83+
body: "❌ " + (errorMsg ?? `Failed to start the workspace. Please check the [action logs](${runUrl}) for details.`)
18184
});

0 commit comments

Comments
 (0)
0