Skip to content

Commit

Permalink
Changelog packages (#170)
Browse files Browse the repository at this point in the history
* Start of stuff

* Add a changeset

* Add types as a dep to changelog-git and replace custom getChangelogEntry file with changelog-github

* Add another changeset
  • Loading branch information
emmatown committed Oct 4, 2019
1 parent 94de7c1 commit bb855a8
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 57 deletions.
5 changes: 4 additions & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"changelog": "./getChangelogEntry.js",
"changelog": [
"@changesets/changelog-github",
{ "repo": "atlassian/changesets" }
],
"commit": false,
"access": "public"
}
5 changes: 5 additions & 0 deletions .changeset/fluffy-actors-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/get-github-info": patch
---

Convert internals to TypeScript
44 changes: 0 additions & 44 deletions .changeset/getChangelogEntry.js

This file was deleted.

5 changes: 5 additions & 0 deletions .changeset/plenty-nails-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/get-github-info": minor
---

Add backticks around commits to be more similar to how commits are shown in GitHub comments
6 changes: 6 additions & 0 deletions .changeset/sixty-beers-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@changesets/changelog-git": minor
"@changesets/changelog-github": minor
---

Create package
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"codecov": "^3.5.0",
"dataloader": "^1.4.0",
"detect-indent": "^6.0.0",
"dotenv": "^8.0.0",
"dotenv": "^8.1.0",
"enquirer": "^2.3.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.2.0",
Expand Down
12 changes: 12 additions & 0 deletions packages/changelog-git/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@changesets/changelog-git",
"version": "0.0.0",
"description": "A changelog entry generator for git that writes hashes",
"main": "dist/changelog-git.cjs.js",
"module": "dist/changelog-git.esm.js",
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/master/packages/changelog-git",
"dependencies": {
"@changesets/types": "^0.3.0"
}
}
45 changes: 45 additions & 0 deletions packages/changelog-git/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
NewChangesetWithCommit,
VersionType,
ChangelogFunctions,
ModCompWithWorkspace
} from "@changesets/types";

const getReleaseLine = async (
changeset: NewChangesetWithCommit,
_type: VersionType
) => {
const [firstLine, ...futureLines] = changeset.summary
.split("\n")
.map(l => l.trimRight());

let returnVal = `- ${
changeset.commit ? `${changeset.commit}: ` : ""
}${firstLine}\n${futureLines.map(l => ` ${l}`).join("\n")}`;

return returnVal;
};

const getDependencyReleaseLine = async (
changesets: NewChangesetWithCommit[],
dependenciesUpdated: ModCompWithWorkspace[]
) => {
if (dependenciesUpdated.length === 0) return "";

const changesetLinks = changesets.map(
changeset => `- Updated dependencies [${changeset.commit}]`
);

const updatedDepenenciesList = dependenciesUpdated.map(
dependency => ` - ${dependency.name}@${dependency.newVersion}`
);

return [...changesetLinks, ...updatedDepenenciesList].join("\n");
};

const defaultChangelogFunctions: ChangelogFunctions = {
getReleaseLine,
getDependencyReleaseLine
};

export default defaultChangelogFunctions;
14 changes: 14 additions & 0 deletions packages/changelog-github/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@changesets/changelog-github",
"version": "0.0.0",
"description": "A changelog entry generator for GitHub that links to commits, PRs and users",
"main": "dist/changelog-github.cjs.js",
"module": "dist/changelog-github.esm.js",
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/master/packages/changelog-github",
"dependencies": {
"@changesets/get-github-info": "^0.2.1",
"@changesets/types": "^0.3.0",
"dotenv": "^8.1.0"
}
}
67 changes: 67 additions & 0 deletions packages/changelog-github/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { ChangelogFunctions } from "@changesets/types";
// @ts-ignore
import { config } from "dotenv";
import { getInfo } from "@changesets/get-github-info";

config();

const changelogFunctions: ChangelogFunctions = {
getDependencyReleaseLine: async (
changesets,
dependenciesUpdated,
options
) => {
if (!options.repo) {
throw new Error(
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]'
);
}
if (dependenciesUpdated.length === 0) return "";

const changesetLink = `- Updated dependencies [${(await Promise.all(
changesets.map(async cs => {
if (cs.commit) {
let { links } = await getInfo({
repo: options.repo,
commit: cs.commit
});
return links.commit;
}
})
))
.filter(_ => _)
.join(", ")}]:`;

const updatedDepenenciesList = dependenciesUpdated.map(
dependency => ` - ${dependency.name}@${dependency.newVersion}`
);

return [changesetLink, ...updatedDepenenciesList].join("\n");
},
getReleaseLine: async (changeset, type, options) => {
if (!options.repo) {
throw new Error(
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]'
);
}
const [firstLine, ...futureLines] = changeset.summary
.split("\n")
.map(l => l.trimRight());

if (changeset.commit) {
let { links } = await getInfo({
repo: options.repo,
commit: changeset.commit
});
return `\n\n- ${links.commit}${
links.pull === null ? "" : ` ${links.pull}`
}${
links.user === null ? "" : ` Thanks ${links.user}!`
} - ${firstLine}\n${futureLines.map(l => ` ${l}`).join("\n")}`;
} else {
return `\n\n- ${firstLine}\n${futureLines.map(l => ` ${l}`).join("\n")}`;
}
}
};

export default changelogFunctions;
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @ts-ignore
import fetch from "node-fetch";
import DataLoader from "dataloader";

function makeQuery(repos) {
function makeQuery(repos: any) {
return `
query {
${Object.keys(repos)
Expand All @@ -13,7 +14,9 @@ function makeQuery(repos) {
) {
${repos[repo]
.map(
commit => `a${commit}: object(expression: ${JSON.stringify(
(
commit: string
) => `a${commit}: object(expression: ${JSON.stringify(
commit
)}) {
... on Commit {
Expand Down Expand Up @@ -49,13 +52,13 @@ function makeQuery(repos) {
// 2. batching
// getReleaseLine will be called a large number of times but it'll be called at the same time
// so instead of doing a bunch of network requests, we can do a single one.
const GHDataLoader = new DataLoader(async requests => {
const GHDataLoader = new DataLoader(async (requests: any[]) => {
if (!process.env.GITHUB_TOKEN) {
throw new Error(
"Please create a GitHub personal access token at https://github.com/settings/tokens/new and add it as the GITHUB_TOKEN environment variable"
);
}
let repos = {};
let repos: Record<any, any> = {};
requests.forEach(({ commit, repo }) => {
if (repos[repo] === undefined) {
repos[repo] = [];
Expand All @@ -69,7 +72,7 @@ const GHDataLoader = new DataLoader(async requests => {
method: "POST",
body: JSON.stringify({ query: makeQuery(repos) })
}
).then(x => x.json());
).then((x: any) => x.json());

// this is mainly for the case where there's an authentication problem
if (!data.data) {
Expand All @@ -80,7 +83,7 @@ const GHDataLoader = new DataLoader(async requests => {
);
}

let cleanedData = {};
let cleanedData: Record<any, any> = {};
let dataKeys = Object.keys(data.data);
Object.keys(repos).forEach((repo, index) => {
cleanedData[repo] = {};
Expand All @@ -93,7 +96,18 @@ const GHDataLoader = new DataLoader(async requests => {
return requests.map(({ repo, commit }) => cleanedData[repo][commit]);
});

export async function getInfo(request) {
export async function getInfo(request: {
commit: string;
repo: string;
}): Promise<{
user: string | null;
pull: number | null;
links: {
commit: string;
pull: string | null;
user: string | null;
};
}> {
if (!request.commit) {
throw new Error("Please pass a commit SHA to getInfo");
}
Expand All @@ -114,7 +128,7 @@ export async function getInfo(request) {
? data.associatedPullRequests.nodes[0].number
: null,
links: {
commit: `[${request.commit}](${data.commitUrl})`,
commit: `[\`${request.commit}\`](${data.commitUrl})`,
pull:
data.associatedPullRequests &&
data.associatedPullRequests.nodes &&
Expand Down
7 changes: 4 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1869,9 +1869,10 @@ domexception@^1.0.1:
dependencies:
webidl-conversions "^4.0.2"

dotenv@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.0.0.tgz#ed310c165b4e8a97bb745b0a9d99c31bda566440"
dotenv@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.1.0.tgz#d811e178652bfb8a1e593c6dd704ec7e90d85ea2"
integrity sha512-GUE3gqcDCaMltj2++g6bRQ5rBJWtkWTmqmD0fo1RnnMuUqHNCt2oTPeDnS9n6fKYvlhn7AeBkb38lymBtWBQdA==

ecc-jsbn@~0.1.1:
version "0.1.2"
Expand Down

0 comments on commit bb855a8

Please sign in to comment.