Skip to content
New issue

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

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

Already on GitHub? # to your account

Set author and co-authors using git mob core #106

Merged
merged 27 commits into from
Oct 22, 2023
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4775bda
Set author using git mob core
rkotze Apr 30, 2023
f9a0dc0
Throw error if author key not found
rkotze Apr 30, 2023
745650c
Throw error if author not found
rkotze May 1, 2023
2198475
Read git mob specific config
rkotze May 1, 2023
b41a6d6
WIP: Only need to find & save missing authors
rkotze May 1, 2023
e62389f
Compose authors changed to save missing authors
rkotze May 14, 2023
b78438b
Fix lint - never used variable
rkotze May 15, 2023
f7c94fc
Get git mob and git configs for local template
rkotze May 28, 2023
d250b6a
Core can detect if in git repo
rkotze May 28, 2023
36ce05b
Fix test bad import of rev-parse
rkotze May 28, 2023
2521c47
Get selected coauthors from mob config in core
rkotze May 29, 2023
3875993
Add home page paths for packages
rkotze May 29, 2023
be83c3d
If name empty use login name
rkotze May 29, 2023
7471961
Replace old exec with new one.
rkotze Jul 1, 2023
7a052ef
Merge remote-tracking branch 'origin/master' into integrate-core-git-mob
rkotze Jul 1, 2023
22f46e6
Npm link not needed in npm workspace env
rkotze Jul 1, 2023
18d12af
Use latest git mob core version
rkotze Jul 1, 2023
535953b
Reduce CLI calls to print co-authors
rkotze Jul 1, 2023
3016314
Remove the usage of gitMessage
rkotze Jul 1, 2023
de85eb4
Use new exec getConfig over old silentRun
rkotze Jul 1, 2023
63c3af8
No longer needed author print function
rkotze Oct 21, 2023
03d0045
:pencil: Update documentation
rkotze Oct 21, 2023
7a1b0cf
Duplicate function all not needed
rkotze Oct 21, 2023
92b10d9
Convert check author to TS
rkotze Oct 21, 2023
7e0e01a
Primary author prefer undefined over null
rkotze Oct 21, 2023
12f5934
Convert git mob to TS
rkotze Oct 21, 2023
1b33506
Convert src git mob to TS
rkotze Oct 21, 2023
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
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -39,9 +39,6 @@ jobs:
- name: Build package
run: npm run build

- name: Install Git-Mob (link)
run: npm link

- name: Run lint and tests
run: npm run checks

29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,35 @@

Follows [Semantic Versioning](https://semver.org/).

## git-mob-core 0.6.0 current

### Added

```ts
gitMobConfig = {
localTemplate(): <Promise<boolean>>,
fetchFromGitHub(): <Promise<boolean>>,
};

gitConfig = {
getLocalCommitTemplate(): <Promise<string>>,
getGlobalCommitTemplate(): <Promise<string>>,
};

gitRevParse = {
insideWorkTree(): string,
topLevelDirectory(): boolean,
};
```

## git-mob 2.5.0 current

### Added

- Integrated git-mob-core for main `git mob` features
- Reduced the calls to `git` CLI to speed up command execution for `git mob`
- Convert src git-mob and spec files from JS to TS

## git-mob-core 0.5.0 10-06-2023

### Added
26 changes: 20 additions & 6 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/bob/git-mob.config.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ const glob = require('glob');

const baseConfig = {
entryPoints: [
'./src/git-mob.js',
'./src/git-mob.ts',
'./src/solo.js',
'./src/git-add-coauthor.ts',
'./src/git-delete-coauthor.js',
20 changes: 18 additions & 2 deletions packages/git-mob-core/README.md
Original file line number Diff line number Diff line change
@@ -15,22 +15,38 @@ npm i git-mob-core
```TS
saveNewCoAuthors(authors): <Promise<Author[]>>
getAllAuthors(): <Promise<Author[]>>
getPrimaryAuthor(): Author | null
getPrimaryAuthor(): Author | undefined
getSelectedCoAuthors(allAuthors): Author[]
setCoAuthors(keys): <Promise<Author[]>>
setPrimaryAuthor(author): void
solo(): void
solo(): <Promise<void>>
updateGitTemplate(selectedAuthors): void
fetchGitHubAuthors(userNames: string[], userAgent: string): <Promise<Author[]>>
pathToCoAuthors(): string
getConfig(prop: string): string | undefined
updateConfig(prop: string, value: string): void
gitMobConfig = {
localTemplate(): <Promise<boolean>>,
fetchFromGitHub(): <Promise<boolean>>,
};

gitConfig = {
getLocalCommitTemplate(): <Promise<string>>,
getGlobalCommitTemplate(): <Promise<string>>,
};

gitRevParse = {
insideWorkTree(): string,
topLevelDirectory(): boolean,
};
class Author
```

## Author class

```TS
class Author;

// Properties
Author.key: string
Author.name: string
1 change: 1 addition & 0 deletions packages/git-mob-core/package.json
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
"name": "git-mob-core",
"version": "0.5.0",
"description": "Core Git Mob library to manage co-authoring",
"homepage": "https://github.com/rkotze/git-mob/blob/master/packages/git-mob-core/README.md",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
32 changes: 2 additions & 30 deletions packages/git-mob-core/src/commands.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
const { exec } = require('child_process');
const { promisify } = require('util');
const { silentRun } = require('./silent-run');
const { getConfig } = require('./config-manager');

/**
* Runs the given command in a shell.
* @param {string} command The command to execute
* @returns {Promise} stdout string
*/
async function silentExec(command) {
const execAsync = promisify(exec);
try {
const cmdConfig = {};
const processCwd = getConfig('processCwd');
if (processCwd) cmdConfig.cwd = processCwd;
const response = await execAsync(command, cmdOptions(cmdConfig));

return response.stdout;
} catch (error) {
return `GitMob silentExec: "${command}" ${error.message}`;
}
}
const { execCommand } = require('./git-mob-api/exec-command');

function handleResponse(query) {
try {
@@ -77,20 +56,13 @@ function gitAddCoAuthor(coAuthor) {
}

async function getRepoAuthors() {
return silentExec(`git shortlog -sen HEAD`);
return execCommand('git shortlog -sen HEAD');
}

function removeGitMobSection() {
return silentRun(`git config --global --remove-section git-mob`);
}

function cmdOptions(extendOptions = {}) {
return {
...extendOptions,
encoding: 'utf8',
};
}

module.exports = {
config: {
getAll,
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class AuthorNotFound extends Error {
constructor(initials: string) {
super(`Author with initials "${initials}" not found!`);
}
}
39 changes: 39 additions & 0 deletions packages/git-mob-core/src/git-mob-api/exec-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { exec } from 'node:child_process';
import { promisify } from 'node:util';
import { getConfig as cmGetConfig } from '../config-manager';

type ExecCommandOptions = {
encoding: string;
cwd?: string;
};

// Runs the given command in a shell.
export async function execCommand(command: string): Promise<string> {
const cmdConfig: ExecCommandOptions = { encoding: 'utf8' };
const processCwd = cmGetConfig('processCwd');
if (processCwd) cmdConfig.cwd = processCwd;
const execAsync = promisify(exec);
const { stderr, stdout } = await execAsync(command, cmdConfig);

if (stderr) {
throw new Error(`GitMob execCommand: "${command}" ${stderr.trim()}`);
}

return stdout.trim();
}

export async function getConfig(key: string) {
try {
return await execCommand(`git config --get ${key}`);
} catch {
return undefined;
}
}

export async function getAllConfig(key: string) {
try {
return await execCommand(`git config --get-all ${key}`);
} catch {
return undefined;
}
}
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ const ghRkotzeResponse = {
id: 123,
login: 'rkotze',
name: 'Richard Kotze',
stars: 2,
};

const ghDidelerResponse = {
@@ -83,6 +84,26 @@ test('Query for two GitHub users and build AuthorList', async () => {
]);
});

test('Handle GitHub user with no name', async () => {
mockedFetch.mockResolvedValue(
buildBasicResponse({
id: 329,
name: null,
login: 'kotze',
})
);

const actualAuthorList = await fetchGitHubAuthors(['kotze'], agentHeader);

expect(actualAuthorList).toEqual([
{
key: 'kotze',
name: 'kotze',
email: '329+kotze@users.noreply.github.com',
},
]);
});

test('Error if no user agent specified', async () => {
await expect(fetchGitHubAuthors(['badrequestuser'], '')).rejects.toThrow(
/Error no user-agent header string given./
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ const getHeaders: RequestOptions = {
type GitHubUser = {
id: number;
login: string;
name: string;
name?: string;
};

function validateGhUser(o: any): o is GitHubUser {
@@ -49,7 +49,7 @@ async function fetchGitHubAuthors(
if (validateGhUser(ghUser.data)) {
const { login, id, name } = ghUser.data;
authorAuthorList.push(
new Author(login, name, `${id}+${login}@users.noreply.github.com`)
new Author(login, name || login, `${id}+${login}@users.noreply.github.com`)
);
}
}
2 changes: 1 addition & 1 deletion packages/git-mob-core/src/git-mob-api/git-authors/index.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ const os = require('os');
const path = require('path');
const { promisify } = require('util');
const { Author } = require('../author');
const { topLevelDirectory } = require('../../git-rev-parse');
const { topLevelDirectory } = require('../git-rev-parse');

function gitAuthors(readFilePromise, writeFilePromise, overwriteFilePromise) {
async function readFile(path) {
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path');
const fs = require('fs');
const { Author } = require('../author');
const { topLevelDirectory } = require('../../git-rev-parse');
const { topLevelDirectory } = require('../git-rev-parse');
const { gitAuthors, pathToCoAuthors } = require('.');

const validJsonString = `
9 changes: 9 additions & 0 deletions packages/git-mob-core/src/git-mob-api/git-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getConfig } from './exec-command';

export async function getLocalCommitTemplate() {
return getConfig('--local commit.template');
}

export async function getGlobalCommitTemplate() {
return getConfig('--global commit.template');
}
15 changes: 15 additions & 0 deletions packages/git-mob-core/src/git-mob-api/git-mob-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getConfig, getAllConfig } from './exec-command';

export async function localTemplate() {
const localTemplate = await getConfig('--local git-mob-config.use-local-template');
return localTemplate === 'true';
}

export async function fetchFromGitHub() {
const githubFetch = await getConfig('--global git-mob-config.github-fetch');
return githubFetch === 'true';
}

export async function getSetCoAuthors() {
return getAllConfig('--global git-mob.co-author');
}
Loading