• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1113439055

19 Dec 2023 09:01PM UTC coverage: 82.752% (-17.2%) from 99.964%
1113439055

Pull #4258

gitlab-ci

mender-test-bot
chore: Types update

Signed-off-by: Mender Test Bot <mender@northern.tech>
Pull Request #4258: chore: Types update

4326 of 6319 branches covered (0.0%)

8348 of 10088 relevant lines covered (82.75%)

189.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/js/components/settings/webhooks/configuration.js
1
// Copyright 2022 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useCallback, useEffect, useState } from 'react';
15

16
// material ui
17
import { Close as CloseIcon } from '@mui/icons-material';
18
import { Button, Divider, Drawer, IconButton, TextField } from '@mui/material';
19

20
import validator from 'validator';
21

22
import { EXTERNAL_PROVIDER } from '../../../constants/deviceConstants';
23
import { emptyWebhook } from '../../../constants/organizationConstants';
24
import MenderTooltip from '../../common/mendertooltip';
25
import { HELPTOOLTIPS, MenderHelpTooltip } from '../../helptips/helptooltips';
26

27
const WebhookConfiguration = ({ adding, editing, onCancel, onSubmit, webhook = { ...emptyWebhook } }) => {
5✔
28
  const [description, setDescription] = useState('');
2✔
29
  const [secret, setSecret] = useState('');
2✔
30
  const [secretFormatError, setSecretFormatError] = useState('');
2✔
31
  const [url, setUrl] = useState('');
2✔
32

33
  const { credentials = {}, description: hookDescription = '', id = 'new' } = webhook;
2!
34
  const {
35
    [EXTERNAL_PROVIDER.webhook.credentialsType]: { secret: hookSecret = '', url: hookUrl = '' }
×
36
  } = credentials;
2✔
37
  useEffect(() => {
2✔
38
    setDescription(hookDescription);
2✔
39
    setUrl(hookUrl);
2✔
40
    setSecret(hookSecret);
2✔
41
  }, [adding, editing, hookDescription, hookSecret, hookUrl]);
42

43
  useEffect(() => {
2✔
44
    setSecretFormatError(!secret || validator.isHexadecimal(secret) ? '' : 'The secret has to be entered as a hexadecimal string');
2!
45
  }, [secret]);
46

47
  const onSubmitClick = useCallback(() => {
2✔
48
    let webhookConfig = {
×
49
      id,
50
      provider: EXTERNAL_PROVIDER.webhook.provider,
51
      credentials: { type: EXTERNAL_PROVIDER.webhook.credentialsType, [EXTERNAL_PROVIDER.webhook.credentialsType]: { secret, url } },
52
      description
53
    };
54
    if (editing) {
×
55
      // eslint-disable-next-line no-unused-vars
56
      const { credentials, description, ...remainder } = webhookConfig;
×
57
      webhookConfig = { ...remainder, credentials: { ...credentials, [EXTERNAL_PROVIDER.webhook.credentialsType]: { url } } };
×
58
    }
59
    onSubmit(webhookConfig);
×
60
  }, [description, editing, id, onSubmit, secret, url]);
61

62
  const secretInputTip = editing ? 'Cannot edit webhook secrets after they have been saved' : 'The secret has to be entered as a hexadecimal string';
2!
63
  const descriptionInput = (
64
    <TextField
2✔
65
      label="Description (optional)"
66
      id="webhook-description"
67
      disabled={editing}
68
      multiline
69
      onChange={e => setDescription(e.target.value)}
×
70
      value={description}
71
    />
72
  );
73
  const urlInput = <TextField label="Url" id="webhook-name" disabled={editing} value={url} onChange={e => setUrl(e.target.value)} />;
2✔
74
  const isSubmitDisabled = !url || (editing && url === hookUrl);
2!
75
  return (
2✔
76
    <>
77
      <div className="flexbox column" style={{ width: 500 }}>
78
        {editing ? (
2!
79
          <MenderTooltip arrow placement="bottom-start" title="Cannot edit webhook url after it has been saved">
80
            {urlInput}
81
          </MenderTooltip>
82
        ) : (
83
          urlInput
84
        )}
85
        {editing ? (
2!
86
          <MenderTooltip arrow placement="bottom-start" title="Cannot edit webhook description after it has been saved">
87
            {descriptionInput}
88
          </MenderTooltip>
89
        ) : (
90
          descriptionInput
91
        )}
92
        <MenderTooltip arrow placement="bottom-start" title={secretInputTip}>
93
          <TextField
94
            label="Secret (optional)"
95
            id="webhook-secret"
96
            disabled={editing}
97
            error={!editing && !!secretFormatError}
2!
98
            helperText={!editing && secretFormatError}
2!
99
            onChange={e => setSecret(e.target.value)}
×
100
            type={editing ? 'password' : 'text'}
2!
101
            value={secret}
102
          />
103
        </MenderTooltip>
104
      </div>
105
      <Divider className="margin-top-large" light />
106
      <div className="flexbox centered margin-top" style={{ justifyContent: 'flex-end' }}>
107
        <Button onClick={onCancel}>Cancel</Button>
108
        {!editing && (
2!
109
          <Button color="secondary" variant="contained" disabled={isSubmitDisabled} onClick={onSubmitClick}>
110
            Submit
111
          </Button>
112
        )}
113
      </div>
114
    </>
115
  );
116
};
117

118
export const WebhookCreation = ({ adding, onCancel, ...props }) => (
5✔
119
  <Drawer anchor="right" open={adding} PaperProps={{ style: { minWidth: 600, width: '50vw' } }}>
3✔
120
    <div className="flexbox center-aligned margin-bottom-small space-between">
121
      <div className="flexbox center-aligned">
122
        <h3>Add a webhook</h3>
123
        <MenderHelpTooltip id={HELPTOOLTIPS.webhooks.id} />
124
      </div>
125
      <IconButton onClick={onCancel} aria-label="close">
126
        <CloseIcon />
127
      </IconButton>
128
    </div>
129
    <Divider />
130
    <WebhookConfiguration adding={adding} onCancel={onCancel} {...props} />
131
  </Drawer>
132
);
133

134
export default WebhookConfiguration;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc