forgejo-federation/templates/repo/actions/dispatch.tmpl
Mai-Lapyst 51735c415b Add support for workflow_dispatch (#3334)
Closes #2797

I'm aware of https://github.com/go-gitea/gitea/pull/28163 exists, but since I had it laying around on my drive and collecting dust, I might as well open a PR for it if anyone wants the feature a bit sooner than waiting for upstream to release it or to be a forgejo "native" implementation.

This PR Contains:
- Support for the `workflow_dispatch` trigger
- Inputs: boolean, string, number, choice

Things still to be done:
- [x] API Endpoint `/api/v1/<org>/<repo>/actions/workflows/<workflow id>/dispatches`
- ~~Fixing some UI bugs I had no time figuring out, like why dropdown/choice inputs's menu's behave weirdly~~ Unrelated visual bug with dropdowns inside dropdowns
- [x] Fix bug where opening the branch selection submits the form
- [x] Limit on inputs to render/process

Things not in this PR:
- Inputs: environment (First need support for environments in forgejo)

Things needed to test this:
- A patch for https://code.forgejo.org/forgejo/runner to actually consider the inputs inside the workflow.
  ~~One possible patch can be seen here: https://code.forgejo.org/Mai-Lapyst/runner/src/branch/support-workflow-inputs~~
  [PR](https://code.forgejo.org/forgejo/runner/pulls/199)

![image](/attachments/2db50c9e-898f-41cb-b698-43edeefd2573)

## Testing

- Checkout PR
- Setup new development runner with [this PR](https://code.forgejo.org/forgejo/runner/pulls/199)
- Create a repo with a workflow (see below)
- Go to the actions tab, select the workflow and see the notice as in the screenshot above
- Use the button + dropdown to run the workflow
  - Try also running it via the api using the `` endpoint
- ...
- Profit!

<details>
<summary>Example workflow</summary>

```yaml
on:
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log Level'
        required: true
        default: 'warning'
        type: choice
        options:
        - info
        - warning
        - debug
      tags:
        description: 'Test scenario tags'
        required: false
        type: boolean
      boolean_default_true:
        description: 'Test scenario tags'
        required: true
        type: boolean
        default: true
      boolean_default_false:
        description: 'Test scenario tags'
        required: false
        type: boolean
        default: false
      number1_default:
        description: 'Number w. default'
        default: '100'
        type: number
      number2:
        description: 'Number w/o. default'
        type: number
      string1_default:
        description: 'String w. default'
        default: 'Hello world'
        type: string
      string2:
        description: 'String w/o. default'
        required: true
        type: string

jobs:
  test:
    runs-on: docker
    steps:
      - uses: actions/checkout@v3
      - run: whoami
      - run: cat /etc/issue
      - run: uname -a
      - run: date
      - run: echo ${{ inputs.logLevel }}
      - run: echo ${{ inputs.tags }}
      - env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - run: echo "abc"
```
</details>

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3334
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Mai-Lapyst <mai-lapyst@noreply.codeberg.org>
Co-committed-by: Mai-Lapyst <mai-lapyst@noreply.codeberg.org>
2024-06-28 05:17:11 +00:00

99 lines
No EOL
4.1 KiB
Go HTML Template

<div class="ui info message tw-flex tw-items-center">
<span>
{{ctx.Locale.Tr "actions.workflow.dispatch.trigger_found"}}
</span>
<div class="ui dropdown custom tw-ml-4" id="workflow_dispatch_dropdown">
<button class="ui compact small basic button tw-flex">
<span class="text">{{ctx.Locale.Tr "actions.workflow.dispatch.run"}}</span>
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
</button>
<div class="menu">
<div class="message ui form">
<div class="field">
<label>{{ctx.Locale.Tr "actions.workflow.dispatch.use_from"}}</label>
{{template "repo/branch_dropdown" dict
"root" (dict
"IsViewBranch" true
"BranchName" .Repo.BranchName
"CommitID" .Repo.CommitID
"RepoLink" .Repo.RepoLink
"Repository" .Repo.Repository
)
"disableCreateBranch" true
"branchForm" "branch-dropdown-form"
"setAction" false
"submitForm" false
}}
</div>
<form method="post" action="{{.Repo.RepoLink}}/actions/manual" id="branch-dropdown-form">
{{range $i, $key := .CurWorkflowDispatchInputKeys}}
{{$val := index $.CurWorkflowDispatch.Inputs $key}}
<div class="{{if $val.Required}}required {{end}}field">
{{if eq $val.Type "boolean"}}
<div class="ui checkbox">
<label><strong>{{if $val.Description}}{{$val.Description}}{{else}}{{$key}}{{end}}</strong></label>
<input {{if $val.Required}}required{{end}} type="checkbox" name="inputs[{{$key}}]" {{if eq $val.Default "true"}}checked{{end}}>
</div>
{{else}}
<label>{{if $val.Description}}{{$val.Description}}{{else}}{{$key}}{{end}}</label>
{{if eq $val.Type "number"}}
<input {{if $val.Required}}required{{end}} type="number" name="inputs[{{$key}}]" {{if $val.Default}}value="{{$val.Default}}"{{end}}>
{{else if eq $val.Type "string"}}
<input {{if $val.Required}}required{{end}} type="text" name="inputs[{{$key}}]" {{if $val.Default}}value="{{$val.Default}}"{{end}}>
{{else if eq $val.Type "choice"}}
<div class="ui selection dropdown">
<input name="inputs[{{$key}}]" type="hidden" value="{{$val.Default}}">
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="text"></div>
<div class="menu">
{{range $opt := $val.Options}}
<div data-value="{{$opt}}" class="{{if eq $val.Default $opt}}active selected {{end}}item">{{$opt}}</div>
{{end}}
</div>
</div>
{{else}}
<strong>{{ctx.Locale.Tr "actions.workflow.dispatch.invalid_input_type" $val.Type}}</strong>
{{end}}
{{end}}
</div>
{{end}}
{{if .WarnDispatchInputsLimit}}
<div class="text yellow tw-mb-4">
{{svg "octicon-alert"}} {{ctx.Locale.Tr "actions.workflow.dispatch.warn_input_limit" .DispatchInputsLimit}}
</div>
{{end}}
{{.CsrfTokenHtml}}
<input type="hidden" name="ref" value="{{if $.Repo.BranchName}}{{$.Repo.BranchName}}{{else}}{{$.Repo.Repository.DefaultBranch}}{{end}}">
<input type="hidden" name="workflow" value="{{$.CurWorkflow}}">
<input type="hidden" name="actor" value="{{$.CurActor}}">
<input type="hidden" name="status" value="{{$.CurStatus}}">
<button type="submit" id="workflow-dispatch-submit" class="ui primary small compact button">{{ctx.Locale.Tr "actions.workflow.dispatch.run"}}</button>
</form>
</div>
</div>
</div>
<script>
window.addEventListener('load', () => {
const dropdown = $('#workflow_dispatch_dropdown');
const menu = dropdown.find('> .menu');
$(document.body).on('click', (ev) => {
if (!dropdown[0].contains(ev.target) && menu.hasClass('visible')) {
menu.transition({ animation: 'slide down out', duration: 200, queue: false });
}
});
dropdown.on('click', (ev) => {
const inMenu = $(ev.target).closest(menu).length !== 0;
if (inMenu) return;
ev.stopPropagation();
if (menu.hasClass('visible')) {
menu.transition({ animation: 'slide down out', duration: 200, queue: false });
} else {
menu.transition({ animation: 'slide down in', duration: 200, queue: true });
}
});
});
</script>
</div>