Skip to content

Commit

Permalink
Merge branch 'main' into fix/js-template
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Apr 25, 2022
2 parents 6e3d1e2 + 6a30a6c commit febb627
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/.cache_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.0.5.1
8.0.5.2
1 change: 0 additions & 1 deletion .github/workflows/process-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ jobs:
with:
fetch-depth: 0
token: ${{ secrets.TOKEN_RELEASE_BOT }}
ref: main_test

- name: Setup
id: setup
Expand Down
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

./scripts/ci/husky/pre-commit.js
30 changes: 30 additions & 0 deletions config/generation.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// eslint-disable-next-line import/no-commonjs
module.exports = {
patterns: [
// Ignore the roots and go down the tree by negating hand written files
'clients/**',
'!clients/README.md',
'!clients/**/.openapi-generator-ignore',

// Java
'!clients/algoliasearch-client-java-2/algoliasearch-core/src/com/algolia/exceptions/*',
'!clients/algoliasearch-client-java-2/algoliasearch-core/src/com/algolia/utils/*',
'clients/algoliasearch-client-java-2/algoliasearch-core/com/algolia/utils/echo/EchoResponse*.java',
'!clients/algoliasearch-client-java-2/algoliasearch-core/com/algolia/utils/echo/EchoResponseInterface.java',

// JavaScript
'!clients/algoliasearch-client-javascript/*',
'!clients/algoliasearch-client-javascript/.github/**',
'!clients/algoliasearch-client-javascript/.yarn/**',
'!clients/algoliasearch-client-javascript/scripts/**',
'!clients/algoliasearch-client-javascript/packages/algoliasearch/**',
'!clients/algoliasearch-client-javascript/packages/requester-*/**',
'!clients/algoliasearch-client-javascript/packages/client-common/**',

// PHP
'!clients/algoliasearch-client-php/lib/Configuration/*',
'clients/algoliasearch-client-php/lib/*.php',
'clients/algoliasearch-client-php/lib/Api/*',
'clients/algoliasearch-client-php/lib/Configuration/Configuration.php',
],
};
Binary file modified gradle/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"docker:setup": "yarn docker:clean && yarn docker:build && yarn docker:mount",
"fix:json": "eslint --ext=json . --fix",
"github-actions:lint": "eslint --ext=yml .github/",
"postinstall": "yarn workspace eslint-plugin-automation-custom build",
"postinstall": "husky install && yarn workspace eslint-plugin-automation-custom build",
"playground:browser": "yarn workspace javascript-browser-playground start",
"release": "yarn workspace scripts createReleaseIssue",
"scripts:lint": "eslint --ext=ts scripts/",
Expand Down Expand Up @@ -50,6 +50,7 @@
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-unused-imports": "2.0.0",
"eslint-plugin-yml": "0.14.0",
"husky": "7.0.4",
"json": "11.0.0",
"mustache": "4.2.0",
"prettier": "2.6.2",
Expand Down
24 changes: 24 additions & 0 deletions scripts/ci/husky/__tests__/pre-commit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-disable import/no-commonjs */
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { createMemoizedMicromatchMatcher } = require('../pre-commit');

describe('createMemoizedMicromatchMatcher', () => {
it('matches correctly', () => {
const matcher = createMemoizedMicromatchMatcher([
'clients/**',
'!clients/README.md',
]);

expect(matcher('clients/README.md')).toEqual(false);
expect(matcher('clients/CONTRIBUTING.md')).toEqual(true);
});

it('prioritizes the exact match when two patterns conflict', () => {
const matcher = createMemoizedMicromatchMatcher([
'!lib/Configuration/*',
'lib/Configuration/Configuration.php',
]);

expect(matcher('lib/Configuration/Configuration.php')).toEqual(true);
});
});
81 changes: 81 additions & 0 deletions scripts/ci/husky/pre-commit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node
/* eslint-disable no-console */
/* eslint-disable import/no-commonjs */
/* eslint-disable @typescript-eslint/no-var-requires */
const chalk = require('chalk');
const execa = require('execa');
const micromatch = require('micromatch');

const GENERATED_FILE_PATTERNS =
require('../../../config/generation.config').patterns;

const run = async (command, { cwd } = {}) => {
return (
(await execa.command(command, { shell: 'bash', all: true, cwd })).all ?? ''
);
};

function createMemoizedMicromatchMatcher(patterns = []) {
const exactMatchers = [];
const positiveMatchers = [];
const negativeMatchers = [];

patterns.forEach((pattern) => {
if (pattern.startsWith('!')) {
// Patterns starting with `!` are negated
negativeMatchers.push(micromatch.matcher(pattern.slice(1)));
} else if (!pattern.includes('*')) {
exactMatchers.push(micromatch.matcher(pattern));
} else {
positiveMatchers.push(micromatch.matcher(pattern));
}
});

return function matcher(str) {
if (exactMatchers.some((match) => match(str))) {
return true;
}

// As `some` returns false on empty array, test will always fail if we only
// provide `negativeMatchers`. We fallback to `true` is it's the case.
const hasPositiveMatchers =
Boolean(positiveMatchers.length === 0 && negativeMatchers.length) ||
positiveMatchers.some((match) => match(str));

return hasPositiveMatchers && !negativeMatchers.some((match) => match(str));
};
}

async function preCommit() {
const stagedFiles = (await run('git diff --name-only --cached')).split('\n');
const deletedFiles = new Set(
(await run('git diff --name-only --staged --diff-filter=D')).split('\n')
);
const matcher = createMemoizedMicromatchMatcher(GENERATED_FILE_PATTERNS);

for (const stagedFile of stagedFiles) {
// keep the deleted files staged even if they were generated before.
if (deletedFiles.has(stagedFile)) {
continue;
}

if (!matcher(stagedFile)) {
continue;
}

console.log(
chalk.bgYellow('[INFO]'),
`Generated file found, unstaging: ${stagedFile}`
);

await run(`git restore --staged ${stagedFile}`);
}
}

if (require.main === module && !process.env.CI) {
preCommit();
}

module.exports = {
createMemoizedMicromatchMatcher,
};
4 changes: 4 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"cleanGeneratedBranch": "ts-node ci/codegen/cleanGeneratedBranch.ts",
"createMatrix": "ts-node ci/createMatrix.ts",
"createReleaseIssue": "ts-node release/create-release-issue.ts",
"pre-commit": "./ci/husky/pre-commit.js",
"processRelease": "ts-node release/process-release.ts",
"pushGeneratedCode": "ts-node ci/codegen/pushGeneratedCode.ts",
"setRunVariables": "ts-node ci/setRunVariables.ts",
Expand All @@ -19,9 +20,11 @@
"@types/inquirer": "8.2.1",
"@types/jest": "27.4.1",
"@types/js-yaml": "4.0.5",
"@types/micromatch": "4.0.2",
"@types/mustache": "4.1.2",
"@types/node": "16.11.26",
"@types/semver": "7.3.9",
"chalk": "4.1.2",
"commander": "9.1.0",
"dotenv": "16.0.0",
"eslint": "8.12.0",
Expand All @@ -31,6 +34,7 @@
"inquirer": "8.2.2",
"jest": "27.5.1",
"js-yaml": "4.1.0",
"micromatch": "4.0.5",
"mustache": "4.2.0",
"openapi-types": "10.0.0",
"ora-classic": "5.4.2",
Expand Down
3 changes: 0 additions & 3 deletions specs/bundled/algoliasearch-lite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1901,9 +1901,6 @@ servers:
security:
- appId: []
apiKey: []
tags:
- name: search
description: Search API reference.
paths:
/1{path}:
post:
Expand Down
17 changes: 17 additions & 0 deletions website/docs/commitAndPullRequest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Commit and Pull-request
---

# Commit and Pull-request

## Commit

If you accidentally include generated files in your commit, the `pre-commit` hook will automatically unstage them.

We create commits on the CI as well, and in that case, we skip this unstaging behavior with the environment variable `CI=true` given.

If you want to change the patterns of generated file paths, see [config/generation.config.js](https://github.com/algolia/api-clients-automation/blob/main/config/generation.config.js).

## Pull-request

Semantic title is required. It's validated by [GitHub Action](https://github.com/deepakputhraya/action-pr-title). See [pr-title.yml](https://github.com/algolia/api-clients-automation/blob/main/.github/workflows/pr-title.yml) for the complete regular expressions.
2 changes: 1 addition & 1 deletion website/docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To contribute to the repository, make sure to take a look at our guidelines and
- [Setup the repository tooling](/docs/setupRepository): to install our tooling.
- [Add a new client](/docs/addNewClient): to add a new client spec to generate.
- [Support a new language](/docs/addNewLanguage): to add a new supported language to the API clients.
- [Pull-request](/docs/pullRequest): to see how to send pull-requests.
- [Commit and Pull-request](/docs/commitAndPullRequest): to see how to commit and send pull-requests.
- [Release process](/docs/releaseProcess): to see how to release API clients.

CLI commands can be found at [CLI > specs commands](/docs/specsCommands) and [CLI > generation commands](/docs/generationCommands)
Expand Down
7 changes: 0 additions & 7 deletions website/docs/pullRequest.md

This file was deleted.

40 changes: 40 additions & 0 deletions website/docs/releaseProcess.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,43 @@ The [GitHub action release](https://github.com/algolia/api-clients-automation/bl
This Part 3 runs conditionally according to what has been done in Part 2. Under "Version Changes" section of the release issue, if a language is checked, this Part 3 will creates a commit like `chore: release v<NEXT-VERSION>` in each repository. If it is not checked, it will create a commit like `chore: update repo <DATE-STAMP>`.

Each language repository should have their own release process, and should run only when the latest commit starts with `chore: release`. By doing so, we have a way to just update the repository, for example READMEs, without having to release.

## Releasing manually

### Java

Java is released to [sonatype](https://oss.sonatype.org/) before being sent to [Maven](https://search.maven.org/artifact/com.algolia/algoliasearch-core) central repository, the `jar` need to be signed before publishing, and then verified on sonatype by using `closeAndRelease` target on Gradle.
All of this is handled in the [release action](https://github.com/algolia/algoliasearch-client-java-2/tree/next/.github/workflows/release.yml), executed on the [Java repository](https://github.com/algolia/algoliasearch-client-java-2).
If you want to release manually, you need to copy some secrets to either:
- `clients/algoliasearch-client-java-2/gradle.properties` /!\ make sure to remove them before committing !
- `~/.gradle/gradle.properties` which is safer because it's not committed and can stay on your computer.

The secrets are fetched from the vault, make sure you have access to `api-clients-squad`, and then read the value and place them in the `gradle.properties` file you want (don't copy this file verbatim):
```bash
signingInMemoryKey="$(vault read -field sub_private_key secret/algolia/api-clients-squad/maven-signing | awk 'NR == 1 { } 1' ORS='\\n')"
signingInMemoryKeyId=$(vault read -field subkey_id secret/algolia/api-clients-squad/maven-signing)
signingInMemoryKeyPassword=$(vault read -field password secret/algolia/api-clients-squad/maven-signing)

mavenCentralUsername=$(vault read -field user secret/algolia/api-clients-squad/sonatype)
mavenCentralPassword=$(vault read -field password secret/algolia/api-clients-squad/sonatype)
```

To release a snapshot, you need to add `-SNAPSHOT` to the `VERSION_NAME` in `clients/algoliasearch-client-java-2/gradle.properties`, then to release run:
` ./gradle/gradlew -p clients/algoliasearch-client-java-2 --no-parallel publish`

And if it's not a snapshot, run:
` ./gradle/gradlew -p clients/algoliasearch-client-java-2 closeAndReleaseRepository`

Once the package is published, it can be used in gradle file as:
```gradle
repositories {
mavenCentral()
maven { url = "https://oss.sonatype.org/content/repositories/snapshots/" }
}
dependencies {
implementation 'com.algolia:algoliasearch-core:0.0.1-SNAPSHOT'
}
```

If it's not a snapshot, you can ignore the sonatype repository.
2 changes: 1 addition & 1 deletion website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const sidebars = {
},
'addNewClient',
'addNewLanguage',
'pullRequest',
'commitAndPullRequest',
'releaseProcess',
],
},
Expand Down
Loading

0 comments on commit febb627

Please sign in to comment.