YahnisElsts / plugin-update-checker

A custom update checker for WordPress plugins. Useful if you don't want to host your project in the official WP repository, but would still like it to support automatic updates. Despite the name, it also works with themes.
MIT License
2.22k stars 403 forks source link

Add a filter for prereleases #506

Closed jihaisse closed 1 year ago

jihaisse commented 1 year ago

With this, you can filter on prerelease for example :

$myUpdateChecker->getVcsApi()->filterPrereleaseName('beta');

Will return only the last release with semver like "1.0.0-beta.1"

It's just a proposal, I'm not sure you will include this like that, the code may be improved.

YahnisElsts commented 1 year ago

Could you elaborate on the intent of this change? The code itself looks relatively simple, but I'm not sure I fully understand what goal it's trying to accomplish.

By the way, the "latest release" endpoint of the GitHub API specifically does not return pre-releases, so this approach might not work with that.

jihaisse commented 1 year ago

Yes of course !

For example, let's say we have a dev environment, a staging and a prod environment.

In my Gitlab, I could have a main branch, a dev branch and a staging branch. The main is for the prod, dev for dev and staging for staging.

With Gitlab I've made a CI uising semantic-release, so when I push on the main branch, it make a release with the version number updated, for example 1.1.0.

I've got the same with the staging branch, and the release will be 1.1.0-staging.1

So, in my plugin, I check if I am on local / dev or staging or production, for example with the domain name.

This way, I can filter on the release name and PUC will give me the latest release I want depending of the environment.

So if I'm in staging, I will configure PUC with : $myUpdateChecker->getVcsApi()->filterPrereleaseName('staging');

If I am in production, I have no configuration to make, and PUC will return the latest release that match exactly the version number x.x.x (the second regex)

And if I am in local or in dev, I can use a branch.

Let me know if I am not clear enough.

OK for GitHub, I wasn't aware of that, I saw the line on the code but didn't noticed it. I remember that I was looking for a such flag in GitLab but without success.

YahnisElsts commented 1 year ago

Thank you for the additional details. Given this, I have two issues with the proposed implementation:

I've added setReleaseVersionFilter() and setReleaseFilter() methods that implement these ideas. Here's an example:

//Allow only beta versions (e.g. for testing).
$updateChecker->getVcsApi()->setReleaseVersionFilter(
    '/beta/i', //Regex for the version number.
    Api::RELEASE_FILTER_ALL, //Optional. Disables the default filter that normally removes prereleases.
    30 //Optional. Max number of recent releases to scan for matches.
);
jihaisse commented 1 year ago

Hello, thanks for the update.

I have a strange behavior now, the link "check for updates" is missing in the UI. And PUC doesn't detect any new release or update from the main branch

YahnisElsts commented 1 year ago

What exactly do you mean by "missing the UI"? Also, is this happening with just the patch, or have you actually added a custom filter?

If "missing the UI" means that there's a blank page somewhere, that could indicate a PHP error. Please check the PHP error log for recent errors.

jihaisse commented 1 year ago

I have removed all filters to have something like this, and only this :

    new GitLabApi(
        'https://gitlab.example.com/wordpress/plugins/mon-plugin/',
        'glpat-xxxxxx',
        'plugins'
    ),
    __FILE__,
    'mon-plugin'
);

Capture d’écran 2022-11-25 à 11 05 27

So in the plugins page, in the metabox of the plugin the link to force a check for an update is missing.

YahnisElsts commented 1 year ago

In the screenshot, it looks like the plugin is inactive. Is the code that sets up the update checker inside the plugin? In that case, it won't work unless the plugin is active. The update checker can't display the "check for updates" link if the code isn't actually running.

jihaisse commented 1 year ago

OK, my bad ! I've spend one hour on this 😅

jihaisse commented 1 year ago

I've tried with the setReleaseVersionFilter() it's working like a charm.

I have a warning in my IDE : Method 'setReleaseVersionFilter' not found in YahnisElsts\PluginUpdateChecker\v5p0\Vcs\Api when I call $myUpdateChecker->getVcsApi()->setReleaseVersionFilter()

If you know how to resolve this.

Thanks for the job !

YahnisElsts commented 1 year ago

This might vary depending on the IDE, but I think it's probably because the Api base class does not have this method, only some of the subclasses do. I didn't add it to the base class because some APIs integrations (currently BitBucket) don't support releases or release filters.

jihaisse commented 1 year ago

OK, thanks ! I think this pull request can be closed now.

YahnisElsts commented 1 year ago

All right, I am closing the PR.