Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added a JSON file output upon completing the changeset #171

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: add way to enable/disable output file and change directory of o…
…utput file
  • Loading branch information
Mees van Dongen committed Mar 7, 2024
commit 4141e3cc83d577402e11e9b75e46e57f940b5fed
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@ GitLab CI cli for [changesets](https://github.com/atlassian/changesets) like its
- `INPUT_PUBLISHED` - Command executed after published
- `INPUT_ONLY_CHANGESETS` - Command executed on only changesets detected
- `INPUT_REMOVE_SOURCE_BRANCH` - Enables the merge request "Delete source branch" checkbox. Default false.
- `INPUT_TARGET_BRANCH` -> The merge request target branch. Defaults to current branch
- `INPUT_TARGET_BRANCH` - The merge request target branch. Defaults to current branch
- `INPUT_CREATE_GITLAB_RELEASES` - A boolean value to indicate whether to create Gitlab releases after publish or not. Default true.
- `INPUT_CREATE_OUTPUT_FILE` - A boolean value to indicate whether to enable the output file or not. Default false.
- `INPUT_OUTPUT_FILE_DIRECTORY` - A string value to indiciate where the `changesets-gitlab.output.json` file will be placed. Defaults to the HOME directory.

### Output

When publishing, a list of published packages is written to a file named `changesets-gitlab.output.json`.
When publishing, a list of published packages is written to a file named `changesets-gitlab.output.json`, located in the output directory as configured above. The file will look like this:

```json
[
{ "name": "@xx/xx", "version": "1.2.0" },
{ "name": "@xx/xy", "version": "0.8.9" }
]
{
"published": true,
"publishedPackages": [
{ "name": "@xx/xx", "version": "1.2.0" },
{ "name": "@xx/xy", "version": "0.8.9" }
]
}
```

The existence of this file implies that a publish has happened. This file should be added to `.gitignore`.

### Environment Variables

```sh
Expand Down
12 changes: 9 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import { setupUser } from './gitUtils.js'
import readChangesetState from './readChangesetState.js'
import { runPublish, runVersion } from './run.js'
import type { MainCommandOptions } from './types.js'
import { execSync, getOptionalInput, getUsername } from './utils.js'
import {
execSync,
getOptionalInput,
getUsername,
writePublishResultIfNeeded,
} from './utils.js'

import { createApi } from './index.js'

Expand All @@ -26,6 +31,8 @@ export const main = async ({
DEBUG_GITLAB_CREDENTIAL = 'false',
} = env

writePublishResultIfNeeded({ published: false })

if (CI) {
console.log('setting git user')
await setupUser()
Expand Down Expand Up @@ -88,8 +95,7 @@ export const main = async ({
})

if (result.published) {
const filePath = `${HOME}/published-packages.json`
fs.writeJsonSync(filePath, result.publishedPackages)
writePublishResultIfNeeded(result)

if (published) {
execSync(published)
Expand Down
2 changes: 1 addition & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ interface PublishedPackage {
version: string
}

type PublishResult =
export type PublishResult =
| {
published: false
}
Expand Down
15 changes: 14 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { execSync as _execSync } from 'node:child_process'
import fs from 'node:fs'
import path from 'node:path'

import { getInput } from '@actions/core'
import { exec } from '@actions/exec'
import type { Gitlab } from '@gitbeaker/core'
import type { Package } from '@manypkg/get-packages'
import { getPackages } from '@manypkg/get-packages'
import fs from 'fs-extra'
import { toString as mdastToString } from 'mdast-util-to-string'
import remarkParse from 'remark-parse'
import remarkStringify from 'remark-stringify'
import { unified } from 'unified'

import { env } from './env.js'
import type { PublishResult } from './run.js'

export const BumpLevels = {
dep: 0,
Expand Down Expand Up @@ -162,3 +163,15 @@ export const getUsername = (api: Gitlab) => {
api.Users.showCurrentUser().then(currentUser => currentUser.username)
)
}

export function writePublishResultIfNeeded(publishResult: PublishResult) {
if (getInput('create_output_file') !== 'true') {
return
}

const outputFileDirectory =
getOptionalInput('output_file_directory') ?? env.HOME

const filePath = `${outputFileDirectory}/changesets-gitlab.output.json`
fs.writeJsonSync(filePath, publishResult)
}