Skip to content

Commit 43703eb

Browse files
committed
Move shared methods to common Action* classes
Feedback and GitMirror actions are the same and share most of the code. Finish hooks are also similar, the only difference is that we don't allow to return an error/noop, since they are kind of "auxiliary" tasks we do to the migration and we don't want to mask a migration error with a hook error. Also a hook error shouldn't block migrations. Change-Id: I5e2304936b8897e221572af153ef94879b060be2
1 parent 11c1f20 commit 43703eb

17 files changed

Lines changed: 357 additions & 328 deletions

docs/reference.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [console.verbose](#console.verbose)
2525
- [console.warn](#console.warn)
2626
- [core](#core)
27+
- [core.action](#core.action)
2728
- [core.copy](#core.copy)
2829
- [core.dynamic_feedback](#core.dynamic_feedback)
2930
- [core.dynamic_transform](#core.dynamic_transform)
@@ -58,7 +59,10 @@
5859
- [feedback.context.record_effect](#feedback.context.record_effect)
5960
- [feedback.context.success](#feedback.context.success)
6061
- [feedback.finish_hook_context](#feedback.finish_hook_context)
62+
- [feedback.finish_hook_context.error](#feedback.finish_hook_context.error)
63+
- [feedback.finish_hook_context.noop](#feedback.finish_hook_context.noop)
6164
- [feedback.finish_hook_context.record_effect](#feedback.finish_hook_context.record_effect)
65+
- [feedback.finish_hook_context.success](#feedback.finish_hook_context.success)
6266
- [feedback.revision_context](#feedback.revision_context)
6367
- [filter_replace](#filter_replace)
6468
- [folder](#folder)
@@ -585,6 +589,21 @@ Name | Type | Description
585589
<span style="white-space: nowrap;">`--validate-starlark`</span> | *string* | Starlark should be validated prior to execution, but this might break legacy configs. Options are LOOSE, STRICT
586590
<span style="white-space: nowrap;">`-v, --verbose`</span> | *boolean* | Verbose output.
587591

592+
<a id="core.action" aria-hidden="true"></a>
593+
### core.action
594+
595+
Create a dynamic Skylark action. This should only be used by libraries developers. Actions are Starlark functions that receive a context, perform some side effect and return a result (success, error or noop).
596+
597+
`dynamic.action core.action(impl, params={})`
598+
599+
600+
#### Parameters:
601+
602+
Parameter | Description
603+
--------- | -----------
604+
impl | `callable`<br><p>The Skylark function to call</p>
605+
params | `dict`<br><p>The parameters to the function. Will be available under ctx.params</p>
606+
588607
<a id="core.copy" aria-hidden="true"></a>
589608
### core.copy
590609

@@ -1453,7 +1472,7 @@ console | Get an instance of the console to report errors or warnings
14531472
destination | An object representing the destination. Can be used to query or modify the destination state
14541473
feedback_name | The name of the Feedback migration calling this action.
14551474
origin | An object representing the origin. Can be used to query about the ref or modifying the origin state
1456-
params | Parameters for the function if created with core.dynamic_feedback
1475+
params | Parameters for the function if created with core.action
14571476
refs | A list containing string representations of the entities that triggered the event
14581477

14591478
<a id="feedback.context.error" aria-hidden="true"></a>
@@ -1526,9 +1545,37 @@ console | Get an instance of the console to report errors or warnings
15261545
destination | An object representing the destination. Can be used to query or modify the destination state
15271546
effects | The list of effects that happened in the destination
15281547
origin | An object representing the origin. Can be used to query about the ref or modifying the origin state
1529-
params | Parameters for the function if created with core.dynamic_feedback
1548+
params | Parameters for the function if created with core.action
15301549
revision | Get the requested/resolved revision
15311550

1551+
<a id="feedback.finish_hook_context.error" aria-hidden="true"></a>
1552+
### feedback.finish_hook_context.error
1553+
1554+
Returns an error action result.
1555+
1556+
`dynamic.action_result feedback.finish_hook_context.error(msg)`
1557+
1558+
1559+
#### Parameters:
1560+
1561+
Parameter | Description
1562+
--------- | -----------
1563+
msg | `string`<br><p>The error message</p>
1564+
1565+
<a id="feedback.finish_hook_context.noop" aria-hidden="true"></a>
1566+
### feedback.finish_hook_context.noop
1567+
1568+
Returns a no op action result with an optional message.
1569+
1570+
`dynamic.action_result feedback.finish_hook_context.noop(msg=None)`
1571+
1572+
1573+
#### Parameters:
1574+
1575+
Parameter | Description
1576+
--------- | -----------
1577+
msg | `string` or `NoneType`<br><p>The no op message</p>
1578+
15321579
<a id="feedback.finish_hook_context.record_effect" aria-hidden="true"></a>
15331580
### feedback.finish_hook_context.record_effect
15341581

@@ -1547,6 +1594,13 @@ destination_ref | `destination_ref`<br><p>The destination ref</p>
15471594
errors | `sequence of string`<br><p>An optional list of errors</p>
15481595
type | `string`<br><p>The type of migration effect:<br><ul><li><b>'CREATED'</b>: A new review or change was created.</li><li><b>'UPDATED'</b>: An existing review or change was updated.</li><li><b>'NOOP'</b>: The change was a noop.</li><li><b>'INSUFFICIENT_APPROVALS'</b>: The effect couldn't happen because the change doesn't have enough approvals.</li><li><b>'ERROR'</b>: A user attributable error happened that prevented the destination from creating/updating the change. <li><b>'STARTED'</b>: The initial effect of a migration that depends on a previous one. This allows to have 'dependant' migrations defined by users.<br>An example of this: a workflow migrates code from a Gerrit review to a GitHub PR, and a feedback migration migrates the test results from a CI in GitHub back to the Gerrit change.<br>This effect would be created on the former one.</li></ul></p>
15491596

1597+
<a id="feedback.finish_hook_context.success" aria-hidden="true"></a>
1598+
### feedback.finish_hook_context.success
1599+
1600+
Returns a successful action result.
1601+
1602+
`dynamic.action_result feedback.finish_hook_context.success()`
1603+
15501604

15511605

15521606
## feedback.revision_context

java/com/google/copybara/Core.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,7 @@ public Transformation dynamic_transform(
14671467
impl, Dict.<Object, Object>copyOf(thread.mutability(), params), printHandler);
14681468
}
14691469

1470+
// TODO(malcon): Deprecate this method once all references moved to core.action
14701471
@SuppressWarnings("unused")
14711472
@StarlarkMethod(
14721473
name = "dynamic_feedback",
@@ -1490,6 +1491,29 @@ public Action dynamicFeedback(StarlarkCallable impl, Dict<?, ?> params, Starlark
14901491
impl, Dict.<Object, Object>copyOf(thread.mutability(), params), printHandler);
14911492
}
14921493

1494+
@SuppressWarnings("unused")
1495+
@StarlarkMethod(
1496+
name = "action",
1497+
doc = "Create a dynamic Skylark action. This should only be used by libraries"
1498+
+ " developers. Actions are Starlark functions that receive a context, perform"
1499+
+ " some side effect and return a result (success, error or noop).",
1500+
parameters = {
1501+
@Param(
1502+
name = "impl",
1503+
named = true,
1504+
doc = "The Skylark function to call"),
1505+
@Param(
1506+
name = "params",
1507+
named = true,
1508+
doc = "The parameters to the function. Will be available under ctx.params",
1509+
defaultValue = "{}"),
1510+
},
1511+
useStarlarkThread = true)
1512+
public Action action(StarlarkCallable impl, Dict<?, ?> params, StarlarkThread thread) {
1513+
return new StarlarkAction(
1514+
impl, Dict.<Object, Object>copyOf(thread.mutability(), params), printHandler);
1515+
}
1516+
14931517
@SuppressWarnings("unused")
14941518
@StarlarkMethod(
14951519
name = "fail_with_noop",

java/com/google/copybara/SkylarkContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ public interface SkylarkContext<T> {
2828
T withParams(Dict<?, ?> params);
2929

3030
/** Performs tasks after Starlark code finishes. */
31-
void onFinish(Object result, SkylarkContext<?> context) throws ValidationException;
31+
void onFinish(Object result, SkylarkContext<T> context) throws ValidationException;
3232
}

java/com/google/copybara/TransformWork.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,8 @@ private void setDateForCurrentRev(Map<String, ImmutableList<String>> labels) {
754754
}
755755

756756
@Override
757-
public void onFinish(Object result, SkylarkContext<?> context) throws ValidationException {
757+
public void onFinish(Object result, SkylarkContext<TransformWork> context)
758+
throws ValidationException {
758759
checkCondition(
759760
result == null || result.equals(Starlark.NONE),
760761
"Transform work cannot return any result but returned: %s",

java/com/google/copybara/Workflow.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.google.copybara.util.Glob;
5151
import com.google.copybara.util.Identity;
5252
import com.google.copybara.util.console.Console;
53+
5354
import java.io.IOException;
5455
import java.nio.file.Path;
5556
import java.nio.file.Paths;
@@ -62,6 +63,7 @@
6263
import java.util.logging.Level;
6364
import java.util.logging.Logger;
6465
import java.util.stream.Collectors;
66+
6567
import javax.annotation.Nullable;
6668

6769
/**

java/com/google/copybara/action/Action.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package com.google.copybara.action;
1818

1919
import com.google.common.collect.ImmutableSetMultimap;
20-
import com.google.copybara.SkylarkContext;
2120
import com.google.copybara.exception.RepoException;
2221
import com.google.copybara.exception.ValidationException;
22+
2323
import net.starlark.java.annot.StarlarkBuiltin;
2424
import net.starlark.java.eval.StarlarkValue;
2525

@@ -34,7 +34,7 @@
3434
documented = false)
3535
public interface Action extends StarlarkValue {
3636

37-
void run(SkylarkContext<?> context) throws ValidationException, RepoException;
37+
void run(ActionContext context) throws ValidationException, RepoException;
3838

3939
String getName();
4040

0 commit comments

Comments
 (0)