GitLabClient.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. const path = require("path");
  3. const { URL } = require("whatwg-url");
  4. const log = require("npmlog");
  5. const fetch = require("node-fetch");
  6. class GitLabClient {
  7. constructor(baseUrl = "https://gitlab.com/api/v4", token) {
  8. this.baseUrl = baseUrl;
  9. this.token = token;
  10. }
  11. createRelease({ owner, repo, name, tag_name: tagName, body }) {
  12. const releasesUrl = this.releasesUrl(owner, repo, "releases");
  13. log.silly("Requesting GitLab releases", releasesUrl);
  14. return fetch(releasesUrl, {
  15. method: "post",
  16. body: JSON.stringify({ name, tag_name: tagName, description: body }),
  17. headers: {
  18. "PRIVATE-TOKEN": this.token,
  19. "Content-Type": "application/json",
  20. },
  21. }).then(({ ok, status, statusText }) => {
  22. if (!ok) {
  23. log.error("gitlab", `Failed to create release\nRequest returned ${status} ${statusText}`);
  24. } else {
  25. log.silly("gitlab", "Created release successfully.");
  26. }
  27. });
  28. }
  29. releasesUrl(namespace, project) {
  30. return new URL(
  31. `${this.baseUrl}/${path.join("projects", encodeURIComponent(`${namespace}/${project}`), "releases")}`
  32. ).toString();
  33. }
  34. }
  35. module.exports.GitLabClient = GitLabClient;