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

Bug 1805455: Remove Bugzilla-updating logic from Phabricator uplift extension #35

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -72,95 +72,6 @@ public function shouldAppearInApplicationTransactions() {
return true;
}

/* Set QE required flag on the relevant Bugzilla bug. */
public function setManualQERequiredFlag(int $bug) {
// Construct request for setting `qe-verify` flag, see
// https://bmo.readthedocs.io/en/latest/api/core/v1/bug.html#update-bug
$url = id(new PhutilURI(PhabricatorEnv::getEnvConfig('bugzilla.url')))
->setPath('/rest/bug/'.$bug);
$api_key = PhabricatorEnv::getEnvConfig('bugzilla.automation_api_key');

// Encode here because `setData` below will fail due to nested arrays.
$data = phutil_json_encode(
array(
'flags' => array(
array(
'name' => 'qe-verify',
'status' => '?',
),
),
),
);

$future = id(new HTTPSFuture($url))
->addHeader('Accept', 'application/json')
->addHeader('Content-Type', 'application/json')
->addHeader('User-Agent', 'Phabricator')
->addHeader('X-Bugzilla-API-Key', $api_key)
->setData($data)
->setMethod('PUT')
->setTimeout(PhabricatorEnv::getEnvConfig('bugzilla.timeout'));

try {
list($status, $body) = $future->resolve();
$status_code = (int) $status->getStatusCode();

# Return an error string and invalidate transaction if Bugzilla can't be contacted.
$body = phutil_json_decode($body);
if (array_key_exists("error", $body) && $body["error"]) {
throw new Exception(
'Could not set `qe-verify` on Bugzilla: status code: '.$status_code.'! Please file a bug.'
);
}

} catch (PhutilJSONParserException $ex) {
throw new Exception(
'Expected invalid JSON response from BMO while setting `qe-verify` flag.'
);
}
}

/* Comment the uplift request form on the relevant Bugzilla bug. */
public function commentUpliftOnBugzilla(int $bug) {
// Construct request for leaving a comment, see
// https://bmo.readthedocs.io/en/latest/api/core/v1/comment.html#create-comments
$url = id(new PhutilURI(PhabricatorEnv::getEnvConfig('bugzilla.url')))
->setPath('/rest/bug/'.$bug.'/comment');
$api_key = PhabricatorEnv::getEnvConfig('bugzilla.automation_api_key');

$data = array(
'comment' => $this->getRemarkup(),
'is_markdown' => true,
'is_private' => false,
);

$future = id(new HTTPSFuture($url))
->addHeader('Accept', 'application/json')
->addHeader('User-Agent', 'Phabricator')
->addHeader('X-Bugzilla-API-Key', $api_key)
->setData($data)
->setMethod('POST')
->setTimeout(PhabricatorEnv::getEnvConfig('bugzilla.timeout'));

try {
list($status, $body) = $future->resolve();
$status_code = (int) $status->getStatusCode();

# Return an error string and invalidate transaction if Bugzilla can't be contacted.
$body = phutil_json_decode($body);
if (array_key_exists("error", $body) && $body["error"]) {
throw new Exception(
'Could not leave a comment on Bugzilla: status code '.$status_code.'! Please file a bug.',
);
}

} catch (PhutilJSONParserException $ex) {
throw new Exception(
'Received invalid JSON response from BMO while leaving a comment.'
);
}
}

/* -( Edit View )---------------------------------------------------------- */

public function shouldAppearInEditView() {
@@ -341,10 +252,6 @@ public function validateUpliftForm(array $form): array {
return $validation_errors;
}

public function qeRequired() {
return $this->getValue()['Needs manual QE test'] === true;
}

public function validateApplicationTransactions(
PhabricatorApplicationTransactionEditor $editor,
$type, array $xactions) {
@@ -371,37 +278,6 @@ public function validateApplicationTransactions(
return $errors;
}

// Update Bugzilla when applying effects.
public function applyApplicationTransactionExternalEffects(
PhabricatorApplicationTransaction $xaction
) {
$ret = parent::applyApplicationTransactionExternalEffects($xaction);

// Don't update Bugzilla when the field is empty.
if (empty($this->getvalue())) {
return $ret;
}

// Similar idea to `BugStore::resolveBug`.
// We should have already checked for the bug during validation.
$bugzillaField = new DifferentialBugzillaBugIDField();
$bugzillaField->setObject($this->getObject());
(new PhabricatorCustomFieldStorageQuery())
->addField($bugzillaField)
->execute();
$bug = $bugzillaField->getValue();

// Always comment the uplift form.
$this->commentUpliftOnBugzilla($bug);

// If QE is required, set the Bugzilla flag.
if ($this->qeRequired()) {
$this->setManualQERequiredFlag($bug);
}

return $ret;
}

// When storing the value convert the question => answer mapping to a JSON string.
public function getValueForStorage(): string {
return phutil_json_encode($this->getValue());