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

sineverba / docker-delete-tag / #26

23 Apr 2024 03:38PM UTC coverage: 100.0%. Remained the same
#26

push

semaphore

web-flow
Add filter for ID project (#15)

* Add filter for ID project

23 of 23 branches covered (100.0%)

Branch coverage included in aggregate %.

36 of 36 new or added lines in 1 file covered. (100.0%)

251 of 251 relevant lines covered (100.0%)

1.59 hits per line

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

100.0
/src/utils/utils.ts
1
/**
1✔
2
 * File to collect some utilities
1✔
3
 */
1✔
4
import winston from "winston";
1✔
5
import dotenvFlow from "dotenv-flow";
1✔
6

1✔
7
// Start env file
1✔
8
dotenvFlow.config({ silent: true });
1✔
9

1✔
10
/**
1✔
11
 * Calculates the log level based on the environment variable or defaults to "debug".
1✔
12
 * @returns The log level as a string.
1✔
13
 */
1✔
14
const getLogLevel = (): string => {
1✔
15
  /**
6✔
16
   * If the environment variable LOG_LEVEL is not set or is an empty string,
6✔
17
   * default to "debug".
6✔
18
   */
6✔
19
  if (!process.env.LOG_LEVEL || process.env.LOG_LEVEL === "") {
6✔
20
    return "debug";
1✔
21
  }
1✔
22
  /**
5✔
23
   * Otherwise, return the log level specified in the environment variable.
5✔
24
   */
5✔
25
  return process.env.LOG_LEVEL;
5✔
26
};
5✔
27

1✔
28
/**
1✔
29
 * Formats log messages for Winston.
1✔
30
 * @param logMessage The log message object containing information such as timestamp, level, and message.
1✔
31
 * @returns The formatted log message string.
1✔
32
 */
1✔
33
const formatLogMessage = (
1✔
34
  logMessage: winston.Logform.TransformableInfo,
1✔
35
): string =>
1✔
36
  `${logMessage.timestamp} - ${logMessage.level.toUpperCase()} - ${logMessage.message}`;
1✔
37

1✔
38
/**
1✔
39
 * Create a logger instance to be used throughout the project.
1✔
40
 */
1✔
41
const logger: winston.Logger = winston.createLogger({
1✔
42
  /**
1✔
43
   * Specifies the minimum level of messages to log.
1✔
44
   */
1✔
45
  level: "info",
1✔
46

1✔
47
  /**
1✔
48
   * Specifies the format of the log messages.
1✔
49
   */
1✔
50
  format: winston.format.combine(
1✔
51
    winston.format.splat(),
1✔
52
    winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
1✔
53
    /**
1✔
54
     * The log message format template.
1✔
55
     * @param {Object} log The log message object.
1✔
56
     * @param {string} log.message The log message.
1✔
57
     * @param {string} log.level The log level (e.g., 'info', 'error').
1✔
58
     * @param {string} log.service The name of the service.
1✔
59
     * @param {string} log.timestamp The timestamp of the log message.
1✔
60
     * @returns {string} The formatted log message.
1✔
61
     */
1✔
62
    winston.format.printf(formatLogMessage),
1✔
63
  ),
1✔
64

1✔
65
  /**
1✔
66
   * Specifies the default metadata to be included with log messages.
1✔
67
   */
1✔
68
  defaultMeta: { service: "docker-delete-tag" },
1✔
69

1✔
70
  /**
1✔
71
   * Specifies the transports (outputs) where log messages should be sent.
1✔
72
   */
1✔
73
  transports: [
1✔
74
    new winston.transports.Stream({
1✔
75
      /**
1✔
76
       * The output stream where log messages are written.
1✔
77
       */
1✔
78
      stream: process.stderr,
1✔
79

1✔
80
      /**
1✔
81
       * Specifies the minimum level of messages to log for this transport.
1✔
82
       */
1✔
83
      level: getLogLevel(),
1✔
84

1✔
85
      /**
1✔
86
       * Specifies whether this transport should be silent (i.e., not produce any output).
1✔
87
       */
1✔
88
      silent: process.env.NODE_ENV === "test",
1✔
89
    }),
1✔
90
  ],
1✔
91
});
1✔
92

1✔
93
/**
1✔
94
 * Checks the validity of tokens based on the following rules:
1✔
95
 *
1✔
96
 * - If both DOCKER_USERNAME and DOCKER_PASSWORD are defined and non-empty, returns true.
1✔
97
 * - If both DOCKER_USERNAME, DOCKER_PASSWORD, and GITLAB_TOKEN are defined and non-empty, returns true.
1✔
98
 * - If GITLAB_TOKEN is defined and non-empty, returns true.
1✔
99
 * - If DOCKER_USERNAME is defined but DOCKER_PASSWORD is not, or vice versa, returns false.
1✔
100
 *
1✔
101
 * @returns A boolean indicating whether the tokens are valid.
1✔
102
 *
1✔
103
 * @example
1✔
104
 *
1✔
105
 * // Returns true if both DOCKER_USERNAME and DOCKER_PASSWORD are defined and non-empty,
1✔
106
 * // or if GITLAB_TOKEN is defined and non-empty.
1✔
107
 * checkValidToken();
1✔
108
 *
1✔
109
 * process.env.DOCKER_USERNAME = 'username';
1✔
110
 * process.env.DOCKER_PASSWORD = 'password';
1✔
111
 * process.env.GITLAB_TOKEN = 'gitlab_token';
1✔
112
 *
1✔
113
 * // Returns true since all tokens are defined and non-empty.
1✔
114
 * checkValidToken();
1✔
115
 *
1✔
116
 * process.env.DOCKER_PASSWORD = ''; // Emptying DOCKER_PASSWORD
1✔
117
 *
1✔
118
 * // Returns false since DOCKER_PASSWORD is now empty.
1✔
119
 * checkValidToken();
1✔
120
 *
1✔
121
 * delete process.env.DOCKER_USERNAME; // Removing DOCKER_USERNAME
1✔
122
 *
1✔
123
 * // Returns true since GITLAB_TOKEN is still defined and non-empty.
1✔
124
 * checkValidToken();
1✔
125
 */
1✔
126
const checkValidToken = (): boolean => {
1✔
127
  return (
5✔
128
    !!(
5✔
129
      process.env.DOCKER_USERNAME &&
5✔
130
      process.env.DOCKER_PASSWORD &&
4✔
131
      process.env.DOCKER_USERNAME.trim() !== "" &&
3✔
132
      process.env.DOCKER_PASSWORD.trim() !== ""
3✔
133
    ) || !!process.env.GITLAB_TOKEN
2✔
134
  );
5✔
135
};
5✔
136

1✔
137
/**
1✔
138
 * Filters an array of projects based on the provided criteria and returns the ID of the first matching project.
1✔
139
 *
1✔
140
 * @param projects An array containing project objects.
1✔
141
 * @returns The ID of the first project matching the criteria, converted to a string, or null if no matching project is found.
1✔
142
 *
1✔
143
 * @example
1✔
144
 *
1✔
145
 * const projects = [
1✔
146
 *   { id: 1, path: 'project1' },
1✔
147
 *   { id: 2, path: 'project2' },
1✔
148
 *   { id: 3, path: 'project3' }
1✔
149
 * ];
1✔
150
 *
1✔
151
 * process.env.PROJECT = 'project2';
1✔
152
 *
1✔
153
 * // Returns '2' since it finds the project with path 'project2' and returns its ID as a string.
1✔
154
 * filterProject(projects);
1✔
155
 *
1✔
156
 * process.env.PROJECT = 'project4';
1✔
157
 *
1✔
158
 * // Returns null since there's no project with path 'project4'.
1✔
159
 * filterProject(projects);
1✔
160
 */
1✔
161
const filterProject = (projects: any): string | null =>
1✔
162
  projects
2✔
163
    .filter((project: any) => project.path === process.env.PROJECT)[0]
2✔
164
    ?.id.toString() || null;
2✔
165

1✔
166
export {
1✔
167
  getLogLevel,
1✔
168
  formatLogMessage,
1✔
169
  logger,
1✔
170
  checkValidToken,
1✔
171
  filterProject,
1✔
172
};
1✔
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

© 2026 Coveralls, Inc