|
| 1 | +import Controller from '@ember/controller'; |
| 2 | +import { action } from '@ember/object'; |
| 3 | +import { service } from '@ember/service'; |
| 4 | +import { tracked } from '@glimmer/tracking'; |
| 5 | + |
| 6 | +import { task } from 'ember-concurrency'; |
| 7 | + |
| 8 | +export default class NewTrustedPublisherController extends Controller { |
| 9 | + @service notifications; |
| 10 | + @service store; |
| 11 | + @service router; |
| 12 | + |
| 13 | + @tracked publisher = 'GitHub'; |
| 14 | + @tracked repositoryOwner = ''; |
| 15 | + @tracked repositoryName = ''; |
| 16 | + @tracked workflowFilename = ''; |
| 17 | + @tracked environment = ''; |
| 18 | + @tracked repositoryOwnerInvalid = false; |
| 19 | + @tracked repositoryNameInvalid = false; |
| 20 | + @tracked workflowFilenameInvalid = false; |
| 21 | + |
| 22 | + get crate() { |
| 23 | + return this.model.crate; |
| 24 | + } |
| 25 | + |
| 26 | + get publishers() { |
| 27 | + return ['GitHub']; |
| 28 | + } |
| 29 | + |
| 30 | + saveConfigTask = task(async () => { |
| 31 | + if (!this.validate()) return; |
| 32 | + |
| 33 | + let config = this.store.createRecord('trustpub-github-config', { |
| 34 | + crate: this.crate, |
| 35 | + repository_owner: this.repositoryOwner, |
| 36 | + repository_name: this.repositoryName, |
| 37 | + workflow_filename: this.workflowFilename, |
| 38 | + environment: this.environment || null, |
| 39 | + }); |
| 40 | + |
| 41 | + try { |
| 42 | + // Save the new config on the backend |
| 43 | + await config.save(); |
| 44 | + |
| 45 | + this.repositoryOwner = ''; |
| 46 | + this.repositoryName = ''; |
| 47 | + this.workflowFilename = ''; |
| 48 | + this.environment = ''; |
| 49 | + |
| 50 | + // Navigate back to the crate settings page |
| 51 | + this.notifications.success('Trusted Publishing configuration added successfully'); |
| 52 | + this.router.transitionTo('crate.settings', this.crate.id); |
| 53 | + } catch (error) { |
| 54 | + // Notify the user |
| 55 | + let message = 'An error has occurred while adding the Trusted Publishing configuration'; |
| 56 | + |
| 57 | + let detail = error.errors?.[0]?.detail; |
| 58 | + if (detail && !detail.startsWith('{')) { |
| 59 | + message += `: ${detail}`; |
| 60 | + } |
| 61 | + |
| 62 | + this.notifications.error(message); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + validate() { |
| 67 | + this.repositoryOwnerInvalid = !this.repositoryOwner; |
| 68 | + this.repositoryNameInvalid = !this.repositoryName; |
| 69 | + this.workflowFilenameInvalid = !this.workflowFilename; |
| 70 | + |
| 71 | + return !this.repositoryOwnerInvalid && !this.repositoryNameInvalid && !this.workflowFilenameInvalid; |
| 72 | + } |
| 73 | + |
| 74 | + @action resetRepositoryOwnerValidation() { |
| 75 | + this.repositoryOwnerInvalid = false; |
| 76 | + } |
| 77 | + |
| 78 | + @action resetRepositoryNameValidation() { |
| 79 | + this.repositoryNameInvalid = false; |
| 80 | + } |
| 81 | + |
| 82 | + @action resetWorkflowFilenameValidation() { |
| 83 | + this.workflowFilenameInvalid = false; |
| 84 | + } |
| 85 | +} |
0 commit comments