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

Changesets Action Release Functionality #672

Closed
wants to merge 113 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
945079e
feat: commit raw files
samchungy Nov 16, 2021
72d515e
Squashed commit of the following:
samchungy Nov 16, 2021
a9e35fd
merge branches
samchungy Nov 16, 2021
c9e479d
chore: add comment
samchungy Nov 16, 2021
54e0823
refactor: tidy up gitUtils
samchungy Nov 16, 2021
50a7cac
Merge branch 'changeset' of github.com:seek-oss/skuba into changeset
samchungy Nov 16, 2021
3fb28e5
refactor: remove spread
samchungy Nov 16, 2021
6b0928b
fix: correct index file
samchungy Nov 16, 2021
129937b
Merge branch 'changeset' of github.com:seek-oss/skuba into changeset
samchungy Nov 16, 2021
8f3a0d2
refactor: rename init
samchungy Nov 16, 2021
6075f1d
fix: correct exec import
samchungy Nov 16, 2021
8cbb3ba
fix: correct const export
samchungy Nov 17, 2021
88cf095
feat: downgrade to non esm package
samchungy Nov 17, 2021
aba5566
downgrade more packages, setup types
samchungy Nov 17, 2021
bb8adf1
fix: correct auto import
samchungy Nov 17, 2021
25cd190
feat: get branch name from environment
samchungy Nov 17, 2021
0d25c42
feat: remove netrc
samchungy Nov 17, 2021
382aa4d
create exec function in utils
samchungy Nov 17, 2021
54b2f3f
add pushWithToken
samchungy Nov 17, 2021
64e59ad
Merge with master
samchungy Nov 17, 2021
3f19fcd
substitute new git push function
samchungy Nov 17, 2021
2925bb9
enhance git operations
samchungy Nov 17, 2021
9913e2a
add git branch functionality
samchungy Nov 18, 2021
3bfb4f7
use github token app as user
samchungy Nov 18, 2021
86aba33
add git push tags
samchungy Nov 18, 2021
88045f5
hardcode user for now
samchungy Nov 18, 2021
d7a0f9c
use Promise all
samchungy Nov 18, 2021
a3b19d3
implement git status porcelain
samchungy Nov 18, 2021
b68cca0
use git commit and git add
samchungy Nov 18, 2021
abbb23e
reuse cli function
samchungy Nov 18, 2021
019c46e
use getChangedFiles
samchungy Nov 18, 2021
e053a9c
relock yarn
samchungy Nov 18, 2021
a0acdd3
add reset hard
samchungy Nov 19, 2021
e8cf2a6
create git reset
samchungy Nov 19, 2021
9187657
refactor: rename const
samchungy Nov 19, 2021
e5eb3b8
handle files being removed
samchungy Nov 19, 2021
d446769
fix pushing tags
samchungy Nov 20, 2021
6275a08
add catch error
samchungy Nov 20, 2021
cea1907
filter out old tags
samchungy Nov 20, 2021
346379e
move tag retrieving logic
samchungy Nov 20, 2021
522e067
add git reset int test
samchungy Nov 22, 2021
92ec9b8
Merge branch 'master' of github.com:seek-oss/skuba into changeset
samchungy Nov 22, 2021
6918472
refactor move dir to variable
samchungy Nov 22, 2021
4a4dccf
add vol reset
samchungy Nov 22, 2021
0d12afa
refactor: use dir variable
samchungy Nov 22, 2021
b907e5e
add getChangedFiles tests
samchungy Nov 22, 2021
6e7bee5
change set user logic
samchungy Nov 22, 2021
84967e0
move gitCommitAll function
samchungy Nov 22, 2021
b9e6171
refactor: rename function
samchungy Nov 22, 2021
37e1ffd
fix: correct filepath
samchungy Nov 22, 2021
580f5e6
Merge branch 'master' of github.com:seek-oss/skuba into changeset
samchungy Nov 24, 2021
0534957
refactor: restore original octokit logic
samchungy Nov 24, 2021
75af6d4
use new Git api
samchungy Nov 24, 2021
240ac3e
add test coverage for gitUtils
samchungy Nov 24, 2021
8589dc9
use try catch
samchungy Nov 24, 2021
1f9c117
remove renamed file
samchungy Nov 24, 2021
8a50146
delete redundant file
samchungy Nov 24, 2021
2be7d1e
Merge branch 'changeset' of github.com:seek-oss/skuba into changeset
samchungy Nov 25, 2021
b43c43d
copy unit and comonent tests
samchungy Nov 25, 2021
5a6c14a
Merge branch 'master' into changeset
samchungy Nov 25, 2021
2dff173
add source
samchungy Nov 25, 2021
fdf6d67
add some basic docs
samchungy Nov 25, 2021
b23975c
fix description
samchungy Nov 27, 2021
ec31429
Remove commented out code
samchungy Nov 29, 2021
1438dfe
Merge branch 'master' into changeset
samchungy Nov 29, 2021
bb2cedb
try and fetch app
samchungy Dec 1, 2021
b9aea90
Merge branch 'master' of github.com:seek-oss/skuba into changeset
samchungy Dec 1, 2021
bda3f3a
feat: get bot user details from app slug
samchungy Dec 27, 2021
51c8f64
Merge branch 'master' of github.com:seek-oss/skuba into changeset
samchungy Dec 27, 2021
55d0374
refactor: remove useless return
samchungy Dec 27, 2021
ff4778b
Merge branch 'master' into changeset
samchungy Dec 29, 2021
b9fd823
Merge branch 'master' of github.com:seek-oss/skuba into changeset
samchungy Mar 26, 2022
693f353
fix: remove app slug logic
samchungy Mar 26, 2022
7874804
refactor: remove mocked util
samchungy Mar 26, 2022
aff8425
Update release.md
samchungy Mar 26, 2022
8857f60
Run `skuba format`
seek-oss-ci Mar 26, 2022
1b0abf7
feat: update changeset packages
samchungy Mar 26, 2022
f40cec1
Merge branch 'changeset' of github.com:seek-oss/skuba into changeset
samchungy Mar 26, 2022
bbe2edc
feat: remove inputs
samchungy Mar 26, 2022
66dc014
refactor: remove setOutput
samchungy Mar 26, 2022
35f3f6b
fix: adjust test cases
samchungy Mar 26, 2022
5a4b99a
Run `skuba format`
seek-oss-ci Mar 26, 2022
efa3f78
refactor: remove unused function
samchungy Mar 26, 2022
d22df05
refactor: move git logic to separate file
samchungy Mar 27, 2022
0651c12
refactor: move mock
samchungy Mar 27, 2022
b6d66c0
refactor: remove package version check
samchungy Mar 27, 2022
63f53d9
feat: add documentation
samchungy Mar 27, 2022
596c4c2
refactor: simplify github token logic
samchungy Mar 27, 2022
6b64c6f
Create fast-items-build.md
samchungy Mar 27, 2022
c63248c
Run `skuba format`
seek-oss-ci Mar 27, 2022
7acae25
refactor: rename to `changesets`
samchungy Mar 27, 2022
94cdd3f
Merge branch 'changeset' of github.com:seek-oss/skuba into changeset
samchungy Mar 27, 2022
8c86a14
refactor: rename integration test to `changesets`
samchungy Mar 27, 2022
701a0ef
chore: nitpick grammar
samchungy Mar 27, 2022
643a004
Update changesets.md
samchungy Mar 28, 2022
7362a56
Merge branch 'master' of github.com:seek-oss/skuba into changeset
samchungy Mar 29, 2022
d185687
Tweak changeset
72636c Mar 29, 2022
1053df1
feat: add changesets to command list
samchungy Mar 29, 2022
23f4d80
fix: use relative import
samchungy Mar 29, 2022
88906b3
fix: correct export name
samchungy Mar 29, 2022
787fa2a
Merge branch 'master' into changeset
samchungy Mar 29, 2022
d4230ed
Merge branch 'master' of github.com:seek-oss/skuba into changeset
samchungy Mar 29, 2022
7c5357d
Merge branch 'master' into changeset
72636c Mar 30, 2022
b6fabab
feat: ignore changeset release branches
samchungy Mar 30, 2022
6a2f87b
chore: update log message
samchungy Mar 30, 2022
34d4c3d
big time refactor
samchungy Apr 2, 2022
82c45dd
Run `skuba format`
seek-oss-ci Apr 2, 2022
35112da
refactor: more renaming and moving
samchungy Apr 2, 2022
86b3bac
refactor: move to folders
samchungy Apr 2, 2022
74be853
refactor: use cleaner exec syntax
samchungy Apr 2, 2022
0445570
Merge branch 'master' into changeset
72636c Apr 6, 2022
d7ab5e1
Merge branch 'master' into changeset
72636c Apr 6, 2022
76de7a2
add git tests
samchungy Apr 10, 2022
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
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"url": "https://github.com/seek-oss/skuba.git"
},
"devDependencies": {
"@changesets/cli": "2.18.0",
"@changesets/get-github-info": "0.5.0",
"@changesets/types": "4.0.1",
"@types/concurrently": "6.3.0",
"@types/ejs": "3.1.0",
"@types/express": "4.17.13",
Expand All @@ -59,6 +59,10 @@
"type-fest": "2.5.2"
},
"dependencies": {
"@changesets/cli": "^2.18.0",
"@changesets/pre": "^1.0.7",
"@changesets/read": "^0.5.1",
"@manypkg/get-packages": "^1.1.3",
"@octokit/rest": "^18.12.0",
"@octokit/types": "^6.34.0",
"@types/jest": "^27.0.1",
Expand All @@ -79,13 +83,16 @@
"jest": "^27.1.0",
"latest-version": "^5.1.0",
"lodash.mergewith": "^4.6.2",
"mdast-util-to-string": "^3.1.0",
"module-alias": "^2.2.2",
samchungy marked this conversation as resolved.
Show resolved Hide resolved
"normalize-package-data": "^3.0.0",
"npm-run-path": "^4.0.1",
"npm-which": "^3.0.1",
"picomatch": "^2.2.2",
"prettier": "~2.4.1",
"read-pkg-up": "^7.0.1",
"remark-parse": "^10.0.0",
"remark-stringify": "^10.0.1",
"runtypes": "^6.0.0",
"semantic-release": "^17.4.7",
"serialize-error": "^8.0.1",
Expand All @@ -95,7 +102,8 @@
"ts-node": "^9.1.1",
"ts-node-dev": "^1.1.8",
"tsconfig-seek": "1.0.2",
"typescript": "~4.4.4"
"typescript": "~4.4.4",
"unified": "^10.1.0"
},
"peerDependencies": {
"skuba-dive": "1"
Expand Down
17 changes: 17 additions & 0 deletions src/cli/changeset/coreAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const inputs: Record<string, string | undefined> = {
publish: 'yarn changeset publish',
version: undefined,
commit: undefined,
title: undefined,
};

export const getInput = (name: string): string | undefined => inputs[name];

// Maybe we could store these in a checkrun..?
export const setFailed = (message: string) => {
// eslint-disable-next-line no-console
console.error(message);
throw new Error();
};

export const setOutput = (_key: string, _value: string) => {};
68 changes: 68 additions & 0 deletions src/cli/changeset/gitUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Adapted from https://github.com/changesets/action/blob/21240c3cd1d2efa2672d64e0235a03cf139b83e6/src/utils.ts

import { exec } from 'utils/exec';
samchungy marked this conversation as resolved.
Show resolved Hide resolved

import { execWithOutput } from './utils';

// TODO probably try and make this configurable
const getBotName = () => 'buildkite';

export const setupUser = async () => {
await exec('git', 'config', '--global', 'user.name', getBotName());
samchungy marked this conversation as resolved.
Show resolved Hide resolved
await exec(
'git',
'config',
'--global',
'user.email',
`${getBotName()}@users.noreply.github.com`,
);
};

export const pullBranch = async (branch: string) => {
await exec('git', 'pull', 'origin', branch);
};

export const push = async (
branch: string,
{ force }: { force?: boolean } = {},
) => {
await exec(
'git',
...['push', 'origin', `HEAD:${branch}`, force && '--force'].filter<string>(
Boolean as any,
),
);
};

export const pushTags = async () => {
await exec('git', 'push', 'origin', '--tags');
};

export const switchToMaybeExistingBranch = async (branch: string) => {
const { stderr } = await execWithOutput('git', ['checkout', branch], {
ignoreReturnCode: true,
});
const isCreatingBranch = !(stderr as Error)
.toString()
.includes(`Switched to a new branch '${branch}'`);
if (isCreatingBranch) {
await exec('git', 'checkout', '-b', branch);
}
};

export const reset = async (
pathSpec: string,
mode: 'hard' | 'soft' | 'mixed' = 'hard',
) => {
await exec('git', 'reset', `--${mode}`, pathSpec);
};

export const commitAll = async (message: string) => {
await exec('git', 'add', '.');
await exec('git', 'commit', '-m', message);
};

export const checkIfClean = async (): Promise<boolean> => {
const { stdout } = await execWithOutput('git', ['status', '--porcelain']);
return !(stdout as Array<unknown>).length;
};
21 changes: 21 additions & 0 deletions src/cli/changeset/githubAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Octokit } from '@octokit/rest';
import { getCurrentBranchRef, getHeadSha, getOwnerRepo } from 'utils/git';

export const getOctokit = (token: string) => new Octokit({ auth: token });

export const context = async (dir: string) => {
const [{ owner, repo }, currentBranchRef, headSha] = await Promise.all([
getOwnerRepo(dir),
getCurrentBranchRef(dir),
getHeadSha(dir),
]);

return {
sha: headSha,
ref: currentBranchRef,
repo: {
repo,
owner,
},
};
};
5 changes: 5 additions & 0 deletions src/cli/changeset/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { init } from 'wrapper/testing/expressRequestListener';

export const release = () => {
init();
};
109 changes: 109 additions & 0 deletions src/cli/changeset/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Adapted from https://github.com/changesets/action/blob/21240c3cd1d2efa2672d64e0235a03cf139b83e6/src/index.ts
/* eslint-disable no-sync */
/* eslint-disable no-console */
import fs from 'fs-extra';

import * as core from './coreAdapter';
import * as gitUtils from './gitUtils';
import readChangesetState from './readChangesetState';
import { runPublish, runVersion } from './run';

const getOptionalInput = (name: string) => core.getInput(name) || undefined;

(async () => {
const githubToken = process.env.GITHUB_TOKEN;

if (!githubToken) {
core.setFailed('Please add the GITHUB_TOKEN to the changesets action');
return;
}

console.log('setting git user');
await gitUtils.setupUser();

console.log('setting GitHub credentials');
samchungy marked this conversation as resolved.
Show resolved Hide resolved
await fs.writeFile(
`${process.env.HOME as string}/.netrc`,
`machine github.com\nlogin github-actions[bot]\npassword ${githubToken}`,
);

const { changesets } = await readChangesetState();

const publishScript = core.getInput('publish');
const hasChangesets = changesets.length !== 0;
const hasPublishScript = Boolean(publishScript);

core.setOutput('published', 'false');
core.setOutput('publishedPackages', '[]');
core.setOutput('hasChangesets', String(hasChangesets));

switch (true) {
case !hasChangesets && !hasPublishScript:
console.log('No changesets found');
return;
case !hasChangesets && hasPublishScript: {
console.log(
'No changesets found, attempting to publish any unpublished packages to npm',
);

const userNpmrcPath = `${process.env.HOME as string}/.npmrc`;
samchungy marked this conversation as resolved.
Show resolved Hide resolved
if (fs.existsSync(userNpmrcPath)) {
console.log('Found existing user .npmrc file');
const userNpmrcContent = await fs.readFile(userNpmrcPath, 'utf8');
const authLine = userNpmrcContent.split('\n').find((line) =>
// check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
/^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line),
);
if (authLine) {
console.log(
'Found existing auth token for the npm registry in the user .npmrc file',
);
} else {
console.log(
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one",
);
fs.appendFileSync(
userNpmrcPath,
`\n//registry.npmjs.org/:_authToken=${
process.env.NPM_TOKEN as string
}\n`,
);
}
} else {
console.log('No user .npmrc file found, creating one');
fs.writeFileSync(
userNpmrcPath,
`//registry.npmjs.org/:_authToken=${
process.env.NPM_TOKEN as string
}\n`,
);
}

const result = await runPublish({
script: publishScript as string,
githubToken,
});

if (result.published) {
core.setOutput('published', 'true');
core.setOutput(
'publishedPackages',
JSON.stringify(result.publishedPackages),
);
}
return;
}
case hasChangesets:
await runVersion({
script: getOptionalInput('version'),
githubToken,
prTitle: getOptionalInput('title'),
commitMessage: getOptionalInput('commit'),
hasPublishScript,
});
return;
}
})().catch((err: unknown) => {
console.error(err);
core.setFailed((err as Error).message);
});
29 changes: 29 additions & 0 deletions src/cli/changeset/readChangesetState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Adapted from https://github.com/changesets/action/blob/21240c3cd1d2efa2672d64e0235a03cf139b83e6/src/readChangesetState.ts

import { readPreState } from '@changesets/pre';
import readChangesets from '@changesets/read';
import type { NewChangeset, PreState } from '@changesets/types';

export type ChangesetState = {
preState: PreState | undefined;
changesets: NewChangeset[];
};

export default async function readChangesetState(
cwd: string = process.cwd(),
): Promise<ChangesetState> {
const preState = await readPreState(cwd);
const isInPreMode = preState !== undefined && preState.mode === 'pre';

let changesets = await readChangesets(cwd);

if (isInPreMode) {
const changesetsToFilter = new Set(preState.changesets);
changesets = changesets.filter((x) => !changesetsToFilter.has(x.id));
}

return {
preState: isInPreMode ? preState : undefined,
changesets,
};
}
Loading