|
| 1 | +const parseGithubUrl = require('parse-github-url'); |
| 2 | +const urlJoin = require('url-join'); |
| 3 | +const got = require('got'); |
| 4 | +const debug = require('debug')('semantic-release:github'); |
| 5 | +const resolveConfig = require('./resolve-config'); |
| 6 | + |
| 7 | +module.exports = async (pluginConfig, {repositoryUrl}, {gitHead, gitTag, notes}, logger) => { |
| 8 | + const {gitlabToken, gitlabUrl, gitlabApiPathPrefix} = resolveConfig(pluginConfig); |
| 9 | + const {name: repo, owner} = parseGithubUrl(repositoryUrl); |
| 10 | + |
| 11 | + debug('release name: %o', gitTag); |
| 12 | + debug('release ref: %o', gitHead); |
| 13 | + |
| 14 | + try { |
| 15 | + // Test if the tag already exists |
| 16 | + await got.get(urlJoin(gitlabUrl, gitlabApiPathPrefix, `/projects/${owner}%2F${repo}/repository/tags/${gitTag}`), { |
| 17 | + json: true, |
| 18 | + headers: {'Private-Token': gitlabToken}, |
| 19 | + }); |
| 20 | + debug('The git tag %o already exists, update the release description', gitTag); |
| 21 | + // Update the release notes |
| 22 | + await got.post( |
| 23 | + urlJoin(gitlabUrl, gitlabApiPathPrefix, `/projects/${owner}%2F${repo}/repository/tags/${gitTag}/release`), |
| 24 | + {json: true, headers: {'Private-Token': gitlabToken}, body: {tag_name: gitTag, description: notes}} // eslint-disable-line camelcase |
| 25 | + ); |
| 26 | + } catch (err) { |
| 27 | + // If the error is 404, the tag doesn't exist, otherwise it's an error |
| 28 | + if (err.statusCode !== 404) { |
| 29 | + throw err; |
| 30 | + } |
| 31 | + debug('Create git tag %o with commit %o and release description', gitTag, gitHead); |
| 32 | + await got.post( |
| 33 | + urlJoin(gitlabUrl, gitlabApiPathPrefix, `/projects/${owner}%2F${repo}/repository/tags/${gitTag}/release`), |
| 34 | + { |
| 35 | + json: true, |
| 36 | + headers: {'PRIVATE-TOKEN': gitlabToken}, |
| 37 | + body: {tag_name: gitTag, ref: gitHead, release_description: notes}, // eslint-disable-line camelcase |
| 38 | + } |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + logger.log('Published GitLab release: %s', gitTag); |
| 43 | +}; |
0 commit comments