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: commit conventions #343

Merged
merged 10 commits into from
Sep 12, 2022
13 changes: 11 additions & 2 deletions lib/add-contributor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = addContributor;

const getUserDetails = require("./get-user-details");
const ContentFiles = require("./modules/content-files");
const convertMessage = require("commit-conv");

async function addContributor({
context,
Expand Down Expand Up @@ -52,17 +53,25 @@ async function addContributor({
originalSha: config.getOriginalSha(),
};

const convention = config.get().commitConvention;
const prTitle = convertMessage({
tag: "docs",
msg: `add ${who} as a contributor`,
convention
});

// create or update pull request
const {
pullRequestURL,
pullCreated,
} = await repository.createPullRequestFromFiles({
title: `docs: add ${who} as a contributor for ${contributions.join(", ")}`,
body: `Add @${who} as a contributor for ${contributions.join(
title: prTitle,
body: `Adds @${who} as a contributor for ${contributions.join(
", "
)}.\n\nThis was requested by ${commentReply.replyingToWho()} [in this comment](${commentReply.replyingToWhere()})`,
filesByPath: filesByPathToUpdate,
branchName,
convention,
});

// let user know in comment
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Config {
files: ["README.md"],
imageSize: 100,
commit: false,
commitConvention: "angular",
contributors: [],
contributorsPerLine: 7,
};
Expand All @@ -75,6 +76,9 @@ class Config {
if (!Number.isInteger(options.contributorsPerLine)) {
options.contributorsPerLine = 7;
}
if (!("commitConvention" in options)) {
options.commitConvention = "angular";
}
return options;
}

Expand Down
41 changes: 33 additions & 8 deletions lib/modules/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
BranchNotFoundError,
ResourceNotFoundError,
} = require("./errors");
const convertMessage = require("commit-conv");

class Repository {
/**
Expand Down Expand Up @@ -117,48 +118,64 @@ class Repository {
});
}

async updateFile({ filePath, content, branchName, originalSha }) {
async updateFile({ filePath, content, branchName, originalSha, convention }) {
const contentBinary = Buffer.from(content).toString("base64");

//octokit.github.io/rest.js/#api-Repos-updateFile
await this.octokit.repos.createOrUpdateFileContents({
owner: this.owner,
repo: this.repo,
path: filePath,
message: `docs: update ${filePath} ${this.skipCiString}`.trim(),
message: convertMessage({
tag: "docs",
msg: `update ${filePath} ${this.skipCiString}`,
convention,
}).trim(),
content: contentBinary,
sha: originalSha,
branch: branchName,
});
}

async createFile({ filePath, content, branchName }) {
async createFile({ filePath, content, branchName, convention }) {
const contentBinary = Buffer.from(content).toString("base64");

//octokit.github.io/rest.js/#api-Repos-createFile
await this.octokit.repos.createOrUpdateFileContents({
owner: this.owner,
repo: this.repo,
path: filePath,
message: `docs: create ${filePath} ${this.skipCiString}`.trim(),
message: convertMessage({
tag: "docs",
msg: `create ${filePath} ${this.skipCiString}`,
convention,
}).trim(),
content: contentBinary,
branch: branchName,
});
}

async createOrUpdateFile({ filePath, content, branchName, originalSha }) {
async createOrUpdateFile({
filePath,
content,
branchName,
originalSha,
convention,
}) {
if (originalSha === undefined) {
await this.createFile({ filePath, content, branchName });
await this.createFile({ filePath, content, branchName, convention });
} else {
await this.updateFile({
filePath,
content,
branchName,
originalSha,
convention,
});
}
}

async createOrUpdateFiles({ filesByPath, branchName }) {
async createOrUpdateFiles({ filesByPath, branchName, convention }) {
const repository = this;
const createOrUpdateFilesMultiple = Object.entries(filesByPath).map(
([filePath, { content, originalSha }]) => {
Expand All @@ -167,6 +184,7 @@ class Repository {
content,
branchName,
originalSha,
convention,
});
}
);
Expand Down Expand Up @@ -217,7 +235,13 @@ class Repository {
}
}

async createPullRequestFromFiles({ title, body, filesByPath, branchName }) {
async createPullRequestFromFiles({
title,
body,
filesByPath,
branchName,
convention,
}) {
const branchNameExists = branchName === this.baseBranch;
if (!branchNameExists) {
await this.createBranch(branchName);
Expand All @@ -226,6 +250,7 @@ class Repository {
await this.createOrUpdateFiles({
filesByPath,
branchName,
convention,
});

return this.createPullRequest({
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"dependencies": {
"all-contributors-cli": "^6.20.4",
"commit-conv": "^1.0.3",
"compromise": "^11.14.3",
"probot": "^12.1.1"
},
Expand Down
Loading